GATE / CS ARCHIVE

GATE CSE 2014 SET 3 / QUESTION 10

GATE CSE 2014 Set 3 Q10

Programming in C · Arrays & Strings · Full GATE CSE 2014 Set 3 paper

GATE 2014 Set 3, Q10 1 mark · MCQ
Report issue

Arrays & Strings

Question 10, GATE 2014: For an n×n matrix A, execute C=100; for i=1,…,n and j=1,…,n execute Temp=A[i][j]+C; A[i][j]=A[j][i]; A[j][i]=Temp−C. Then print A in row order. What is printed? A original A; B transpose of A; C upper-diagonal entries increased by 100 and lower-diagonal entries decreased by 100; D none of these.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Step 1: Track one iteration with old values u=A[i][j] and v=A[j][i]. Temp=u+100, then the assignments store v and (u+100)−100=u, so the body swaps the two entries. Step 2: For i≠j, both index pairs (i,j) and (j,i) occur in the complete nested loops. The second swap undoes the first. A diagonal entry is swapped with itself. Step 3: Consequently every entry finishes unchanged, A. This reasoning uses exact arithmetic or assumes no problematic overflow in adding and subtracting C.

Original paper, page 55 · Answer key