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

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

for i=1 to n:
    for j=1 to n:
    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
asked in Algorithms by Veteran (96.2k points)
edited by | 289 views

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.

answered by Active (1.7k points)
edited by

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
49,587 questions
54,197 answers
71,151 users