0
votes

I am a 1st grade student and I got a task to complete, but cannot understand how to avoid errors that I got. Could you please help me? Here is my code:

PROGRAM LAB5MAS (INPUT,OUTPUT);
CONST
      n=5;
      m=6;
VAR
A: array[1..n,1..m] of Integer;
R: array[m-1] of Integer;
i,j: Integer;
max: Integer;
BEGIN
  Randomize;
  for i:=1 to n do
    for j:=1 to m do
      a[i,j]:=random(10);
  Writeln('a=,');
  for i:=1 to n do
  BEGIN
    for j:=1 to m do
      write(a[i,j]:4);
    writeln;
  END;
  for i:=1 to n do
    for j:=1 to m-1 do
      R[i,j]:=abs(A[i,j]-A[i,j+1]);
END.

for i:=1 to n do
BEGIN
  max:=R[i,1];
  for j:=1 to m-1 do
  if R[i,j] > max then
  max:=R[i,j];
  Writeln(max);
END;
for i:=1 to n do
BEGIN
  for j:=1 to m do
  write(a[i,j]:4);
  writeln;
END.

And the errors that I got:

Compiling C:\Users\Nadia\Desktop\qqwww\laba.pas
laba.pas(7,17) Error: Error in type definition
laba.pas(24,7) Error: Illegal qualifier
laba.pas(26,1) Fatal: There were 2 errors compiling module, stopping
Fatal: Compilation aborted

How to fix this? And how to compute the biggest value of subtraction between nearest elements in array A(5,6) and write this result to array B? Thanks in advance!

1
LAST TIME i PROGRAMMED IN pASCAL WAS IN THE HIGH SCHOOL.... BUT IT SEEMS THAT R IS DEFINED AS 1 DIMENSIN ARRAY WHILE YOU ARE USING AS MATRIX R[I,J] (ALSO THE DEFINITION OF R SHOULD BE ARRAY [1..M-1] OF INTEGER; - Alex Lop.
Pascal does not take a simple subscript for defining arrays; you must provide the entire range of subscripts that are expected to be valid. Your definition of R is incorrect. Look at how you defined the valid subscripts for A in the line above. Furthermore, you are trying to use R as a two-dimensional array, when it has not been defined as such. Go back and desk-check your code. - Jeff Zeitlin
You have two END. (END ending with a period). That's going to be a syntax error. The syntactical end of your Pascal program is at the end of the first END.. - lurker
Also note that it looks as if the first part, up to the first END. should be a separate procedure. Which Pascal dialect is this? - Rudy Velthuis

1 Answers

2
votes

R: array[m-1] of Integer;

This is wrong, you need to specify a range of values, e.g. "R: array[0..m-1] of Integer;"

R[i,j]:=abs(A[i,j]-A[i,j+1]);

You are trying to treat a one dimesntional array as a multidimensional array.

You also seem to have screwed up your "END"s, I think what you have in that regard is syntatically correct but almost certainly not what you intended (iirc everything after the first "END." is ignored)