# Lesson #8. Sum, Accumulators, Product and Counter. Minimum and maximum value

Дата изменения: 8 октября 2020

## Theory

Lection # 8 in pdf format

Syntax:
The sum in Pascal is calculated by the recurrent expression:

S = S + Y
where S is the accumulated amount
Y – next summand


Lab 1:

To do: 10 numbers are given. It’s required to calculate their sum.

✍ Algorithm: The sum variable is initialized by 0 before loop. Every loop iteration the variable sum is incremented by a value of the next entered number.
{0.3 points} Task 1, sum:

To do: 5 integers are entered. After entering each number, the program should print their sum.

The resulting example:

'please, enter 5 numbers:'
>>> 4
entered number: 4  => sum = 4
>>> 11
entered number: 7 => sum = 11
>>> 14
entered number: 3 => sum = 14
... etc.


[Program name: task-01-sum.pas]

{0.3 points} Task 2, sum:

To do: 5 real numbers are entered. The program should output their sum only once, just before the end of the program.

The resulting example:

entered numbers:
>>> 4.2  >>> 7.1  >>> 3.1  >>> 2.5  >>> 8.6
sum = 25.5


[Program name: task-02-sum.pas]

{0.3 points} Task 3, sum:

To do: Count the sum of 10 numbers: 1 + 2 + 3 + … + 10. Better use for loop.

[Program name: task-03-sum.pas]

{0.3 points} Task 4, sum:

To do: Count the sum of 5 numbers: 1 + 3 + 5 + 7 + 9. Better use for loop with an arbitrary step.

The resulting example:

'1 + 3 + 5 + 7 + 9 ='
25


[Program name: task-04-sum.pas]

{0.4 points} Task 5, sum:

To do: Calculate a sum of all odd integers up to 99 (1 + 3 + 5 + 7 + 9 + 11 + … + 99). Use for loop with an arbitrary step.

The resulting example:

'1 + 3 + 5 + 7 + 9 + ... + 99 ='
2500


[Program name: task-05-sum.pas]

{0.4 points} Task 6, sum:

To do: Calculate a sum of all odd integers up to 99 (1 + 3 + 5 + 7 + 9 + 11 + … + 97 + 99). Use loop.

[Program name: task-06-sum.pas]

Lab 2: Sum – short solution

To do: 10 integers are given. It’s required to calculate their sum.

✍ Algorithm: {0.3 points} Task 7, sum:

To do: 5 integers are entered. The program should output their sum only once, before the end of the program. Use short form as in the lab.

[Program name: task-07-sum.pas]

### Product (multiplication)

Lab 3:

To do: 10 reals are given. It’s required to calculate their product (multiplication).

✍ Algorithm: • The product variable is initialized by 1 value before loop. Every loop iteration the product is incremented by the value of the next number.
{0.3 points} Task 1, product:

To do: 5 integers are entered. After entering each number, the program should print their product.

The resulting example:

'please, enter 5 numbers:'
>>> 4
entered number: 4  => product = 4
>>> 3
entered number: 3 => product = 12
>>> 5
entered number: 5 => product = 60

... etc.


[Program name: task-01-product.pas]

{0.3 points} Task 2, product:

To do: 5 real numbers are entered. The program should output their product only once, just before the end of the program:

The resulting example:

entered numbers:
>>> 4.2  >>> 7.1  >>> 3.1  >>> 2.5  >>> 8.6
product = 1987.503


[Program name: task-02-product.pas]

{0.4 points} Task 3, product:

To do: 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.

The resulting example:

product = 165412864

[Program name: task-03-product.pas]

Lab 4: Product (multiplication) — short solution

To do: 10 real numbers are given. It’s required to calculate their product.

✍ Algorithm: {0.1 points} Task 4, product:

To do: 5 real numbers are entered. The program should output their product only once, just before the end of the program:

The resulting example:

entered numbers:
>>> 4.2  >>> 7.1  >>> 3.1  >>> 2.5  >>> 8.6
product = 1987.503


[Program name: task-04-product.pas]

{0.3 points} Task 5, product:

To do: 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.

The resulting example:

enter 5 integers, please:
>>>2  >>>5  >>>1  >>>7  >>>3
product is: 210

[Program name: task-05-product.pas]

### Counters

Lab:

To do: n >= 0 is given. The program should ask to input n integer numbers and find a quantity of odd numbers among entered numbers.

Solution: We’re going to use an if statement in a loop.

The resulting example:

how many numbers?
>>>4
enter the numbers:
>>>1  >>>3  >>>8  >>>7
the number of odd:  3 

✍ Algorithm:

begin print('how many numbers?'); var n := ReadInteger; print('enter the numbers:'); var count := 0; loop n do begin var x := ReadInteger; if x mod 2 <> 0 then count += 1; end; print('the number of odd: ', count) end.
{0.2 points} Task 1, counters:

To do: Print the sequence: 1 2 3 4 . . . 99 100. Use loop and a variable counter:
A piece of code:

... var counter:=0; loop 100 do begin ... print(counter); end;

The resulting example:

1 2 3 4 5 . . . 99 100


[Program name: task-01-counters.pas]

{0.3 points} Task 2, counters:

To do: Print the sequence: 1 2 3 4 . . . 99 100 99 . . . 3 2 1.

Note: Create two loops: the first loop 1 2 3 4 . . . 99 100, the second loop 99 . . . 3 2 1.

The resulting example:

1 2 3 4 . . . 97 98 99 100 99 98 . . . 4 3 2 1


[Program name: task-02-counters.pas]

{0.2 points} Task 3, counters:

To do: 10 integers are given. You should use for loop and variable named counter to find the quantity of positive among the numbers.

Note: to generate the sequence of numbers you should use random function:

var genNumb: integer; genNumb:=random (a,b);

Note: you can complete the following code:

begin var genNumb, i: integer; var counter := 0; for i := 1 to 10 do begin genNumb := random(-5, 10); // to do ... // ... end; writeln; print('numbers of positive: ', counter) end.

The resulting example:

1 6 10 10 -2 1 -4 4 -4 2
numbers of positive:  7


[Program name: task-03-counters.pas]

### Several counters

Lab:

To do: n grades (marks) on an exam (from 2 to 5) are given. Calculate the number of each grade (how many “2”, how many “3” …).

Solution: We will use several counters, each counter for each grade.

Expected results

how many grades? 5
4
4
4
2
3
"2"= 1, "3"= 1, "4"= 3, "5"= 0


✍ Algorithm:

begin var n := ReadInteger('how many grades?'); var (c2, c3, c4, c5) := (0, 0, 0, 0); writeln('enter grades, please'); loop n do begin var Mark := ReadInteger; case Mark of 2: c2 += 1; 3: c3 += 1; 4: c4 += 1; 5: c5 += 1; end; end; Print($'"2"= {c2}, "3"= {c3}, "4"= {c4}, "5"= {c5}'); end. {0.4 points} Task 4, counters: To do: 10 integers are given. Use loop and two counters to find the quantity of positive and negative among the numbers. Note: to generate the sequence of numbers you should use random function: var genNumb: integer; genNumb:=random(a,b); The resulting example: 1 -5 -12 2 3 9 -1 9 5 -8 counter positive = 6, counter negative = 4  [Program name: task-04-counters.pas] {0.4 points} Task 5, counters: To do: 10 integers are given. Use loop and two counters to find the quantity of even and odd among the numbers. The resulting example: 1 -5 -12 2 3 9 -1 9 5 -8 counter even = 3, counter odd = 7  [Program name: task-05-counters.pas] ### Minimum and maximum value Lab: To do: n numbers are given (n >= 0). Find minimum value of these numbers. ✍ Algorithm: Solution 1. Let’s assign the first entered number to min variable. Then, in a loop check if the next entered number is less than min. If it is, so reassign this value to min: begin var n:=readinteger('enter n'); var x := ReadReal; var min := x; loop n - 1 do begin x := ReadReal; if x < min then min := x; end; print($'min = {min}') end.

What is not good: The first value is handled separately.

Solution 2. Let’s set the maximum value of real type (real.MaxValue) to min variable. Then, in a loop check if the first entered number is less than min. If it is, so reassign this value to min.

The idea is that after the first iteration min will be reassigned in any case because x < real.MaxValue:

begin var n:=readinteger('enter n'); var x:real; var min := real.MaxValue; loop n do begin x := ReadReal; if x < min then min := x; end; print($'min = {min}') end. {0.2 points} Task 1, min & max: To do: 5 numbers are entered. Output the maximum and minimum of the entered numbers. Note: complete the folowing code: begin var // ...; var min := integer.MaxValue; var max := integer.MinValue; loop 5 do begin // ... end; print($'min = {min}, max = {max}') end.

The resulting example:

enter 5 integers:
>>> 5  >>> 3  >>> 8  >>> 1  >>> 2
maximum is 8, minimum is 1


[Program name: task-01-maxmin.pas]

{0.5 points} Task 2, min & max:

To do: An integer N is given and a set of N rectangles defined by their sides (pairs of numbers a and b). Calculate the areas (S = a * b) of all the rectangles and minimum area of them.

The resulting example:

How many rectangles?
>>> 3
Please, enter the sides of the rectangles:
>>> 2  >>> 3
area is: 6
>>> 1  >>> 5
area is: 5
>>> 4  >>> 2
area is: 8
The minimum area of all rectangles is 5


[Program name: task-02-maxmin.pas]

{0.5 points} Task 3, min & max:

To do: An integer N is given and a set of N rectangles defined by their sides (pairs of numbers a and b). Find the perimeters of all the rectangles and maximum perimeter of them.

The resulting example:

How many rectangles?
>>> 3
Please, enter the sides of the rectangles:
>>> 2  >>> 3
perimeter: 10
>>> 1  >>> 5
perimeter: 12
>>> 4  >>> 2
perimeter: 12
The maximum perimeter of all rectangles is 12


[Program name: task-03-maxmin.pas]

{0.3 points} Task 4, min & max:

To do: Find the minimum of 5 numbers and print its position (order number). To store the position you must use extra variable.

Note: to generate the sequence of numbers you should use random function:

var genNumb: integer; genNumb:=random (a,b);

The resulting example:

5 generated numbers:
5   3   8   1   2
minimum is 1, its position is 4


[Program name: task-04-maxmin.pas]

{0.5 points} Task 5, min & max:

To do: Calculate the minimum and maximum of 10 generated numbers and print the sum of their serial numbers.

Note: to generate the sequence of numbers you should use random function:

var genNumb: integer; genNumb:=random (a,b);

The resulting example:

10 generated numbers:
2  15  3  8  1  2  9  1  7  11
max is 15 position is 2, min is 1 position is 8, 2 + 8 = 10


[Program name: task-05-maxmin.pas]

### Sums and products in the while and repeat loops

Lab:

To do: Calculate the sum of the sequence: 11 + 13 + 15 + ... + 99.

Expected results:

sum = 2475


✍ Algorythm:

While loop:

begin var x:=11; var s:=0; while x<=99 do begin s+=x; write(x); x+=2; end; Print(\$'sum = {s}'); end.

Loop: {0.3 points} Task 1, sum and while:

To do: Calculate a sum of all odd 2-digit integers (11+ 13 + 15 + 17 + 19 + 21 + ... + 99). Make the task two times - using while and repeat loops.

The resulting example:

while loop. sum = 2475
repeat loop. sum = 2475


[Program name: task-01-sum-while.pas]

{0.3 points} Task 2, sum and while:

To do: Calculate a product of 2-digit even integers in the range [10;30] (10 * 12 * 14 * ... * 28 * 30). Make the task two times - using while and repeat loops.

The resulting example:

while loop. product = 667418624
repeat loop. product = 667418624


[Program name: task-02-sum-while.pas]

{0.3 points} Task 3, sum and while:

To do: 5 real numbers are entered. The program should output their sum only once, just before the end of the program. Make the task two times - using while and repeat loops.

The resulting example:

please enter 5 numbers:
>>> 4.2  >>> 7.1  >>> 3.1  >>> 2.5  >>> 8.6
sum = 25.5


[Program name: task-03-sum-while.pas]

{0.3 points} Task 4, sum and while:

To do: 5 numbers are entered. The program should output their product (multiplication) only once, just before the end of the program. Make the task two times - using while and repeat loops.

The resulting example:

please enter 5 numbers:
>>> 4  >>> 7  >>> 3  >>> 2  >>> 8
multiplication = 1344


[Program name: task-04-sum-while.pas]

Вставить формулу как
Дополнительные настройки
Цвет формулы
Используйте LaTeX для набора формулы
Предпросмотр
$${}$$
Формула не набрана
Вставить 