Summing column and row values.

I was wondering if I could receive some assistance with this problem? I am having an issue in returning the row and column values back to my table. The programs prototype are formatted with the guidelines I have. I was able to get the results of the rows but I couldn't figure out how to return it to the table for the 5th column and the 5th row. Thank you very much, for any assistance.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  /*This program asks for input from the user to display the sales for 4 divisions and 4 quarters. This program will 
calculate the total for each quarter, and the total for each division displayed as a table. It will then give the total 
sales for all quarters and division as well as provide the division with the highest quarter of sales and the division
with the lowest quarter of sales*/

#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

const int COLS = 4;
const int ROWS = 4;
const string DIVISIONS[5] = { "NORTH", "SOUTH", "EAST", "WEST", "QUARTER TOTAL" };

//Prototypes
double getTotal(double[][COLS]);
double getAverage(double[][COLS]);
double getRowTotal(double[][COLS], int);
void showtable(double[][COLS], int);
//double getColumnTotal(double[][COLS], int);
//double getHighest(double[][COLS], int&, int&);
//double getLowest(double[][COLS], int&, int&);

int main()
{
	
	double sales[ROWS][COLS];
	double totalSales,
		average;
	int  div, qtr;
	int n = 5;

	// User input to populate table.
	for (div = 0; div < 4; div++)
	{
		for (qtr = 0; qtr < 4; qtr++)
		{
			cout << DIVISIONS[div]<< endl;
			cout << "Quarter " << qtr + 1 << endl;
			cin >> sales[div][qtr];
		}
	}

	//  Adds up all sales for quarters and divisions.
	totalSales = getTotal(sales);
	cout << totalSales << endl;

	// Gets average for company
	average = getAverage(sales);
	cout << average << endl;

	// Returns row total for each quarter.
	  getRowTotal(sales,ROWS);

	showtable(sales, ROWS);


	return 0;
}

// Returns total of table.
double getTotal(double sales[ROWS][COLS])
{
	double totalSales = 0;
	for (int row = 0; row < 4; row++)
	{
		for (int col = 0; col < 4; col++)
		{
			totalSales += sales[row][col];
		}
	}
	return totalSales;
}


// Returns the average.
double getAverage(double sales[ROWS][COLS])
{
	double totalSales = 0;
	double average;
	for (int row = 0; row < 4; row++)
	{
		for (int col = 0; col < 4; col++)
		{
			totalSales += sales[row][col];
		}
	}

	average = totalSales / (ROWS * COLS);
	return average;
}

//Adds up the rows.
double getRowTotal(double sales[ROWS][COLS], int n)
{
	double total = 0;
	
	for (int row = 0; row < ROWS; row++)
	{
		 total = 0;

		for (int col = 0; col < COLS; col++)
		{
			
			total += sales[n][col];
			
			
		}
		
			
	}
	return total;
			
	
}

// Displays table.
void showtable(double sales[][COLS], int rows)
{
	for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < COLS; y++)
		{
			cout << setw(4) << sales[x][y] << " ";
		}
		cout << endl;
	}
}
Last edited on
closed account (D80DSL3A)
I'm thinking that the number for the 5th column under "QUARTER TOTAL" is the number returned by the function getRowTotal. The right place to call it is in the function showTable as the output for each table row is being produced. You shouldn't be calling it in main().
1
2
3
4
5
6
7
8
9
10
11
12
void showtable(double sales[][COLS], int rows)
{
	for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < COLS; y++)
		{
			cout << setw(4) << sales[x][y] << " ";
                        // call getRowTotal() here. Pass x as 2nd parameter. Format using setw as needed.
		}
		cout << endl;
	}
}

Also, there only needs to be one loop in the getRowTotal function, not two. You can eliminate the outer for loop.
Lastly, you're using the number 4 in many places where the constants ROWS and COLS belong. Though the 4's will work (especially because fortunately ROWS=COLS) it's better practice to use the defined constants.
Last edited on
Thank you for the feed back. I tried what you suggested and this is how I wrote it up.
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
double getRowTotal(double sales[ROWS][COLS], int x)
{
	double total = 0;
	
		 

		for (int col = 0; col < COLS; col++)
		{
			total = 0;
			total += sales[x][col];
			
			return total;
		}
	
			
	
}
void showtable(double sales[][COLS], int rows)
{
	for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < COLS; y++)
		{
			cout << setw(4) << sales[x][y] << " ";
			cout << getRowTotal(sales, x);
		}
		cout << endl;
		
	}
}



The problem I am getting now is that it is only summing immediately the row after, and not the row as a total. Any more suggestions or did I mess up something with the code.
Last edited on
closed account (D80DSL3A)
The line total=0; was outside the inner for loop before, so I thought you'd be good. Moving it inside caused the problem. Can you see that it assigns total=0 just before each addition? Just delete line 9. Line 3 takes care of making total=0;
Why is the return inside the for loop? It belongs outside, where you had it before.
Like this :)
1
2
3
4
5
6
7
8
9
10
11
double getRowTotal(double sales[ROWS][COLS], int x)
{
	double total = 0;		 

	for (int col = 0; col < COLS; col++)
	{
		total += sales[x][col];			
	}
			
	return total;
}
Last edited on
Thank you so much for the help with this. My only other question would be about the columns. I thought if I took the same format I used with the row total and just flipped it for the column total I would be ok, but when I did that it gave me the same issue I was having with the rows.
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
37
38
39
40
41
42
43
44
45
46
47
48
double getRowTotal(double sales[ROWS][COLS], int x)
{
	
	double total = 0;

		for (int col = 0; col < COLS; col++)
		{
			
			total += sales[x][col];
			
			
		}
	
	return total;	
	
}
double getColumnTotal(double sales[ROWS][COLS], int y)
{

	double total = 0;

	for (int row = 0; row < ROWS; row++)
	{

		total += sales[y][row];


	}

	return total;

}
void showtable(double sales[][COLS], int rows)
{	
	for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < COLS; y++)
		{
			
			cout << setw(4) << sales[x][y] << " ";
			cout << getColumnTotal(sales, y);
		}
		cout << getRowTotal(sales, x);
		
		cout << endl;
		
	}
}


Am I completely wrong or do I just need to tweek it a little bit.
closed account (D80DSL3A)
I'm sorry. I see I made a mistake on where to place the call to the getRowTotal function. It belongs in the outer for loop. I guess I should test before posting!
Does this work better?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void showtable(double sales[][COLS], int rows)
{
	for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < COLS; y++)
		{
			cout << setw(4) << sales[x][y] << " ";
                        // this was the wrong spot for calling getRowTotal. Row of 8 numbers results if here.

		}
		cout << setw(4) << getRowTotal(sales, x);// better spot
		cout << endl;
	}
}
This one worked great it took me a second but realized that I needed to take it out of the inner loop. For the columns I am not sure how to get the sum of each column added to the last row. I posted above to show what I had. I am not sure what I am getting wrong with that. I am still very new to programming and some of the stuff makes a lot of sense but then some things I am having a lot of trouble with. Appreciate it.
closed account (D80DSL3A)
You're welcome for the semi-competent help.
For the 5th row a getColumnTotals function could be useful.
You want to reverse which index is summed over though.
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
double getColumnTotal(double sales[ROWS][COLS], int y)
{

	double total = 0;

	for (int row = 0; row < ROWS; row++)
	{
		total += sales[row][y];// sum down column y
	}

	return total;
}

I'm actually testing this stuff now, so it should work.
Would the 5th entry in the 5th column be the total of all sales? If so, just call your getTotal(sales) function for that one.

I added this code for the 5th row to the showTable function:
1
2
3
4
5
6
7
// 5th row
for (int x = 0; x < rows; x++)
{
    cout << setw(4) << getColumnTotal(sales, x) << " ";
}
cout << setw(4) << getTotal(sales);
cout << endl;

I get the following output for the table:

  10   20   30   40  100
  20   30   40   50  140
  30   40   50   60  180
  40   50   60   70  220
 100  140  180  220  640
Last edited on
Topic archived. No new replies allowed.