Open In App
Related Articles

GATE | GATE-CS-2014-(Set-3) | Question 20

Improve Article
Save Article
Like Article

Let A be a square matrix of size n x n. Consider the following program. 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


The matrix A itself


Transpose of matrix A


Adding 100 to the upper diagonal elements and subtracting 100 from diagonal elements of A


None of the above

Answer: (A)


If we take look at the inner statements of first loops, we can notice that the statements swap A[i][j] and A[j][i] for all i and j. Since the loop runs for all elements, every element A[l][m] would be swapped twice, once for i = l and j = m and then for i = m and j = l. Swapping twice means the matrix doesn’t change.

Quiz of this Question
Please comment below if you find anything wrong in the above post

Level Up Your GATE Prep!
Embark on a transformative journey towards GATE success by choosing Data Science & AI as your second paper choice with our specialized course. If you find yourself lost in the vast landscape of the GATE syllabus, our program is the compass you need.

Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads