1k views
1. Consider the following Pascal function where $A$ and $B$ are non-zero positive integers. What is the value of $GET(3, 2)$?
function GET(A,B:integer): integer;
begin
if B=0 then
GET:= 1
else if A < B then
GET:= 0
else
GET:= GET(A-1, B) + GET(A-1, B-1)
end;
2. The Pascal procedure given for computing the transpose of an $N \times N, (N>1)$ matrix $A$ of integers has an error. Find the error and correct it. Assume that the following declaration are made in the main program
const
MAXSIZE=20;
type
INTARR=array [1..MAXSIZE,1..MAXSIZE] of integer;
Procedure TRANSPOSE (var A: INTARR; N : integer);
var
I, J, TMP: integer;
begin
for I:=1 to N – 1 do
for J:=1 to N do
begin
TMP:= A[I, J];
A[I, J]:= A[J, I];
A[J, I]:= TMP
end
end;


edited | 1k views

1. =3
2.
 begin
for I:=2 to N do
for J:=1 to ( I-1) do
begin
TMP:= A[I, J];
A[I, J]:= A[J, I];
A[J, I]:= TMP
end

Should be the condition...

by
edited by
+14

one simple solution :

for(i=1; i<n ;++i)

for(j=i; j<n ;++j)

0
@papesh your option B solution is nice
+1

@Anu007 Sir, won't it be "<=" ?

0

@MiNiPanda,  i too feel the same. Because if i<n, then we are not swapping the last row elements..

0
No need, it should be i<n and j<n because by the time you finish n-1 rows, you would be done swapping. Try taking a 3 by 3 matrix and run through the code, you will notice the last row elements 31 and 32 don't need swapping as they are already swapped when you were performing swap on earlier rows 1,3<->3,1 & 23<->32.

Plz refer below img.

Q.B:->Error is there bcz we are swapping content of cells of matrix two times whose overall effect is Nothing . Correction is only J=I+1 instead of J=1 .

By doing above change in code we are swapping only Upper triangular part with Lower triangular which is nothing but Transpose of matrix.

Everything else is All is Well ;)

0
nice solution
0

@Rajesh Pradhan i think the outer loop condition is for i=1 to n because if we use for i=1 to n-1, then we are leaving the last row without swapping.