7,107 views
0 votes
0 votes

Let A be a square matrix of size n x n. Consider the following program. What is the expected output?

C = 100
for(i=0; i<n; i++)
    for (j=0; j<n; j++)
    {
         Temp = A[i][j] + C
        A[i][j] = A[j][i]
        A[j][i] = Temp - C
    }
    
for(i=0; i<n; i++)
    for (j=0; j<n; j++)
        printf(%d, A[i][j]);

A) Transpose of matrix A

B) The matrix A itself

C) Adding 100 to the upper diagonal elements and subtracting 100 from diagonal elements of A   

D) None

My answer is A) Transpose of matrix. But the answer says option B.

1 Answer

Best answer
2 votes
2 votes

B is the correct answer. It will be swap two times.

For Ex: i = 0, j  = 1, There will be one time swap of A[0][1], A[1][0].

For i = 1, j = 0, Again there will be swap between A[1][0], A[0][1]. This swap will cancel the effect of previous swap.

You can check here also.
https://ideone.com/yFpNP0

selected by

Related questions