Ravi and Rupali are asked to write a program to sum the rows of 2X2 matrices stored in the array A.
Ravi writes the following code (Code A):

 for n = 0 to 1 
 sumRow1[n] = A[n][1] + A[n][2]
 end

Rupali writes the following code (Code B):

sumRow1[0] = A[0][1] + A[0][2] 
sumRow1[1] = A[1][1] + A[1][2]

Comment upon these codes (Assume no loop unrolling done by compiler) ?

A. Code a will execute faster than b.

B. Code b will execute faster than a.

C. Code a is logically incorrect.

D. Code b is logically incorrect

Answer : B. Code b will execute faster than a

Explanation:

  • Both codes are taking two steps of operation, so same complexity.
  • But if we consider the overhead of looping (as it takes time to increment counter) then code b will be faster.

Categorized in: