Hello!
I just started to learn Pascal. The case is very interesting. But here is my task: I need to create a simple program for finding the maximum and minimum values of the N entered numbers.
It seems to have done everything right. And the compiler does not issue errors. But the program does not work quite right.
{Программа для определения максимального и минимального значения из N введенных чисел} program max_i_min_iz_N; uses crt; var N,min,max,a,i,m:integer; BEGIN clrscr; repeat repeat write('Введите любое положительное число: '); readln(N); writeln(); until(N>0); write('Введите ',N,' чисел(-а) через пробел: '); i:=2; read(a); max:=a; min:=a; repeat read(a); if(a>max)then max:=a else max:=max; if(a<min)then min:=a else min:=min; i:=i+1; until(i>N); writeln(); writeln('max=',max); writeln('min=',min); writeln(); writeln('Для продолжения программы нажмите цифру 1'); writeln('Для завершения программы нажмите цифру 2'); writeln(); readln(m); writeln(); until(m=2); END.
In general, the program works correctly in all cases except one ...
If here
write('Введите ',N,' чисел(-а) через пробел: ');
the user enters the value of the variable N = 1, then the program still waits for the next variable to be entered for comparison.
I understand that this happens because I have the code spelled out twice to read the variable a , but I did not succeed in another way.
If you do this:
i:=1; max:=a; min:=a; repeat read(a); if(a>max)then max:=a else max:=max; if(a<min)then min:=a else min:=min; i:=i+1; until(i>N);
Then I assign the variable i to 1 and it is not necessary to enter additional values, in this plan the cycle works correctly. But from the entered values it finds only the maximum number. Assigns the minimum to 0. It is interesting that if the program is not closed, but continued further, then the program starts to find the minimum and maximum number correctly. But not from the values just entered, but from all the values that were entered.
If you do this:
i:=1; read(a); max:=a; min:=a; repeat if(a>max)then max:=a else max:=max; if(a<min)then min:=a else min:=min; i:=i+1; until(i>N);
Here, the program assigns the first value to both max and min, and does not compare the following values. In this case, the cycle to repeat the program does not work. The program just closes and that's it. In principle, it is clear why it does not compare the values, because in the next cycle only the same value of the variable a, which was entered first, participates. But why then the program closes itself, without reading the variable m?
In general, I abandoned the idea of using only one reading of the variable N. But how can the program, if I enter the number 1 for the variable N, make the program work correctly, only then? Maybe just add for 1 case construction?
I also don't really like i: = 2; Not beautiful somehow or something. It’s more usual to see i: = 1; But if I assign the variable i the value 1, then the program asks for comparison of the entered values one more.
But in general, I bang my eyes and did not see what I was doing wrong.
Do not take the trouble, tell me, please, where I am mistaken.