add 2D array values

ganaa's Avatar, Join Date: Feb 2009
Newbie Member
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's Avatar, Join Date: Aug 2004
Mentor
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.)
shabbir like this