274 views

What does the following program output?

program module (input, output);
var
a:array [1...5] of integer;
i, j: integer;
procedure unknown (var b: integer, var c: integer);
var
i:integer;
begin
for i := 1 to 5 do a[i] := i;
b:= 0; c := 0
for i := 1 to 5 do write (a[i]);
writeln();
a:=11; a:=11;
for i:=1 to 5 do a [i] := sqr(i);
writeln(c,b); b := 5; c := 6;
end;
begin
i:=1; j:=3; unknown (a[i], a[j]);
for i:=1 to 5 do write (a[i]);
end;

| 274 views

We can assume that the code is using pass-by-reference or else there is no use in the parameters $b,c.$

program module (input, output);
var
a:array [1...5] of integer;
i, j: integer;
procedure unknown (var b: integer, var c: integer);
//pass by reference; b and a have same address
//and c and a have same address
var
i:integer;
begin
for i := 1 to 5 do a[i] := i;
b:= 0; c := 0
for i := 1 to 5 do write (a[i]); // prints 1, 2, 3, 4, 5
writeln();
a:=11; a:=11;
for i:=1 to 5 do a [i] := sqr(i);
writeln(c,b); // prints 121, 121
b := 5; c := 6;
end;
begin
i:=1; j:=3; unknown (a[i], a[j]); //pass by reference
for i:=1 to 5 do write (a[i]); //prints 5, 4, 6, 16, 25
end;

by Veteran (425k points)
0
how did we got values of b =121 and c =121?
+1 vote

Module is a predefined array and globally decleared

Say values of a=7

a=8

a=14

a=16

a=9

Now,

At begin i=1,j=3

Now, calling procedure unknown where b=8,c=16

a=1,a=2,a=3,a=4,a=5 // As here no array declared, this array value will change global array value

b,c is assigned as 0.

a=11,a=11

a=11,a=2,a=11,a=4,a=5

Next line all are squaring

a=121,a=4,a=121,a=16,a=25

b is overwrite with value 5, c with value 6

Now, it is returning to main b=5,c=6

a=5,a=6 //main() have no array defined, so changing global array value

Global array now containing 121,5,121,6,25

In main it is printing 121,5,121,6,25

by Veteran (117k points)
edited by
0
See the question now..