# Thread: Need help with a simple program

1. ## Need help with a simple program

Well i got the class c++ programming 2 and i got my first program to write and im stuck.. on the problem here is the problem.. i would appreciate any help i can get. or any ideas of how i can accomplish this. thanks

sincerely,
Ara

Program 1,
Given the following two-dimensional 6x6 square array:

2 3 38 21 19 3
3 23 49 37 83 -7
14 37 20 2 4 22
30 2 2 3 223 22
18 43 29 37 34 23
81 34 92 73 43 32

Write a program that will compute:

The sum of each row.
The sum of each column.
The sum of the major diagonal (coordinates that satisfy r=c)
the sum of the minor diagonal (coordinates that satisfy 6-r=c)
The sum of each concentric square (i.e. all points equidistant from the border)

what i got so far. i have the rows and colums adding already i just need some help with the last three. this is what i have so far.

Code:
```using namespace std;
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main () {
int a[6][6] =
{
{2, 3, 38, 21, 19, 3},
{3, 23, 49, 37, 83, -7},
{14, 37, 20, 2, 4, 22},
{30, 2, 2, 3, 223, 22},
{18, 43, 29, 37, 34, 23},
{81, 34, 92, 73, 43, 32}
};
const int csize = 6;
const int rsize= sizeof(a)/sizeof(int)/csize;

for (int r=0; r < rsize; r++ ) {
cout << setw(5) <<  a[r][0];
for (int c=1; c < csize; c++) { // Add a[r][c] to sum
cout << " "<<  setw(rsize) <<a[r][c];
}
cout  << endl;
}

for (int r=0; r < rsize; r++ ) {
int sum = 0;
for (int c=0; c < csize; c++) { // Add a[r][c] to sum
sum = sum + a[r][c];
}
cout << "Row  " << r << " sum: " << sum << endl;
}

for (int c=0; c < csize; c++ ) {
int sum = 0;
for (int r=0; r < csize; r++) { // Add a[r][c] to sum
sum = sum + a[r][c];
}
cout << "Col  " << c << " sum: " << sum << endl;
}

return 0;
}```

2. CODE TAGS first, answer later.

3. Originally Posted by t_r_a_n_s__am
Well i got the class c++ programming 2 and i got my first program to write and im stuck.. on the problem here is the problem.. i would appreciate any help i can get. or any ideas of how i can accomplish this. thanks

sincerely,
Ara

Program 1,
Given the following two-dimensional 6x6 square array:

2 3 38 21 19 3
3 23 49 37 83 -7
14 37 20 2 4 22
30 2 2 3 223 22
18 43 29 37 34 23
81 34 92 73 43 32

Write a program that will compute:

The sum of each row.
The sum of each column.
The sum of the major diagonal (coordinates that satisfy r=c)
the sum of the minor diagonal (coordinates that satisfy 6-r=c)
The sum of each concentric square (i.e. all points equidistant from the border)

what i got so far. i have the rows and colums adding already i just need some help with the last three. this is what i have so far.

Code:
```using namespace std;
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main () {
int a[6][6] =
{
{2, 3, 38, 21, 19, 3},
{3, 23, 49, 37, 83, -7},
{14, 37, 20, 2, 4, 22},
{30, 2, 2, 3, 223, 22},
{18, 43, 29, 37, 34, 23},
{81, 34, 92, 73, 43, 32}
};
const int csize = 6;
const int rsize= sizeof(a)/sizeof(int)/csize;

for (int r=0; r < rsize; r++ ) {
cout << setw(5) <<  a[r][0];
for (int c=1; c < csize; c++) { // Add a[r][c] to sum
cout << " "<<  setw(rsize) <<a[r][c];
}
cout  << endl;
}

for (int r=0; r < rsize; r++ ) {
int sum = 0;
for (int c=0; c < csize; c++) { // Add a[r][c] to sum
sum = sum + a[r][c];
}
cout << "Row  " << r << " sum: " << sum << endl;
}

for (int c=0; c < csize; c++ ) {
int sum = 0;
for (int r=0; r < csize; r++) { // Add a[r][c] to sum
sum = sum + a[r][c];
}
cout << "Col  " << c << " sum: " << sum << endl;
}

return 0;```
You're missing a closing brace (}).

4. ok done fixed sorry should have read the sticky any help would be appreciated thanks

5. What's your question? Have you tried the other three? They follow the same basic procedure as the first two - loop through the values and only sum the ones you want. You just have to figure out the best way to loop through the values. The diagonals should be pretty easy, since you only really need one loop.

6. As Daved said, for (3) and (4) use a loop like you already have:
Code:
```for (int r=0; r < rsize; r++ ) {
//You gave us the formula for c, so use that.

//Then sum as before.
sum = sum + a[r][c];```
For (5), I'm not sure what it's asking.

7. For (5), you could start out with some code to sum the whole square (which you almost have, except sum would not be reset to zero after each row). Then add an outer loop to determine where the border starts (for your square the start of the border is going to be either 0, 1, or 2). Then your two inner loops would start at this value, instead of 0 as you are using for parts (1) and (2).