☰ Contents · Computing

Practical tasks and linear programs

Lessons 32–33 · 2 lessons · M. R. Fayziyeva, D. M. Sayfurov, N. S. Xaytullayeva. Informatics and Information Technologies, Grade 9. “Nashriyot uyi Tasvir”, Tashkent, 2020
33

Programming simple problems in Python

Textbook: pp. 70–71
GoalKnows the concepts of a linear algorithm and a linear program, solves a simple task in four stages (data, result, algorithm, testing), and matches a flowchart with program code.
New words
linear algorithm: commands carried out one after another in a strict order, with no conditions or repeats · chiziqli algoritmlinear program: a linear algorithm written as a program · chiziqli dasturinput data: the values the program receives to work with · kiruvchi ma’lumotoutput data: the result the program shows · chiquvchi ma’lumot
Explanation

In a linear algorithm all commands are carried out one after another from top to bottom: no condition is checked and nothing is repeated. A simple task is solved in four stages: 1) find the input data, its type and its name; 2) find what the result is and which type it will have; 3) build the algorithm: input, calculation, output; 4) test it with different values. In a flowchart the oval shows the start and the end, the parallelogram input and output, and the rectangle a calculation; each of them matches one or several lines of code. For example the input block matches an input() line, the calculation block an assignment line, and the output block print(). The fourth stage matters most: by trying an example whose answer is known in advance you see that the algorithm works correctly.

Worked examples
Speed v = s / t: s = int(input()) ⏎ t = int(input()) ⏎ v = s / t ⏎ print(v) The input data are s and t, the result is v (a float). With 180 km and 3 hours the program prints 60.0.
Change: a = int(input()) ⏎ n = int(input()) ⏎ m = int(input()) ⏎ print(m - a * n) The price of a notebook is 4500, 3 were bought and 20000 so‘m was paid: the change is 20000 − 4500 · 3 = 6500 so‘m.
Class activity

“From chart to code”: the group draws a flowchart on the board for the volume of a cube (oval – parallelogram – rectangle – parallelogram – oval), then writes the matching line of code under each block.

Practice
1
List in order the four stages of solving a simple task.
2
The edge a of a cube is typed and its volume is printed. Write the program (64 when a = 4).
3
What is the area of a trapezoid with bases 6 and 10 and height 5 by (a + b) * h / 2?
4
Why must a finished algorithm be tested with different values?