Two real square matrices of order 10 and 20 are given. In the rows with a negative element on the main diagonal, find the product of all elements.

If you organize the input matrix manually, everything works. With randomize does not always work, often gives an error eitcode = 21. How to fix it?

Const n = 20; Type matr = array [1..n, 1..n] Of integer; vek = array[1..n] Of LongInt; Var A,B: matr; VA,VB: vek; Procedure wwod(Var A: matr; k,p: byte); Var i,j: byte; Begin Writeln('Ввод матрицы ',k,'-го порядка'); randomize; For i:= 1 To k Do Begin For j:= 1 To p Do Begin A[i,j] := random(10)-4; Write(A[i,j]:2,' '); End; readln; End; End; Procedure proizv(Var A: matr; k,p: byte; Var VA: vek); Var i,j: byte; v,f: integer; Begin For i:= 1 To k Do Begin f := j; For j:=1 To p Do If i=j Then If A[i,j]<0 Then Begin Begin v := 1; For f:=1 To p Do v := v*A[i,f] End; Writeln('Произведение элементов ', i, ' строки - ',v); End Else Begin Writeln('На главной диагонали находится положительный элемент'); break; End; End; Readln; End; Begin wwod(A, 10, 10); proizv(A,10,10,VA); wwod(B, 20, 20); proizv(B,20,20,VB); End. 
  • one
    format the code and remove the blank lines - andrybak
  • I have no errors. I replaced ReadLn from the first procedure with WriteLn, and from the second procedure I deleted ReadLn and wrote it before the last End. Used compilers Turbo Pascal and Pascal ABC.NET - works fine both there and there. If there is an error, then at what stage does it occur? - DelphiM0ZG
  • free pascal and in short, I understood what the error is, it does not multiply the string as specified in the loop but from this element to the end of the matrix. Please check if the string is multiplied correctly and if the question works: where to download the turbopascal for win7 64bit? - lybntras
  • Turbo Pascal is already old, it probably is not for Win7 64. - DelphiM0ZG
  • all the same, and in Turbo Pascal writes an unreal product. Where is the mistake? - lybntras

1 answer 1

readln in the wwod procedure wwod not needed

randomize better to put in the body of the program


Randomize better not to Randomize before each use of random , since in this case the pseudo-random number generator "resets" its state using the current time as a seed . If this happens in a short period of time, and the system time does not have time to change much, then random will issue similar sequences.

  • so visually worse and still a mistake. - lybntras
  • @rentscan updated the answer - andrybak