Thread: add 2D array values

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So post your last best attempt at the solution.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    i stopped the program it always resulted wrong
    it is making me crzay
    Code:
        int a[4][4]={{1,2,3,4},             //i mor
                     {5,6,7,8},             //j bagana
                     {9,10,11,12},
                     {13,14,15,16}};
                 
        int i1;
        int j1;
        int i=0,j=0,b[5][5],r,chose,sanakh=4,q,too=0,ber=0, save=0,horg=3,qwe=0,ewq=0;
    int sum[1000];
    for(int l = 0; l < 1000; l++)
    sum[l] = 0;
     //   while(1==1){
           //   for(j=0;j<=3;j++)
           //   {
                
                for(r=j;r<=3;r++)
                  {  for(q=0;q<=16;q++)
                  
                      {  save=0; top: for(i=0;i<=3;i++)
                                   {
                                               
                                   if(i<horg ) {save=save+a[i][r]; }
                                   if(horg==i && qwe==ewq)  {
                                                             for(int z=0;z<=3;z++)
                                                             {
                                                             sum[ber]=save+a[i][z];ber++;too++;
                                                             qwe++;
                                                            
                                                             }
                                                             save=0; 
                                                             horg--;
                                                             }
                                   else if (horg==i) {   sanakh=save;  
                                                         for (int t=r+1;t<=3;t++)
                                                         {
                                                              sanakh=save+a[i][t]; 
                                                              for(int d=0;d<=3;d++)
                                                                  {  
                                                                   cout<<sanakh<<"  ";
                                                                   sum[ber]=a[i+1][d]+sanakh; ber++;too++;
                                                                           
                                                                   } 
                                                              
                                                         }
                                                         
                                                         horg--;
                                                         if(i<4 )  {goto top;  }
                                                     }
                                   }
                                   sanakh--;          
                                   }             
               }
                                 int h=1;
                                  for(q=0;q<=30;q++)      { cout<<sum[q]<<" ";
                                  if (h==4){ cout<<endl;h=0;}h++;}

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the first thing is to learn the value of indentation.
    SourceForge.net: Indentation - cpwiki

    Here is your code, indented.
    Code:
    int a[4][4] = { {1, 2, 3, 4},   //i mor
    {5, 6, 7, 8},                   //j bagana
    {9, 10, 11, 12},
    {13, 14, 15, 16}
    };
    
    int i1;
    int j1;
    int i = 0, j = 0, b[5][5], r, chose, sanakh = 4, q, too = 0, ber =
        0, save = 0, horg = 3, qwe = 0, ewq = 0;
    int sum[1000];
    for (int l = 0; l < 1000; l++)
        sum[l] = 0;
     //   while(1==1){
           //   for(j=0;j<=3;j++)
           //   {
    
    for (r = j; r <= 3; r++) {
        for (q = 0; q <= 16; q++) {
            save = 0;
          top:
            for (i = 0; i <= 3; i++) {
                if (i < horg) {
                    save = save + a[i][r];
                }
                if (horg == i && qwe == ewq) {
                    for (int z = 0; z <= 3; z++) {
                        sum[ber] = save + a[i][z];
                        ber++;
                        too++;
                        qwe++;
                    }
                    save = 0;
                    horg--;
                } else if (horg == i) {
                    sanakh = save;
                    for (int t = r + 1; t <= 3; t++) {
                        sanakh = save + a[i][t];
                        for (int d = 0; d <= 3; d++) {
                            cout << sanakh << "  ";
                            sum[ber] = a[i + 1][d] + sanakh;
                            ber++;
                            too++;
                        }
                    }
    
                    horg--;
                    if (i < 4) {
                        goto top;
                    }
                }
            }
            sanakh--;
        }
    }
    
    int h = 1;
    for (q = 0; q <= 30; q++) {
        cout << sum[q] << " ";
        if (h == 4) {
            cout << endl;
            h = 0;
        }
        h++;
    }
    next_permutation
    Create an array called
    Code:
    row_index[4] = { 0, 1, 2, 3 };
    Create another called
    Code:
    col_index[4] = { 0, 1, 2, 3 };
    For each permutation of row_index, you permute all of col_index

    For all of that, you do
    Code:
    for ( i = 0 ; i < 4 ; i++ ) 
      sum += a[row_index[i]][col_index[i]];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Unfortunately, note that Salem's suggestion of next_permutation is from the C++ standard library; in C you have to implement the algorithm yourself.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    And...another cross-poster.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Mmm, this is indeed the C forum, yet the code has cout calls.
    Moved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    This is probably spoon-feeding, but
    Code:
    #include <iostream>
    #include <iomanip>
    #include <algorithm>
    using namespace std;
    
    int main ( ) {
      int a[4][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12},
        {13, 14, 15, 16}
      };
      int row_index[] = { 0, 1, 2, 3 };
      int col_index[] = { 0, 1, 2, 3 };
      const int N = sizeof(row_index) / sizeof(row_index[0]);
      
      do {
        do {
          for ( int i = 0 ; i < N ; i++ ) {
            cout << "a[" << row_index[i] << "]["<< col_index[i] << "]";
            if ( i == N-1 ) cout << endl; else cout << "+";
          }
        } while ( next_permutation(col_index, col_index+N) );
      } while ( next_permutation(row_index, row_index+N) );
    
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. value in random slot of 2d array?
    By DJPlayer in forum C Programming
    Replies: 3
    Last Post: 02-23-2010, 10:36 PM
  2. Replies: 7
    Last Post: 07-28-2009, 03:15 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM