edited by
18,807 views
51 51 votes

Let $A$ be the square matrix of size $n \times n$. Consider the following pseudocode. What is the expected 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. Transpose of the matrix $A$
  3. Adding $100$ to the upper diagonal elements and subtracting $100$ from lower diagonal elements of $A$
  4. None of the above

4 Answers

Best answer
50 50 votes

A.
In the computation of given pseudo code for each row and column of Matrix $A$, each upper triangular element will be interchanged by its mirror image in the lower triangular and after that the same lower triangular element will be again re-interchanged by its mirror image in the upper triangular, resulting the final computed Matrix $A$ same as input Matrix $A$.

edited by
38 38 votes
Take a small matrix

1  2

3  4

now trace the iteration

i=1,j=1 //no change in matrix cz a[i,j]=a[j,i]

i=1,j=2 // resultant matrix

1  3

2 4

i=2,j=1 //resultant matrix

1  2

3 4

i=2,j=2 //no change in matrix cz a[i,j]=a[j,i]

we get the original matrix as it is So, Option A is Ans.
4 4 votes
take a small matrix
[a b]

[c d]

it will give
[a c

b+100 d]
for first row iterated

[a b + 100 -100
c d]
for second row iterated.
hence answer A.
0 0 votes

Ok so basically for such Questions just need to see operation and then loop

 

Observe :- 

 

Temp = A[ i ] [ j ] + C ;
      A [ i ] [ j ] = A [ j ] [ i ] ;
      A [ j ] [ i ] = Temp - C ;


This is nothing but standard transpose operation



which can also be remembered as :

Temp = A[ i ] [ j ] ;
      A [ i ] [ j ] = A [ j ] [ i ] ;
      A [ j ] [ i ] = Temp ;

 

Now next thing to do is to observe the loops carefully,

There are basically 3 Rules or 3 similar type loops 

other than this it is mostly unlikely to be asked in exam



Rule 1 :-

for(i=1;i<=n;i++)
   for(j=1;j<=n;j++)
      swap(A[i][j], A[j][i]);

Every pair is visited twice

Output = Original Matrix







Rule 2 :- 

for(i=1;i<=n;i++)
   for(j=i+1;j<=n;j++)
      swap(A[i][j], A[j][i]);


Only upper triangular part is visited.

Each pair is swapped exactly once.

Output :- Transpose

 

Rule 3 :- 

 

for(i=1;i<=n;i++)
   for(j=1;j<i;j++)
      swap(A[i][j], A[j][i]);

 

Only lower triangular part is visited.

Again, each pair is swapped exactly once.

Output :- Transpose
 

That's it And all set!

Answer:
Position:
Show:

Related questions

9 9 votes
5 answers 5 answers
8.8k
8.8k views
go_editor asked Sep 28, 2014
8,845 views
In the context of modular software design, which one of the following combinations is desirable?High cohesion and high couplingHigh cohesion and low couplingLow cohesion ...
90 90 votes
10 answers 10 answers
43.6k
43.6k views
go_editor asked Sep 28, 2014
43,634 views
You have an array of $n$ elements. Suppose you implement quicksort by always choosing the central element of the array as the pivot. Then the tightest upper bound for the...
34 34 votes
5 answers 5 answers
16.4k
16.4k views
go_editor asked Sep 28, 2014
16,378 views
Consider the function func shown below: int func(int num) { int count = 0; while (num) { count++; num>>= 1; } return (count); }The value returned by func($435$) is ______...
6 6 votes
3 3 answers
2.4k
2.4k views
Souvik33 asked Jan 15, 2023
2,400 views
Consider the function func shown below: int func(int num) { int count = 0; while (num) { count++; num>>= 1; } return (count); }The value returned by func(-435) is:69Will ...