The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+5 votes
293 views

Consider the following program modifying an $n \times n$ square matrix $A$:

for i=1 to n:
    for j=1 to n:
        temp=A[i][j]+10
        A[i][j]=A[j][i]
        A[j][i]=temp-10
    end for
end for

Which of the following statements about the contents of matrix $A$ at the end of this program must be TRUE?

  1. the new $A$ is the transpose of the old $A$
  2. all elements above the diagonal have their values increased by $10$ and all the values below have their values decreased by $10$
  3. all elements above the diagonal have their values decreased by $10$ and all the values below have their values increased by $10$
  4. the new matrix $A$ is symmetric, that is, $A[i][j]=A[j][i]$ for all $1 \leq i, j \leq n$
  5. $A$ remains unchanged
in Algorithms by Veteran (100k points)
edited by | 293 views
0

In executing the code above, an element a[i][j] of Matrix is modified $2-times$, And thus final value of A remains unchanged.

1 Answer

+7 votes
Best answer
1.for i=1 to n:
2.    for j=1 to n:
3.        temp=A[i][j]+10
4.        A[i][j]=A[j][i]
5.        A[j][i]=temp-10
6.    end for
7.end for

The $3,4,5$ lines swap $A [ j ] [ i ]$ and $A [ i ] [ j ]$.

The same variables are swapped twice. For eg when: $i =5$, $j = 10$. $A [10] [5]$ and $A [5] [10]$ will be swapped. They will be swapped again when $i = 10$, $j =5$.

Two times swap of same elements will lead to $A$ remaining unchanged.

Hence, E is correct.

by Active (1.7k points)
edited by
0
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
50,092 questions
55,239 answers
190,756 comments
85,992 users