The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+19 votes
2k views

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
asked in Algorithms by Veteran (107k points)
edited by | 2k views
+1
  1. swap symmetric off-diagonal elements
  2. Perform step $1$ for all elements one-after-another
  3. We will get the same matrix

3 Answers

+25 votes
Best answer

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$.

answered by Boss (19.7k points)
edited by
0
But when upper triangular matrix is changed then how the lower triangular matrix get the value of previous upper triangular matrix.
+16 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.
answered by Boss (23k points)
+2
crystal clear explanation @RAJESh
0
Thank U @Rajesh
0
What is the need of C in d question??why don't v add?
+3 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.
answered by Active (3.2k points)
0
I think first row iteration is wrong as it never store added value 100 in matrix.

plz see the code.
0

Aspi R Osa again check that logic . u can see that u r wrong.

0
what is the significance of last for loop in the question?
0
To print the rows of the Matrix.
Answer:

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

42,685 questions
48,650 answers
156,446 comments
63,961 users