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:

Recursion and Iteration

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Share Article:

Leave a Reply

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock