adding the sum of a row

I am having trouble getting a row to update its accurate sum. I am a true beginner and am trying to follow the manual but the book has nothing about formatting the table i have the data from the array in.

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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const int COLS = 5;
const int ROWS = 5;
void showArray(int[][COLS], string[]);
double getTotal(int[][COLS]);
double getAverage(int[][COLS]);
double getRowTotal(int[][COLS], int);
double getColumnTotal(int[][COLS], int);
double getHighest(int[][COLS], int&, int&);
double getLowest(int[][COLS], int&, int&);

int main()
{

	string division[ROWS] = { "North", "South", "East", "West", "Quarter Total" };
	string quarter[COLS] = { "Quarter 1", "Quarter 2", "Quarter 3", "Quarter 4", "Division Total" };

	double total = 0;
	double average = 0;
	
	double colTotal = 0;
	double highest = 0;
	double lowest = 0;
	int div, qtr; 
	int sales[ROWS][COLS];

	for (div = 0; div < ROWS - 1; div++)
	{
		for (qtr = 0; qtr < COLS - 1; qtr++)
		{
			cout << "What is the sales amount for " << division[div] << " division, "
				<< quarter[qtr] << "?";
			cin >> sales[div][qtr];
		}
		cout << endl;
	}
	for (qtr = 0; qtr < COLS; qtr++)
	{
		cout << "\t" << setw(15) << quarter[qtr];
	}
	showArray(sales, division);
	total = getTotal(sales);
	average = getAverage(sales);
	
	colTotal = getColumnTotal(sales, colTotal);
	highest = getHighest(sales, div, qtr);
	lowest = getLowest(sales, div, qtr);
	
	cout << fixed << showpoint << setprecision(2);
	cout << "The total sales for the company is $" << total << endl;
	cout << "The average sales for each division's quarterly sales is $" << average << endl;
	cout << "The highest sales quarter for the company is $" << highest << endl;
	cout << "The lowest sales quarter for the company is $" << lowest << endl;
	return 0;
}
void showArray(int sales[ROWS][COLS], string division[ROWS])
{
	int div, qtr;
	double rowTotal = 0;
	rowTotal = getRowTotal(sales, rowTotal);
	for (div = 0; div < ROWS - 1; div++)
	{
		cout << "\n\n";
		cout << division[div] << "\t";
		for (qtr = 0; qtr < COLS - 1; qtr++)
		{
			cout << setw(15) << sales[div][qtr] << " ";
		}
		cout << setw(15) << rowTotal;
	}

	cout << "\n\n";
	cout << division[4];
}
double getTotal(int sales[ROWS][COLS])
{
	double total = 0;
	int div, qtr;
	for (div = 0; div < ROWS - 1; div++)
	{
		for (qtr = 0; qtr < COLS - 1; qtr++)
		{
			total += sales[div][qtr];
		}
	}
	return total;
}
double getAverage(int sales[ROWS][COLS])
{
	double total = 0;
	double average = 0;
	int div, qtr;
	for (div = 0; div < ROWS - 1; div++)
	{
		for (qtr = 0; qtr < COLS - 1; qtr++)
		{
			total += sales[div][qtr];
			average = total / ((div +1) * (qtr +1));
		}
	}
	return average;
}
double getRowTotal(int sales[ROWS][COLS], int rowTotal)
{
	for (int div = 0; div < ROWS - 1; div++)
	{
		rowTotal = 0;
		for (int qtr = 0; qtr < COLS - 1; qtr++)
			rowTotal += sales[div][qtr];
	}
	return rowTotal;
}
double getColumnTotal(int sales[ROWS][COLS], int colTotal)
{
	for (int qtr = 0; qtr < COLS - 1; qtr++)
	{
		colTotal = 0;

		for (int div = 0; div < ROWS - 1; div++)
			colTotal += sales[div][qtr];
		cout << "\t" << setw(7) << colTotal << setw(6);
	}	
	cout << endl;
	return 0;
}
double getHighest(int sales[ROWS][COLS], int &div, int &qtr)
{
	int highest = sales[0][0];

	for (int div = 0; div < ROWS - 1; div++)
	{
		for (int qtr = 0; qtr < COLS - 1; qtr++)
		{
			if (sales[div][qtr] > highest)
				highest = sales[div][qtr];
		}
	}
	return highest;
}
double getLowest(int sales[ROWS][COLS], int &div, int &qtr)
{
	int lowest = sales[0][0];

	for (int div = 0; div < ROWS - 1; div++)
	{
		for (int qtr = 0; qtr < COLS - 1; qtr++)
		{
			if (sales[div][qtr] < lowest)
				lowest = sales[div][qtr];
		}
	}
	return lowest;
}
So what exactly is wrong? Code doesn't look bad looking right off the bat.
Thanks for the reply,

The issue is, in the showArray function i am trying to get the amounts of the rows. It is returning the same number from rowtotal for times. How do i get it to return the accurate data from each row?
Hi chrismc1976. I think I see the problem.

This is what is going on.

1
2
3
4
5
6
7
8
9
10
double getRowTotal(int sales[ROWS][COLS], int rowTotal)
{
	for (int div = 0; div < ROWS - 1; div++)
	{
		rowTotal = 0;
		for (int qtr = 0; qtr < COLS - 1; qtr++)
			rowTotal += sales[div][qtr];
	}
	return rowTotal;
}


While you may think that getRowTotal should only get the data for a single specific row, it is always getting the rowTotal of row 4. What's happening is that your for loop for (int div = 0; div < ROWS - 1; div++), finds the rowTotal of the first row, but before you return it, you loop again and thus reset the rowTotal at rowTotal = 0 Then, your code runs through again and finds the rowTotal of the second row. Unfortunately, you never return it because you haven't exited the for loop. Thus you set rowTotal back to 0 and now your for loop is finding the rowTotal of the third row.

Because of this, your getRowTotal will always return the bottom row because that's when you finally exit the for loop.

So what you need to do is have some sort of if statement or something like this:
We need to introduce a new Variable describing which row we want the rowTotal of.

1
2
3
4
5
6
7
double getRowTotal(int sales[ROWS][COLS], int rowTotal, /*int rowNumber*/)
{
	for (int qtr = 0; qtr < COLS - 1; qtr++)
		rowTotal += sales[/*rowNumber*/][qtr];
	
        return rowTotal;
}



Hope this helps!
Thanks for the explanation. I figured that is what was happening, but wasn't for sure. The issue is the solution you suggested will only output the row i specify, correct? The program ask for you to input sale for each quarter, for each four divisions. This gives you four rows that i need to output in the table. The way had the code outputs the last row and the suggestion you gave would output the row i specify. It has to be with the method of it returning the rowTotal.
Topic archived. No new replies allowed.