Labeling rows and adding total column

I am currently taking C++ class and we have this homework that we have to do. I have been working with my classmates and I am trying to figure out how to label the rows and I did find a code but it is printing out weird. Also, how can I add the total column? At one point, I was able to add the total column but the sum of other columns were off. Can any please help? I'd greatly appreciate any input.

**I COMMENTED OUT THE ROW LABEL CODES**

Here is the code:

#include <iostream>
//#include <string>
//#include <array>
#include <iomanip>

using namespace std;

const int rows = 4;
const int columns = 5;
const char row_label_prefix[] = "Sales Person";
double array, sum, totalSales, sales[rows][columns];

int main() {
double array[rows][columns] = {
{ 58.50, 10.25, 13.75, 45, 0 },
{ 45, 40, 32, 30.99, 10.85 },
{ 11, 45, 85.10, 22, 23.12 },
{ 11.25, 22.50, 0.25, 10.50, 0}
};

cout << setw(10) << "Prod1" << setw(11) << "Prod2"
<< setw(11) << "Prod3" << setw(11) << "Prod4"
<< setw(11) << "Prod5" << setw(10) << "Total\n";

//for (int rows = 0; rows < 4; ++rows) {
// cout << row_label_prefix << rows << "\n\n";
//}

for (int rows = 0; rows < 4; rows++) {
double sum = 0;
for (int columns = 0; columns < 5; columns++) {
cout << setw(10) << array[rows][columns] << " ";
//cout << row_label_prefix << rows << "\n";

sum += array[rows][columns];
}
cout << setw(8) << sum << endl;
}

//cout << row_label_prefix << rows << "\n";

for (int columns = 0; columns < 5; columns++) {
double sum = 0;
for (int rows = 0; rows < 4; rows++) {
sum += array[rows][columns];
}
cout << setw(10) << sum << " ";
}
system("pause");

}
Last edited on
@dullerthandull ( Though I'm sure you're NOT )

Total columns and rows were correct, but looked weird because they were not under their corresponding columns. In order for the rows to match up with the data, you must first print the sales person number, then print out the row data. For each row. Here's one way to do it..
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
#include <iostream>
#include <iomanip>

using namespace std;

const int rows = 4;
const int columns = 5;
const char row_label_prefix[] = "Sales Person";
double array, sum, totalSales, sales[rows][columns];

int main()
{
	double array[rows][columns] = {
		{ 58.50, 10.25, 13.75, 45, 0 },
		{ 45, 40, 32, 30.99, 10.85 },
		{ 11, 45, 85.10, 22, 23.12 },
		{ 11.25, 22.50, 0.25, 10.50, 0}
	};

	cout << "\t\t" << setw(10) << "Prod1" << setw(11) << "Prod2"
		<< setw(11) << "Prod3" << setw(11) << "Prod4"
		<< setw(11) << "Prod5" << setw(10) << "Total\n";

	for (int rows = 0; rows < 4; rows++)
	{
		cout << row_label_prefix << " " << rows+1 << " "; // So Sales Person numbers start with 1
		sum = 0;
		for (int columns = 0; columns < 5; columns++) 
		{
			cout << setw(10) << array[rows][columns] << " ";
			sum += array[rows][columns];
		}
		cout << setw(8) << sum << endl;
	}
	cout << "\t\t"; // Help line up sums with columns
	for (int columns = 0; columns < 5; columns++) 
	{
		sum = 0;
		for (int rows = 0; rows < 4; rows++) {
			sum += array[rows][columns];
		}
		cout << setw(10) << sum << " ";
	}
	cout << endl << endl;
	system("pause");
	return 0;
}


Please note: In the future, when you post code, highlight the code after pasting and click the <> symbol in the menu, on the right, under format: It makes reading, and commenting on the code, a lot easier, plus it formats the code, as you can see above. Thanks. Lecture over ;)
@whitenite1 thank you! I didn't know that, I actually thought it would format automatically but thanks for the tip! I'll keep in mind. Thanks again!
Topic archived. No new replies allowed.