one simple solution :

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

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

The Gateway to Computer Science Excellence

+11 votes

- 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;

- 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;

+10 votes

Best answer

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.

+17 votes

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

@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.

52,345 questions

60,497 answers

201,859 comments

95,315 users