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.

  • 3
    Immediately visible oriental man. - skegg
  • 3
    Girls need to learn programming for three reasons - she plans to become the wife of an intelligent man (programming develops). - she plans to raise children well. - she is interested. - KoVadim
  • five
    Here it is worthwhile to put a wider question: does a woman have the right to engage in technology, science, art, business, in general work somewhere or she should just stay at home, prepare meals and raise children. Programming is one of the options for practicing "outside the home," if a particular woman has the ability and desire to do so. And programming and many men should not be engaged. - skegg
  • one
    I apologize wildly, I do not remember the pascal syntax (I write in C), but the algorithms are almost the same everywhere. The simplest is to start with i: = 1 and put it in an internal repeat after read (a) if (i = 1) then begin max: = a; min: = a; end; and then what you have written. In the outer repeat, write i: = 1, and read (a), min: = a and max: = a throw away. Well, of course, else in if for min and max are not needed. - avp
  • 3
    And who said that programming is a man's business? Especially considering that the first programmer was Ada Lovelace. I fully agree with @mikillskegg. You need to be able to share responsibilities, do household chores together, then the house really turns into a cozy family home that you created together, you want to return to, you want to be. And also agrees that you need to respect the interests of each other. I do not argue, many men can do great programming, but they don’t have to cut the desire of women to do it. - elenavictory

4 answers 4

The first thing that catches your eye is these lines:

  if(a>max)then max:=a else max:=max; if(a<min)then min:=a else min:=min; 

They can be accurately written shorter:

  if(a>max)then max:=a; if(a<min)then min:=a; 

The functionality is the same, but the eye is more pleasant. We now turn to the most important thing. To this cycle

 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); 

It can be written more beautifully:

 while(i <= N) do begin read(a); if(a>max)then max:=a; if(a<min)then min:=a; i:=i+1; end; 

And everything should work.

If you don't like i := 2; This can be replaced by i := 1; , but another change is needed - while(i <= N) replaced with while(i < N) .

    To prevent the program from looping, you need to replace the nested Repeat with While:

      i:=1; Write('a = '); read(a); max:=a; min:=a; While (i<N) Do // если ввести 1, цикл работать не будет Begin Write('a = '); read(a); if(a>max)then max:=a; if(a<min)then min:=a; i:=i+1; End; 

    Or write the following condition after the first nested Repeat loop:

     If (N=1) Then Break; 

    But, I would do this: change the condition on the cycle:

     repeat write('Введите любое положительное число: '); readln(N); writeln; until(N>1); 

    Immediately from the user input a positive number to require. Then i can be assigned to the i: = 1 counter.

      Another tip: do not rush to rewrite and append your code. Compilation errors are easy to find, and logical errors are much more complicated.

      And I already started to add a case and write a program through while and for.

      All cycles are interchangeable and it is better to get started with at least one at first :) such a huge program) I would write this:

       Program max_i_min_iz_N; var i, n, a, max: LongInt; begin write('Введите количество чисел: '); readln(N); max := MaxLongInt; min := -MaxLongInt; for i := 1 to N do begin write('Введите ',i,'-oе число: '); readln(a); if a > max then max := a; if a < min then min := a; end; writeln('Max = ', max); writeln('Min = ', min); end. 

      MaxLongInt is a reserved variable in FreePascal, I advise you to write on it. MaxLongInt = 2147483648 - the maximum number that gets into the LongInt FreePascal type can frighten the DOS window, compared to ABC :) but it can allocate much more memory .. In short, Pascal ABC for children :)

      • forgot at the end readln; but not the essence :) - pisarik

      @avp , thank you for the good advice! Indeed, everything ingenious is simple :) Just one line is added => and the program works as it should :) And I’ve started to add the case and write the program through the while and for. It turns out the answer lay on the surface :) Once again, thank you very much and good luck to you, @avp !

      • And to you, Elena, success and many victory . - avp
      • How do I really need these victory now :) Thanks for the wish :) - elenavictory