GATE 2014 CS (CS3) – Question 20
A is an n × n square matrix. What does this pseudocode output?
C = 100;
for i = 1 to n do
for j = 1 to n do {
Temp = A[i][j] + C;
A[i][j] = A[j][i];
A[j][i] = Temp - C;
}
for i = 1 to n do
for j = 1 to n do
output(A[i][j]);
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (A) The matrix A itself
Explanation
Each inner body swaps A[i][j]andA[j][i]; adding then subtracting Ccancels. Every off-diagonal pair is processed once as(i,j)and again as(j,i), restoring its original values. Diagonal entries stay unchanged. Thus the output is **Aitself,A**, assuming arithmetic does not overflow.