edited by
19,212 views
56 56 votes

Consider the following C program which is supposed to compute the transpose of a given $4 \times 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 (i = 0; i < 4; ++i)
        for (j = 0; j < 4; ++j)
            printf ("%d", M[i][j]);
}
  1. for(j = 0; j < 4; ++j){
         t = M[i][j];
         M[i][j] = M[j][i];
         M[j][i] = t;
    }
    
  2. for(j = 0; j < 4; ++j){
         M[i][j] = t;
         t = M[j][i];
         M[j][i] = M[i][j];
    }
    
  3. for(j = i; j < 4; ++j){
         t = M[i][j];
         M[i][j] = M[j][i];
         M[j][i] = t;
    }
    
  4. for(j = i; j < 4; ++j){
         M[i][j] = t;
         t = M[j][i];
         M[j][i] = M[i][j];
    }

6 Answers

Best answer
89 89 votes

Option C:

look at the initial value of $j$, if $j$ starts with $0$, then double for loop will swap  $ M[i][j]$ with $M[j][i]$ and also $M[j][i]$ and $M[i][j]$ so the matrix $M$ will remain unchanged, so to avoid this double swapping we need to initialize $j = i$ and swap only upper triangular matrix with lower triangular matrix.

for(j = i; j < 4; ++j){
 // code for swapping M[i][j] with M[j][i]
     t = M[i][j];
     M[i][j] = M[j][i];
     M[j][i] = t;
}
edited by
18 18 votes

When swapping, this is the correct assignment for the very first location

t = M[i][j];

and not this

M[i][j] = t;

We want to save our first value in a temporary variable; and not overwrite our first value by the temporary variable.

So, Options B and D are eliminated straightaway.

 

Now, if column 0 and row 0 are already swapped, we don't wanna swap it again.

So, j shouldn't start from 0 every time. It should start with value == i.

So, Option C

2 2 votes

Let me help you solve this quesion in a faster way and take around 1min 
so option B & D are directly eliminated as they are not storing intial value which will make difficulty to restore old value  of array and assign garbage values.

  • B for(j = 0; j < 4; ++j){
         M[i][j] = t;//this will assign garbage value so B &d Not possible
         t = M[j][i];
         M[j][i] = M[i][j];
    }

Now look  at option A & B 
take a 2d array

 

12
34

Now apply the code  given in A option you will see you get the original arrray 
and we wanted transpose  so A worng 
SO C is  the answer

 

 

1 1 vote

If (j) starts at 0, each pair is swapped twice (mean same element swap again and again) , returning them to their original positions; therefore, j must start at i to swap each pair only once.

So -  for(j=i;  j<4; j++){

       }

edited by
Answer:
Position:
Show:

Related questions

50 50 votes
5 answers 5 answers
17.6k
17.6k views
Ishrat Jahan asked Nov 1, 2014
17,560 views
Let $x$ be an integer which can take a value of $0$ or $1$. The statementif (x == 0) x = 1; else x = 0;is equivalent to which one of the following ?$x = 1 + x;$$x = 1 - ...
50 50 votes
3 answers 3 answers
15.9k
15.9k views
Ishrat Jahan asked Nov 2, 2014
15,850 views
Consider the following C program:#include <stdio.h typedef struct { char *a; char *b; } t; void f1 (t s); void f2 (t *p); main() { static t s = {"A", "B"}; printf ("%s %s...
35 35 votes
3 answers 3 answers
11.7k
11.7k views
Ishrat Jahan asked Nov 2, 2014
11,710 views
Choose the correct option to fill the $?1$ and $?2$ so that the program prints an input string in reverse order. Assume that the input string is terminated by a new line ...
60 60 votes
3 answers 3 answers
15.7k
15.7k views
Ishrat Jahan asked Nov 2, 2014
15,704 views
What is the output of the following program?#include<stdio.h int funcf (int x); int funcg (int y); main () { int x = 5, y = 10, count; for (count = 1; count <= 2; ++count...