For Loop

how to solve this?

4Comments
Ashish Kumar Goyal @dashish
2 Feb 2017 03:49 pm

it will be 30.

sonali @sonalirangwani
2 Feb 2017 03:57 pm

please explain me!! 30 is right answer

 

Ashish Kumar Goyal @dashish
2 Feb 2017 04:26 pm

Nothing mentioned so by default take row major order.

*(p+20*j+i) is same as accessing p[j][i].

so p[6][10]=306 and p[6][9]=276.. Subtract and u will get 30.

Ashish Kumar Goyal @dashish
3 Feb 2017 09:54 am

@sonalirangwani @sumitverma

Though answer came to be correct but i was wrong..

Notice A[20][30]..... therefore, *(p+20*j+i) will not equal p[j][i].

here, we will check for what vaue of i and j, we will point to A[6][10] and A[6][9]..

A[6][10] = *(p + 30*6 + 10)= *(p + 180 + 10) = *(p + 20*9 + 10) => i.e., for j=9 and i=10, A[6][10] = 30*10 + 9 =309

Similarly, A[6][9] => for j=9 and i=9. A[6][9] = 30*9 + 9 = 279.

hence, 309-279 = 30.