The GATE Grind

GATE 2014 CS (CS3) – Question 20

Programming and Data Structures · Arrays · 1 mark · Multiple choice

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

  1. The matrix A itself
  2. The transpose of A
  3. A with 100 added to upper diagonal elements and subtracted from lower diagonal elements
  4. None of the above

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.