Lesson #9. Sum, Accumulators, Product and Counter. Nested loops
Sum
Task:. 10 numbers are given. It’s required to calculate their sum.
Solution: The sum
variable is initialized by 0 before loop. Every loop iteration the variable sum
increments by a value of the next entered number.
LOOP | FOR |
---|---|
Syntax:
The sum in Pascal is calculated by the recurrent expression: S = S + Y where***********************************************************************************************S
is the accumulated amountY
– next summand
✎
1.{0.3 points}[task-01-sum.pas]
5 integers are entered. After entering each number the program should print their sum:
'please, enter 5 numbers:' entered number: 4 => sum = 4 entered number: 7 => sum = 11 (4 + 7 = 11) entered number: 3 => sum = 14 (11 + 3 = 14) ... etc.
2.{0.3 points}[task-02-sum.pas]
5 real numbers are entered. The program should output their sum only once, just before the end of the program:
entered numbers: 4.2 7.1 3.1 2.5 8.6 => sum = 25.5
3.{0.3 points}[task-03-sum.pas]
Count the sum of 10 numbers: 1 + 2 + 3 + ... + 10
. Better use for
loop.
4.{0.3 points}[task-04-sum.pas]
Count the sum of 5 numbers: 1 + 3 + 5 + 7 + 9
. Better use for
loop with an arbitrary step.
'1 + 3 + 5 + 7 + 9 =' >>> 25
5.1{0.4 points}[task-05_1-sum.pas]
Calculate a sum of all odd integers up to 99 (1+ 3 + 5 + 7 + 9 + 11 + ... + 99). Use for
loop with an arbitrary step.
'1 + 3 + 5 + 7 + 9 + ... + 99 =' >>> 2500
5.2{0.4 points}[task-05_2-sum.pas]
Calculate a sum of all odd integers up to 99 (1+ 3 + 5 + 7 + 9 + 11 + ... + 97 + 99). Use loop
.
Sum – short solution
Task:. 10 integers are given. It’s required to calculate their sum.
PascalABC.NET has a short form of the solution:✎
6.{0.3 points}[task-06-sum.pas]
5 integers are entered. The program should output their sum only once, before the end of the program. Use short form as in the example.
Product (multiplication)
Task: We have 10 real numbers. It’s required to calculate their product (multiplication).
Solution: The product
variable is initialized by 1 value before loop. Every loop iteration the product
increments by the value of the next number.
LOOP | FOR |
---|---|
**********************************************************************************************
✎
1.{0.3 points}[task-01-product.pas]
5 integers are entered. After entering each number the program should print their product:
'please, enter 5 numbers:' entered number: 4 => product = 4 entered number: 3 => product = 12 (4 * 3 = 12) entered number: 5 => product = 60 (12 * 5 = 60) ... etc.
2.{0.3 points}[task-02-product.pas]
5 real numbers are entered. The program should output their product only once, just before the end of the program:
entered numbers: 4.2 7.1 3.1 2.5 8.6 => product = 1987.503******************************************************************************************
Product (multiplication) - short solution
Task:. 10 real numbers are given. It’s required to calculate their product.
PascalABC.NET has a short form of the solution:*****************************************************************************************
✎
3.{0.3 points}[task-03-product.pas]
5 integers are entered. The program should output their product only once - just before the end of the program. Use short form as in the example.
4.{0.4 points}[task-04-product.pas]
Calculate a product of 2-digit even integers in the interval [10;30] (10 * 12 * 14 * ... * 28 * 30). Use for
loop with an arbitrary step.
Nested Loops
Task: Find function z(x,y) = xy
for every x
varying in the interval [2;8]
and y
varying in the interval [2;5]
.
Solution: There have to be organized two for
loops (nested loops): one loop is inside the other. x
has to be modified in outer loop, y
has to be modified in inner loop.
Result:
z(x,y) = 2^2 = 4 z(x,y) = 2^3 = 8 z(x,y) = 2^4 = 16 z(x,y) = 3^2 = 9 z(x,y) = 3^3 = 27 z(x,y) = 3^4 = 81 z(x,y) = 4^2 = 16 z(x,y) = 4^3 = 64 z(x,y) = 4^4 = 256 z(x,y) = 5^2 = 25 z(x,y) = 5^3 = 125 z(x,y) = 5^4 = 625 ... etc.***********************************************************************************************
✎
7.{0.4 points}[task-07-nested_loops.pas]
Find function z(x,y) = x+2y
for every x
varying in the interval [5;10]
and y
varying in the interval [1;5]
.
8.{0.4 points}[task-08-nested_loops.pas]
Find function z(x,y) = x-y
for every x
varying in the interval [30;33]
and y
varying in the interval [1;5]
. For beautiful output use writelnFormat
statement.
Example:
30-1=29 30-2=28 30-3=27 30-4=26 30-5=25 31-1=30 31-2=29 31-3=28 31-4=27 31-5=26 32-1=31 32-2=30 32-3=29 32-4=28 32-5=27 33-1=32 33-2=31 33-3=30 33-4=29 33-5=28
9.{0.4 points}[task-09-nested_loops.pas]
The program must display the next sequences as in the example below. Use nested loops:
0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 .... 9 9 9 9 9
10.{0.4 points}[task-10-nested_loops.pas]
The program must display the next sequences as in the example below. Use nested loops:
1 1 1 1 1 3 3 3 3 3 5 5 5 5 5 7 7 7 7 7 9 9 9 9 9****************************************
Counters
Task: n >= 0
is given. The program should ask to input n
integer numbers from keyboard and should find a quantity of odd numbers among entered numbers.
Solution: We will use an if statement in a loop. After entering a value we run if statement to check if the value is odd. If it is, so we use the counter
to count.
✎
1.{0.2 points}[task-01-counter.pas]
Print the sequence: 1 2 3 4 . . . 99 100
. Use loop and a variable counter
:
... var counter:=0; loop 100 do begin ... print(counter); end;
2.{0.4 points}[task-02-counter.pas]
Print the sequence: 1 2 3 4 . . . 99 100 99 . . . 3 2 1
. Use two loops and two counters: first loop 1 2 3 4 . . . 99 100
, the second loop 99 . . . 3 2 1
3.{0.4 points}[task-03-counter.pas]
10 integers are given. Use loop and a variable counter
to find the quantity of positive among the numbers.
1 -5 -12 2 3 9 -1 9 5 -8 => counter = 4***********************************************
Several counters
Task: Given n marks on an exam (from 2 to 5). Calculate a number of every mark.
Solution: We will use several counters, each counter for every mark.
✎
4.{0.4 points}[task-04-counter.pas]
10 integers are given. Use loop and two counters
to find the quantity of positive and negative among the numbers.
1 -5 -12 2 3 9 -1 9 5 -8 => counter_positive = 6, counter_negative = 4
5.{0.4 points}[task-05-counter.pas]
10 integers are given. Use loop and two counters
to find the quantity of even and odd among the numbers.
1 -5 -12 2 3 9 -1 9 5 -8 => counter_even = 3, counter_odd = 7