add 2D array values

Discussion in 'C' started by ganaa, Nov 14, 2010.

  1. ganaa

    ganaa New Member

    Joined:
    Feb 12, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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 likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice