In Q7, please explain the sum equation, I mean why 2 pointers are used?
Syntax not clear
@Enigmatctrance
The correct answer is C. Let’s dry run this code on an arr =
arr = [ 1 2 3 ]
[ 4 5 6 ]
[ 7 8 9 ]
At first iteration,
i = 0, sum = 0
sum = sum + *( *(arr+i) + i++ ) ;
sum = sum + *( *(arr+0) + 0 ) ; // i is 1 now because of i++ ;
sum = sum + *((arr[0]) + 0 ) ;
sum = sum + (arr[0][0] ) ;
sum = 0 + 1 ;
sum = 1 ;
At second iteration,
i = 1, sum = 1
sum = sum + *( *(arr+i) + i++ ) ;
sum = sum + *( *(arr+1) + 1 ) ; // i is 2 now because of i++ ;
sum = sum + *((arr[1]) + 1 ) ;
sum = sum + (arr[1][1] ) ;
sum = 1 + 5 ;
sum = 6 ;
At third iteration,
i = 2, sum = 6
sum = sum + *( *(arr+i) + i++ ) ;
sum = sum + *( *(arr+2) + 2 ) ; // i is 3 now because of i++ ;
sum = sum + *((arr[2]) + 2 ) ;
sum = sum + arr[2][2] ;
sum = 6 + 9 ;
sum = 15 ;
every time sum += arr[i][i] is happening and arr[i][i] is the diagonal element. Hence c option is the correct one.
Hope this helps
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.