Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   add 2D array values (http://www.go4expert.com/forums/add-2d-array-values-t23866/)

ganaa 14Nov2010 12:55

add 2D array values
 
hi everyone!
i have a problem with 2D array and tried this for 2 days.
array is m*n and want to add values. explained in following example.

1 2 3
4 5 6
7 8 9
10 11 12

outputs:
1 + 4 + 7 + 10
1 + 4 + 7 + 11
1 + 4 + 7 + 12
1 + 4 + 8 + 10
1 + 4 + 8 + 11
1 + 4 + 8 + 12
1 + 4 + 9 + 10
1 + 4 + 9 + 11
1 + 4 + 9 + 12
1 + 5 + 7 + 10
1 + 5 + 7 + 11
1 + 5 + 7 + 12
1 + 5 + 8 + 10
1 + 5 + 8 + 11
1 + 5 + 8 + 12
1 + 5 + 9 + 10
1 + 5 + 9 + 11
1 + 5 + 9 + 12
1 + 6 + 7 + 10
1 + 6 + 7 + 11
1 + 6 + 7 + 12
1 + 6 + 8 + 10
1 + 6 + 8 + 11
1 + 6 + 8 + 12
1 + 6 + 9 + 10
1 + 6 + 9 + 11
1 + 6 + 9 + 12
and in case of 2 and 3, add sequence are same as 1

thanks

xpi0t0s 15Nov2010 15:01

Re: add 2D array values
 
so:
for each one on the first line V1,
for each one on the second line V2,
for each one on the third line V3,
for each one on the 4th line V4,
display V1+V2+V3+V4

correct? Four nested for loops will do the trick. (You could use recursion if m and n are unknown, but the nested for loops are much easier to implement if you know m and n.)


All times are GMT +5.5. The time now is 05:45.