2d array, sum rows and columns and store in another array

So here's another interesting task. I know, i'm on the right path, just something doesn't work here. Basicly I have to sum rows and columns of 2d array and then store the results in separate arrays, as far as the code is now, can see it quite clearly. any advice?!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
#include<cmath>
using namespace std;
int main()

{
    int array[4][5], array1[4], array2[5], sum1, sum2;
    sum1=0;
    sum2=0;
   for (int x=0; x<4; x++) 
       for (int y=0; y<5; y++) 
       
        { cout<<"ievadi masiva elementu["<<x<<"]["<<y<<"]"<<endl;
            cin>>array[x][y];
            }  
  { for (int x=0; x<4; x++) 
      for (int y=0; y<5; y++) 
      {
          sum1+=array[x];
          sum2+=array[y];
         
         strcpy (array1,sum1);
         strcpy (array2,sum2);
         
         cout<<array1<<endl;
         cout<<array2<<endl;
}
}
        system ("pause");
        return 0;
        }
  Put the code you need help with here.
closed account (o3hC5Di1)
Hi there,

First off, you really need to fix your placement of curly braces and indentation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
#include<cmath>
using namespace std;
int main()

{
    int array[4][5], array1[4], array2[5], sum1, sum2;
    sum1=0;
    sum2=0;
   
    for (int x=0; x<4; x++)
    {
       for (int y=0; y<5; y++) 
       { 
            cout<<"ievadi masiva elementu["<<x<<"]["<<y<<"]"<<endl;
            cin>>array[x][y];
        }
    }
    
    for (int x=0; x<4; x++) 
    {
      for (int y=0; y<5; y++) 
      {
          sum1+=array[x];
          sum2+=array[y];
         
         strcpy (array1,sum1);
         strcpy (array2,sum2);
         
         cout<<array1<<endl;
         cout<<array2<<endl;
      }
    }
    
    return 0;
}


Then, strcpy() is used to copy c-strings, so it's not ideal for an array of ints. need to change your logic a little:

1
2
3
4
5
6
7
8
9
10
11
 for (int x=0; x<4; x++)  //this loop traverses rows
    {
        sum1=0;
      for (int y=0; y<5; y++)  //this loop traverses columns
      {
          sum1+=array[x][y];
          array2[y] += array[x][y];
      }
      
      array1[x] = sum;
    }



Printing the arrays also needs a for loop:

1
2
for (int i=0; i<4; ++i)
    std::cout << array1[i] << ", ";


Hope that helps, please don't hesitate to ask if you have any further questions.

All the best,
NwN
Here is a simple way to do what you want to achieve:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

using std::cout;

int main()
{
    int array[6][7], columntotal[6] = {0}, rowtotal[7] = {0}; //I think descriptive names are good.
    int x, y, z = 1; //x & y are just used to iterate around loops, z will be used to fill up the array with some numbers.
    for(x = 0; x < 6; ++x) //Just fill up the array with some numbers.
    {
        for(y = 0; y < 7; ++y)
        {
            array[x][y] = z;
            ++z;
        }
    }
    for(x = 0; x < 6; ++x) //Sum the columns.
    {
        for(y = 0; y < 7; ++y)
        {
            columntotal[x] = columntotal[x] + array[x][y];
        }
    }
    for(x = 0; x < 6; ++x) //Output the value of each column. 
    {
        cout << columntotal[x] << "\n";
    }
    return 0; 
}


That will sum columns. I'll leave it to you to figure out how to sum the rows. Btw, you are not using any functions from <cmath> so no need to include that header file.

As for the problems in your original code:
1
2
3
4
 

          sum1+=array[x];
          sum2+=array[y];


When accessing the elements in a 2d array, you must always refer to both dimensions to get the information.
 
sum1 += array[x][0]; 


Would be valid for example.

1
2
strcpy (array1,sum1);
         strcpy (array2,sum2);


The way you are using strcpy here is incorrect. Also, strcopy requires inclusion of <cstring>. Hmm anyway I suggest reading about what strcopy does: http://www.cplusplus.com/reference/cstring/strcpy/
And not using it in this program, because you don't need to.

You should also avoid using:
 
system ("pause");


Try perhaps to use getchar() or something and try not to use system("anything").

Last edited on
Great! thanks all your advices helped to get sums properly now and sort out all the mess in my code :) but still I have to sleep on this! This is a task for school therefore i have teachers rules to follow, yes i can use <cstring>, guess that would make things easier, functions there are quite useful.
how to do rows and columns
1
2
3
4
5
6
for (int i=0; i<arrWidth; i++)
    for (int j=0; j<arrHeight; j++)
    {
        column[i]+=arr[i][j]
        row[j]+=arr[i][j]
    }

how to print rows and columns
1
2
3
4
5
6
7
8
cout<<"Row:";
for (int i=0; i<arrHeight; i++)
    cout<<" "<<row[i];
cout<<endl;
cout<<"Column:"
for (int i=0; i<arrWidth; i++)
    cout<<" "<<column[i];
cout<<endl;



You should also avoid using: system ("pause");


Why? Its just school homework. If its there, and easy to use... whats the problem?

IRL, you won't use console for output. (Whens the last time you've seen a console application on sale?). So, using system("anything") won't hurt u once you get out of classrooms either...
Last edited on
closed account (o3hC5Di1)
threeright wrote:
Why? Its just school homework. If its there, and easy to use... whats the problem?


http://www.cplusplus.com/forum/articles/11153/

It's never too early to start good coding habits, unless you want to be the guy responsible for your company's million dollar law suit because of a vulnerability you neglected. You can never take security serious enough and part of the problem starts with people learning unsafe habits during their education.

threeright wrote:
IRL, you won't use console for output. (Whens the last time you've seen a console application on sale?). So, using system("anything") won't hurt u once you get out of classrooms either...


I take it you're a windows-user. On Unix/Linux, which are still the main part of the servers out there, command line utilities are the norm rather than exceptions.

All the best,
NwN
nice article!
Mats really thank you! This is how it looks now: and it all works. Question about system thing - what to use to pause programm? cin.get() and getch () doesnt work for me and havent found nothing else really, and i do work on windows 7 :D and antivirus is going crazy :D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;

int main()
{
    int array[4][5], columntotal[4] = {0}, rowtotal[5] = {0}; 
    int x, y, ; 
    for(x = 0; x < 4; x++) 
        for(y = 0; y < 5; y++)
        
        { cout<<"ievadi masiva elementu["<<x<<"]["<<y<<"]\n"; 
         cin>>array[x][y];}
        
    for(x = 0; x < 4; ++x) 
    {
        for(y = 0; y < 5; ++y)
        { columntotal[x] = columntotal[x] + array[x][y];}
    }
    for(x = 0; x < 4; x++) 
    { cout <<"Summas kolonnas: "<<columntotal[x]<<endl;}

    
    for(x = 0; x < 4; x++) 
    {
        for(y = 0; y < 5; y++)
        { rowtotal[y] = rowtotal[y] + array[x][y];}
    }
    for(y = 0; y < 5; y++) 
    { cout <<"Summas horizontales: "<<rowtotal[y]<<endl;}
    
    system ("Pause");
    return 0; 
}
If you place this at the end of programs (to pause them), it will be just fine.
1
2
std::cin.clear();
std::cin.ignore(1);


Of course if you're using namespace std, you can leave off the std:: bit. You just press return to finish.
Last edited on
Topic archived. No new replies allowed.