We've Already Worked With It!
Programs written so far have used a
linear structure, which is represented by
a sequence of instructions
that execute every time we run the Python code, regardless of the input
values.
We have read input data, assigned values, performed calculations, and displayed the result in the console.
EXAMPLE
Let's assume we want to create a program that reads three integers from the keyboard
and displays their
arithmetic mean.
The calculation formula is simple. For
a,
b,
and
c, the arithmetic mean is:
How do we approach designing the algorithm?
Well... first we read the three values and store them as
integers in the three variables,
a,
b,
and
c. We can define a variable
ma which will hold the arithmetic mean
using the expression
(a+b+c)/3. Finally, we display the result,
which is the value stored in
ma!
Look at the logic diagram below. Let's assume the values
9,
10, and
8 were entered:
|
Execute schema
|
Nothing simpler than to
write it now in Python:
PROPOSED EXERCISE
Modify the algorithm above to display the
geometric mean of the three numbers,
using the following formula:
Next, we will study some basic linear algorithms together.
Execute the diagram, the program, and solve the exercise.