Consider the following C program which is supposed to compute the transpose of a given 4 x 4 matrix M. Note that, there is an X in the program which indicates some missing statements. Choose the correct option to replace X in the program.
#include<stdio.h> #define ROW 4 #define COL 4 int M[ROW][COL] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
main() { int i, j, t;
for (i = 0; i < 4; ++i)
{
X
}
for (1 = 0; i < 4; ++i)
for (j = 0; j < 4; ++j)
printf ( "%d" , M[i][j]);
} |
A) |
for(j = 0; j < 4; ++j){ t = M[i][j]; M[i][j] = M[j][i]; M[j][i] = t; }
|
B) |
for(j = 0; j < 4; ++j){ M[i][j] = t; t = M[j][i]; M[j][i] = M[i][j]; }
|
C)
|
for(j = i; j < 4; ++j){ t = M[i][j]; M[i][j] = M[j][i]; M[j][i] = t; }
|
D) |
for(j = i; j < 4; ++j){ M[i][j] = t; t = M[j][i]; M[j][i] = M[i][j]; }
|
(A) A
(B) B
(C) C
(D) D
Answer: (C)
Explanation:
- To compute transpose j needs to be started with i,so A and B are WRONG
- In D, given statement is wrong as temporary variable t needs to be assigned some value and NOT vice versa
M[i][j] = t;
So the answer is C
Check out the correct option C at Solution: https://ide.geeksforgeeks.org/r7wbP6