Pascal beginner program - pascal

Program q2;
Var input: integer ;
Var repeatvar: char ;
Procedure display(input : integer) ;
Begin
If (input = 9) then
Write ('********************************************************') ;
Write ('9! = 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 362880 ');
Write ('********************************************************') ;
If (input = 8) then
Write ('********************************************************') ;
Write ('8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 40320 ');
Write ('********************************************************') ;
If (input = 7) then
Write ('********************************************************') ;
Write ('7! = 7 x 6 x 5 x 4 x 3 x 2 x 1 = 5040 ');
Write ('********************************************************') ;
If (input = 6) then
Write ('********************************************************') ;
Write ('6! = 6 x 5 x 4 x 3 x 2 x 1 = 720 ');
Write ('********************************************************') ;
If (input = 5) then
Write ('********************************************************') ;
Write ('5! = 5 x 4 x 3 x 2 x 1 = 120 ');
Write ('********************************************************') ;
If (input = 4) then
Write ('********************************************************') ;
Write ('4! = 4 x 3 x 2 x 1 = 24 ');
Write ('********************************************************') ;
If (input = 3) then
Write ('********************************************************') ;
Write ('3! = 3 x 2 x 1 = 6 ');
Write ('********************************************************') ;
If (input = 2) then
Write ('********************************************************') ;
Write ('2! = 2 x 1 = 2 ');
Write ('********************************************************') ;
If (input = 1) then
Write ('********************************************************') ;
Write ('1! = 1 = 1 ');
Write ('********************************************************') ;
If (input = 0) then
Write ('********************************************************') ;
Write ('0! = 1 = 1 ');
Write ('********************************************************') ;
End ;
Begin
While (repeatvar = 'y') do
Begin
Write('Enter an integer between 0 and 9 or -1 to quit');
readln(input);
If (input = -1) then
begin
Write('Sentinel value - Exiting program !');
Readln;
end
Else
display(input);
End
End.
This is the warning I get after compiling it .
Free Pascal Compiler version 2.4.0 [2010/05/18] for x86_64
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for x86-64
Compiling q2.pas
q2.pas(61,8) Warning: Variable "repeatvar" does not seem to be initialized
Linking q2
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
78 lines compiled, 0.2 sec
1 warning(s) issued
I am typing in q2 but cannot see any output , it's not doing anything. I have tried a number of things , also I am totally new to Pascal , and fpc . Can anyone tell me what's the problem ?
In the part of the repeatvar , i wanted to implement this :
i wanted to implement this c++ code :
do
{
cout << "Enter an integer between 0 and 9 or -1 to quit => " ;
cin >> input ; // get number
if(input == -1)
{
cout << "Sentinel value - Exiting program !" << endl;
exit ;
system("pause");
return 0;
}
else
{
// display function
display(input) ;
}
cout << endl;
cout << " Press y to repeat , any other key and enter to terminate: " ;
cin >> repeat ;
if(repeat != 'y')
exit;
}while( repeat == 'y');

Because the repeatvar is never initialized, your program never enters the while loop, because there is no way for repeatvar to be 'y' to begin with.

You forgot to initialize the variable
repeatvar := 'y'
insert the above line before the lines
While (repeatvar = 'y') do
Begin
...
End

i hope that the correction of code
program Project1;
Var input: integer ;
Var repeatvar: char ;
Procedure display(input : integer) ;
Begin
If (input = 9) then
begin
Writeln ('********************************************************') ;
Writeln ('9! = 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 362880 ');
Writeln ('********************************************************') ;
end;
If (input = 8) then
begin
Writeln ('********************************************************') ;
Writeln ('8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 40320 ');
Writeln ('********************************************************') ;
end;
If (input = 7) then
begin
Writeln ('********************************************************') ;
Writeln ('7! = 7 x 6 x 5 x 4 x 3 x 2 x 1 = 5040 ');
Writeln ('********************************************************') ;
end;
If (input = 6) then
begin
Writeln ('********************************************************') ;
Writeln ('6! = 6 x 5 x 4 x 3 x 2 x 1 = 720 ');
Writeln ('********************************************************') ;
end;
If (input = 5) then
begin
Writeln ('********************************************************') ;
Writeln ('5! = 5 x 4 x 3 x 2 x 1 = 120 ');
Writeln ('********************************************************') ;
end;
If (input = 4) then
begin
Writeln ('********************************************************') ;
Writeln ('4! = 4 x 3 x 2 x 1 = 24 ');
Writeln ('********************************************************') ;
end;
If (input = 3) then
begin
Writeln ('********************************************************') ;
Writeln ('3! = 3 x 2 x 1 = 6 ');
Writeln ('********************************************************') ;
end;
If (input = 2) then
begin
Writeln ('********************************************************') ;
Writeln ('2! = 2 x 1 = 2 ');
Writeln ('********************************************************') ;
end;
If (input = 1) then
begin
Writeln ('********************************************************') ;
Writeln ('1! = 1 = 1 ');
Writeln ('********************************************************') ;
end;
If (input = 0) then
begin
Writeln ('********************************************************') ;
Writeln ('0! = 1 = 1 ');
Writeln ('********************************************************') ;
end;
End ;
Begin
repeat
writeln('enter number between 1-9 or -1 to quit: ');
readln(input);
display(input);
until input=-1;
End.

Related

Enumerated type and registers

I'm having difficulties with some code, basically I need to fill 3 vectors that work as data storage, where every vector position correspond to a specific province, I'm sorry about the variable names being in spanish.
CONST
F = 24;
TYPE
T_vector = array [1..F] of Real;
subrango = 1..F;
registro_infectados = record
name, email, date_of_birth,estado: string[30];
street, city, provincia: string[30];
street_number, pc,: integer;
id,phone_number:real;
end;
T_archivo_infectados = FILE OF registro_infectados;
PROCEDURE INFECTADOS_POR_PROVINCIA(VAR arch:T_archivo_infectados; VAR vect_activos:t_vector; VAR vect_fallecidos: t_vector; VAR vect_recuperados:t_vector); // This will read every register, count the "reg.state" and sum 1 to the corresponding vector.
var
lim:subrango;
REG:registro_infectados;
Begin
Inicializar_Vector(vect_activos, vect_fallecidos, vect_recuperados, lim); //This iniatiate the vectors.
RESET(ARCH);
Begin
WHILE NOT(EOF(ARCH)) DO
BEGIN
READ(ARCH, REG);
case reg.estado of
'activo':begin
case reg.provincia of
'Entre Rios': vect_activos[1]:= vect_activos[1] + 1 ;
'Buenos Aires':vect_activos[2]:= vect_activos[2] + 1;
'Cordoba': vect_activos[3]:= vect_activos[3] + 1;
'Corrientes': vect_activos[4]:= vect_activos[4] + 1 ;
'Misiones': vect_activos[5]:= vect_activos[5] + 1 ;
'Formosa': vect_activos[6]:= vect_activos[6] + 1 ;
'Chaco': vect_activos[7]:= vect_activos[7] + 1 ;
'Jujuy': vect_activos[8]:= vect_activos[8] + 1 ;
'Salta': vect_activos[9]:= vect_activos[9] + 1 ;
'Tucuman': vect_activos[10]:= vect_activos[10] + 1 ;
'La Rioja': vect_activos[11]:= vect_activos[11] + 1 ;
'Santiago del Estero': vect_activos[12]:= vect_activos[12] + 1 ;
'Catamarca': vect_activos[13]:= vect_activos[13] + 1 ;
'La Pampa': vect_activos[14]:= vect_activos[14] + 1 ;
'Santa Fe': vect_activos[15]:= vect_activos[15] + 1 ;
'Mendoza': vect_activos[16]:= vect_activos[16] + 1 ;
'San Juan': vect_activos[17]:= vect_activos[17] + 1 ;
'Rio Negro': vect_activos[18]:= vect_activos[18] + 1 ;
'Tierra del Fuego': vect_activos[19]:= vect_activos[19] + 1 ;
'Santa Cruz': vect_activos[20]:= vect_activos[20] + 1 ;
'Chubut': vect_activos[21]:= vect_activos[21] + 1 ;
'San Luis': vect_activos[22]:= vect_activos[22] + 1 ;
'Neuquen': vect_activos[23]:= vect_activos[23] + 1 ;
'CABA': vect_activos[24] := vect_activos[24] + 1;
end;
end;
'fallecido': begin
case reg.provincia of
'Entre Rios': vect_fallecidos[1]:= vect_fallecidos[1] + 1 ;
'Buenos Aires':vect_fallecidos[2]:= vect_fallecidos[2] + 1;
'Cordoba': vect_fallecidos[3]:= vect_fallecidos[3] + 1;
'Corrientes': vect_fallecidos[4]:= vect_fallecidos[4] + 1 ;
'Misiones': vect_fallecidos[5]:= vect_fallecidos[5] + 1 ;
'Formosa': vect_fallecidos[6]:= vect_fallecidos[6] + 1 ;
'Chaco': vect_fallecidos[7]:= vect_fallecidos[7] + 1 ;
'Jujuy': vect_fallecidos[8]:= vect_fallecidos[8] + 1 ;
'Salta': vect_fallecidos[9]:= vect_fallecidos[9] + 1 ;
'Tucuman': vect_fallecidos[10]:= vect_fallecidos[10] + 1 ;
'La Rioja': vect_fallecidos[11]:= vect_fallecidos[11] + 1 ;
'Santiago del Estero': vect_fallecidos[12]:= vect_fallecidos[12] + 1 ;
'Catamarca': vect_fallecidos[13]:= vect_fallecidos[13] + 1 ;
'La Pampa': vect_fallecidos[14]:= vect_fallecidos[14] + 1 ;
'Santa Fe': vect_fallecidos[15]:= vect_fallecidos[15] + 1 ;
'Mendoza': vect_fallecidos[16]:= vect_fallecidos[16] + 1 ;
'San Juan': vect_fallecidos[17]:= vect_fallecidos[17] + 1 ;
'Rio Negro': vect_fallecidos[18]:= vect_fallecidos[18] + 1 ;
'Tierra del Fuego': vect_fallecidos[19]:= vect_fallecidos[19] + 1 ;
'Santa Cruz': vect_fallecidos[20]:= vect_fallecidos[20] + 1 ;
'Chubut': vect_fallecidos[21]:= vect_fallecidos[21] + 1 ;
'San Luis': vect_fallecidos[22]:= vect_fallecidos[22] + 1 ;
'Neuquen': vect_fallecidos[23]:= vect_fallecidos[23] + 1 ;
'CABA': vect_fallecidos[24] := vect_fallecidos[24] + 1;
end;
end;
'recuperado': begin
case reg.provincia of
'Entre Rios': vect_recuperados[1]:= vect_recuperados[1] + 1 ;
'Buenos Aires':vect_recuperados[2]:= vect_recuperados[2] + 1;
'Cordoba': vect_recuperados[3]:= vect_recuperados[3] + 1;
'Corrientes': vect_recuperados[4]:= vect_recuperados[4] + 1 ;
'Misiones': vect_recuperados[5]:= vect_recuperados[5] + 1 ;
'Formosa': vect_recuperados[6]:= vect_recuperados[6] + 1 ;
'Chaco': vect_recuperados[7]:= vect_recuperados[7] + 1 ;
'Jujuy': vect_recuperados[8]:= vect_recuperados[8] + 1 ;
'Salta': vect_recuperados[9]:= vect_recuperados[9] + 1 ;
'Tucuman': vect_recuperados[10]:= vect_recuperados[10] + 1 ;
'La Rioja': vect_recuperados[11]:= vect_recuperados[11] + 1 ;
'Santiago del Estero': vect_recuperados[12]:= vect_recuperados[12] + 1 ;
'Catamarca': vect_recuperados[13]:= vect_recuperados[13] + 1 ;
'La Pampa': vect_recuperados[14]:= vect_recuperados[14] + 1 ;
'Santa Fe': vect_recuperados[15]:= vect_recuperados[15] + 1 ;
'Mendoza': vect_recuperados[16]:= vect_recuperados[16] + 1 ;
'San Juan': vect_recuperados[17]:= vect_recuperados[17] + 1 ;
'Rio Negro': vect_recuperados[18]:= vect_recuperados[18] + 1 ;
'Tierra del Fuego': vect_recuperados[19]:= vect_recuperados[19] + 1 ;
'Santa Cruz': vect_recuperados[20]:= vect_recuperados[20] + 1 ;
'Chubut': vect_recuperados[21]:= vect_recuperados[21] + 1 ;
'San Luis': vect_recuperados[22]:= vect_recuperados[22] + 1 ;
'Neuquen': vect_recuperados[23]:= vect_recuperados[23] + 1 ;
'CABA': vect_recuperados[24] := vect_recuperados[24] + 1;
end;
end;
end;
end;
end;
End;
The code is working just fine, but I'm afraid is too long and I can't come up with a solution to make it shorter.
Whats the actual problem,as We dont have all the functions
All your parameters are declared as variables, which means that, all new values will be in the same varables, and as you know procedures do not return* values.
Example:
procedure INFECTED_BY_PROVINCE(VAR X : Integer);
begin
X := 12345; // will set the incoming variable to that value
end;
VAR
TestVar : Integer;
begin
TestVar := 0; /// Set X to 0
INFECTED_BY_PROVINCE(TestVar);
writeln(TestVar); // Output/TestVar will be 12345;
end.
Instead of repeating the case REG.provincia of block for each REG.estado, do it only once and assign a variable, e.g. indx: integer a value that corresponds to the province:
var
lim: subrango;
REG: registro_infectados;
indx: integer; // new variable
case REG.provincia of
'Entre Rios': indx := 1;
'Buenos Aires': indx := 2;
'Cordoba': indx := 3;
//
//... fill in all other provinces
//
'CABA': indx := 24;
end;
Now you can simplify the assignment to the vectors for the three states (REG.estado) using the province indx to just a few lines:
case REG.estado of
'activo': inc(vect_activos[indx]);
'fallecido': inc(vect_fallecidos[indx]);
'recuperado': inc(vect_recuperados[indx]);
end;
As you see, using inc() also shortens the assignment.

Pascal - Sum of odd numbers between 0 and X

I've beeng having some trouble with this code... I need to create an algorithm which makes the user input a number (X), and then the program calculates the sum of all the odd numbers below (x).
This what I've tried so far, but can't really wrap my head around the logic behind it:
Program odd_numbers;
Var
Num, Limite, Soma: integer;
Begin;
Soma := 0;
Writeln('Choose a limit:');
Readln(Limite);
While (Limite / 2 > 0) do
Begin;
Soma := ((Num < Limite) mod 2 > 0);
Writeln('The sum of odd numbers from 0 to ', Limite, ' é ', Soma);
End;
if (Limite mod 2 = 0) then
Begin;
Soma := ((Num < Limite) mod 2 = 0);
Writeln('The sum of odd numbers from 0 to ', Limite, ' é ', Soma);
End;
End.
*PS: Been writing the code with variables in Portuguese, so don't mind the variables appearing weird to understand. *
I see that everyone is happily looping, but this is not necessary. This is a simple arithmetic sequence, and the sum can be calculated without a loop.
Just think of the following:
1 + 3 = 2 * (1 + 3) / 2 = 2 * 2 = 4 ; limits 3 and 4
1 + 3 + 5 = 3 * (1 + 5) / 2 = 3 * 3 = 9 ; limits 5 and 6
1 + 3 + 5 + 7 = 4 * (1 + 7) / 2 = 4 * 4 = 16 ; limits 7 and 8
1 + 3 + 5 + 7 + 9 = 5 * (1 + 9) / 2 = 5 * 5 = 25 ; limits 9 and 10
1 + 3 + 5 + 7 + 9 + 11 = 6 * (1 + 11) / 2 = 6 * 6 = 36 ; limits 11 and 12
But not only that, you'll see that it is in fact always a perfect square: Sqr((n+1) div 2).
So just calculate:
program odd_numbers;
var
Num, Limite, Soma: Integer;
begin
Write('Choose a limit: ');
Readln(Limite);
Num := (Limite + 1) div 2;
Soma := Num * Num;
Writeln('The sum of odd numbers from 0 to ', Limite, ' is ', Soma);
end.
Looks a little simpler than what the others propose.
The loop While (Limite / 2 > 0) do ... uses real arithmetic and not integer arithmetic. I guess you mean While (Limite div 2 > 0) do ... And you should change Limite in the loop otherwise you get stuck because the exit condition can never be reached.
After you have asked the user to enter a number, Limite, you need to keep that unchanged, because you need it in the final message. You also need a loop where you go through all numbers from Limite towards 0.
You started with a while loop which is ok, you are just missing the loop control variable. That is a variable that eventually gets a terminating value which then stops the loop. Use for example the Num variable you already have declared. You can use the same variable to investigate the numbers between user input and 0, for being odd values.
num := limite-1; // give num a start value based on user input (-1 because of "... numbers below (x)")
while num > 0 do // stop the loop when 0 is reached
begin
// here you investigate if `num` is a odd number (e.g. using `mod` operator or
// possibly your pascal has a built in `function Odd(value: integer): boolean;`)
// and add it to `Soma` if it is
num := num - 1;// decrement num at every iteration
end;
Finally you need to consider changes to the above, to handle negative input from the user.
To test if an integer is an odd value, you could use following function:
function IsOdd( value : Integer) : Boolean;
begin
IsOdd := (value mod 2) <> 0;
end;
Many pascal compilers have a built-in function called Odd(), which you could use.
A while loop works well to solve this problem. If you start with lowest odd number above zero, i.e. one and continue upwards so long we do not exceed the limit value we have a simple start:
function GetOddSumBelowX( X : Integer) : Integer;
var
i,sum: Integer;
begin
i := 1; // Start with first odd number
sum := 0;
while (i < X) do begin // as long as i less than X, loop
if IsOdd(i) then begin
sum := sum + i; // add to sum
end;
i := i + 1; // Increment i
end;
GetOddSumBelowX := sum;
end;
Now, that was simple enough. Next step to simplify the loop is to increment the i variable by two instead, just to jump between all odd numbers:
function GetOddSumBelowX( X : Integer) : Integer;
var
i,sum: Integer;
begin
i := 1; // Start with first odd number
sum := 0;
while (i < X) do begin // as long as i less than X, loop
sum := sum + i; // add to sum
i := i + 2; // Increment to next odd number
end;
GetOddSumBelowX := sum;
end;

Divisibility of numbers in pascal

I want to write a pascal program that checks if particular number is divisible by 2, 3, 5, 7, 9 and 11 and whether the sum of the digits is even or odd. In the very end I want to write a statement like "This number is divisible by 5 and 9" and the sum of the numbers is even/odd. What should I do?
Use modulus:
program ModulusTest;
begin
if 8 mod 2 = 0 then
begin
write(8);
writeln(' is even');
end;
if 30 mod 5 = 0 then
begin
write(30);
writeln(' is divisible by 5');
end;
if 32 mod 5 <> 0 then
begin
write(32);
writeln(' is not divisible by 5');
end;
end.
Modulus is what remains after an integer division :)
This's my code, I separate into 2 sections :
program checkNumber;
var number : integer;
divider : string;
digit1, digit2, sum : integer;
begin
//First//
write('Number : '); readln(number);
if (number MOD 2 = 0) then divider := divider+'2, ';
if (number MOD 3 = 0) then divider := divider+'3, ';
if (number MOD 5 = 0) then divider := divider+'5, ';
if (number MOD 7 = 0) then divider := divider+'7, ';
if (number MOD 9 = 0) then divider := divider+'9, ';
if (number MOD 11 = 0) then divider := divider+'11, ';
write('This number is divisible by '); write(divider);
////////////////////////////////////////////////////////
//Second//
digit1 := number DIV 10;
digit2 := number MOD 10;
sum := digit1 + digit2;
write('and the sum of the numbers is ');
if (sum MOD 2 = 0) then write('even') else write('odd');
////////////////////////////////////////////////////////
end.
First part
You need MOD(modulus) operation to get the list of divider values:
write('Number : '); readln(number);
if (number MOD 2 = 0) then divider := divider+'2, ';
if (number MOD 3 = 0) then divider := divider+'3, '; //divider 2 3 5 7 9 11
.
.
Then save the divider into variable divider as string, and write it on monitor.
write('This number is divisible by '); write(divider);
Second part
You need to separate the digits into single variable using DIV(divide) and MOD(modulus) operation. In my code, I limit the number input for 2 digit (1 until 99):
digit1 := number DIV 10;
digit2 := number MOD 10;
sum := digit1 + digit2;
(You change the code use if..then.. function if you want input bigger number).
Then use MOD to check the number is even or odd:
if (sum MOD 2 = 0) then write('even') else write('odd');

Why is my PASCAL program not running?

Program TypeofCreditCard;
Var
AppliName: array[1..99] of String;
SSnum: array[1..99] of Integer;
GSal: array[1..99] of Integer;
TSalD: array[1..99] of Integer;
Name, CC : String;
Rep,Exp, GS,NS,Sum,TSD , YS,SSN,i, ,PofIncome : integer ;
YS, C_Amt: Real
Begin
Writeln ( 'Enter applicants who applied for a type of credit card');
Readln (Rep,Exp,GS,YS,NS,Sum,TSD,SSN,CC,PofIncome) ;
While ( Name <> ' Stop ' ) do
Begin
NS:= GS-TSD ;
Sum:= Exp + Rep ;
PofIncome:=(NS * 0.45);
Begin
If ( GS >4000) AND ( CC = 'Bronze Card' ) then
YS:= GS * 12 ;
C_Amt := YS * 0.25;
i:= i + 1;
AppliName [i]:= Name;
SSNum [i]:= SSN ;
GSal [i]:= GS ;
TSalD [i]:= TSD ;
End ;
Begin
If (GS >= 7500) AND (CC= 'Gold Card') then
YS:= GS * 12 ;
C_Amt:= YS * 0.3;
i:= i + 1 ;
AppliName [i]:= Name;
SSnum [i]:=SSN;
GSal [i]:=GS;
TSalD [i]:=TSD;
End;
Begin
If (GS>=10,000) AND ( CC = ' Platinum Card') then
YS := GS * 12;
C_Amt: = YS * 0.4;
i:= i + 1;
AppliName [i]:= Name;
SSNum [i]:= SSN;
GSal [i]:=GS;
TSalD [i]:= TSD;
End if
End if
End if
End While
End.
I am using free IDE Pascal and it is saying that I have 2 errors. If there are anymore errors or you see anything strange please inform me. The error says : Fatal:Syntax Error, ";" expected but "identifier Writeln found - Source of error- Writeln ( 'Enter applicants who applied for a type of credit card');
There's a ; missing after YS, C_Amt: Real. Also after each then should be a beginand all End if and End while at the end should all be End;

Which of 3 matrixes has the biggest element number sum (FreePacal)?

I have a program which outputs 3 (4x4) matrixes (different number, same layout)
And I must output again matrix, which element number sum are the biggest.
For example 65 is the most biggest elements number sum.
1 2 3 4 10 1 2 3 4 10 1 1 1 1 4
5 6 7 8 26 2 3 4 5 14 2 2 2 2 8
9 1 2 3 15 3 4 5 6 18 3 3 3 3 12
2 3 4 5 14 4 5 6 7 22 4 4 4 4 16
65 64 40
The program which generates 3 random matrixes:
uses
SysUtils;
var
i: integer;
x: integer;
y: integer;
matrix: array[1..4, 1..4] of integer;
begin
randomize;
for i := 1 to 3 do
begin
for x := 1 to 4 do
for y := 1 to 4 do
matrix[x, y] := random(101);
for x := 1 to 4 do
begin
for y := 1 to 4 do
write(IntToStr(matrix[x, y]), ' ');
writeln;
end;
writeln;
end;
readln;
end.
Can You help me? I would be very thankful.
Could be this way for instance:
program Project1;
uses
SysUtils;
// create a type for the matrix
type
TMatrix = array[1..4, 1..4] of Integer;
var
I: Integer;
X: Integer;
Y: Integer;
CurSum: Integer;
MaxIdx: Integer;
MaxSum: Integer;
Matrices: array[1..3] of TMatrix;
begin
// initialize random seed
Randomize;
// initialize max. sum matrix index and max. matrix sum
MaxIdx := 0;
MaxSum := 0;
// iterate to create 3 matrices
for I := 1 to 3 do
begin
// initialize sum value of this matrix to 0
CurSum := 0;
// iterate to fill the matrices with random values
for X := 1 to 4 do
for Y := 1 to 4 do
begin
// to the matrix I assign a random value to the X, Y position
Matrices[I][X, Y] := Random(101);
// add this random value to the current matrix sum value
CurSum := CurSum + Matrices[I][X, Y];
end;
// check if this matrix sum value is greater than the stored one
// and if so, then...
if CurSum > MaxSum then
begin
// store this matrix index
MaxIdx := I;
// and store this matrix sum as a max sum value
MaxSum := CurSum;
end;
// print out this matrix
for X := 1 to 4 do
begin
for Y := 1 to 4 do
Write(IntToStr(Matrices[I][X, Y]), ' ');
WriteLn;
end;
WriteLn;
end;
// print out the index of the matrix with max sum and its sum value
WriteLn;
WriteLn('The biggest matrix is the ' + IntToStr(MaxIdx) + '. one. The sum ' +
'of this matrix is ' + IntToStr(MaxSum) + '.');
WriteLn;
// and print out that matrix with max sum value
for X := 1 to 4 do
begin
for Y := 1 to 4 do
Write(IntToStr(Matrices[MaxIdx][X, Y]), ' ');
WriteLn;
end;
ReadLn;
end.

Resources