Creating a program from an algorithm

 

Creating a program from an algorithm

Consider this simple problem. A cinema is offering discount tickets to anyone who is under 15. Decomposing this problem, gives this algorithm:

  1. find out how old the person is
  2. if the person is younger than 15 then say “You are eligible for a discount ticket.”
  3. otherwise, say “You are not eligible for a discount ticket.”

In pseudocode, the algorithm would look like this:

OUTPUT "How old are you?" INPUT User inputs their age STORE the user's input in the age variable IF age < 15 THEN OUTPUT "You are eligible for a discount." ELSE OUTPUT "You are not eligible for a discount."

To convert the flowchart or pseudocode into a program, look at each individual step, and write an equivalent instruction. Sometimes the steps will not match exactly, but they will be fairly close.

In a flowchart, this algorithm would look like this:

A flowchart can be used to determine your age and whether that makes you eligible for a discount.

Creating the program in Python

Python (3.x) program to meet this algorithm would be:

age = int(input("How old are you?")) if age < 15: print("You are eligible for a discount.") else: print("You are not eligible for a discount.")

Note the similarities between the flowchart and pseudocode, and the finished program.