How to get the sum of the columns in the array

For the program I had to pull the information for the first 4 columns from a file called BusInput.txt with this in it.
111 100.00 200.00 50.00
222 200.00 300.00 100.00
333 400.00 500.00 75.00

The last 2 columns I calculated with a variable.
NOW, I need to find the sum of all of the columns EXCEPT the Bus No.

This is my current code. How do I get the sum of all of the columns?

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
#include <iostream>
#include <iomanip>
#include <fstream>

const int ROW = 200;
const int  COL = 6;
int total = 0;
int count = 0;
int num[ROW][COL];
int next, busNum;
double fuel, equip, misc, dFuel, finCost;

using namespace std;

int main()
{
	ifstream inputFile;
	ofstream outputFile;

	//system("color 0a");
	cout << "Tour Bus Company \n\n";
	
	cout << left << fixed << setprecision(2);
	cout << setw(12) << "Bus No." << setw(10) << "Fuel" << setw(15);
	cout << "Equipment" << setw(10) << "Misc" << setw(20);
	cout << "Discount Fuel" << setw(15) << "Final Cost" << endl;

	inputFile.open("BusInput.txt");
	if (!inputFile)
	{
		cout << "Error opening file";
		return -1;
	}
	 
	while (inputFile >> busNum >> fuel >> equip >> misc) 

	{	
		dFuel = fuel * 0.9;
		finCost = dFuel + equip + misc;

		cout << right << fixed << setprecision(2);
		cout << setw(7) << busNum << setw(9) << fuel << setw(15);
		cout << equip << setw(10) << misc << setw(19);
		cout << dFuel << setw(17) << finCost << endl;
		
	}
	for (int col = 0; col < COL; col++)
	
	
				for (int row = 0; row < ROW; row++)
					total += num[row][col];
		
		cout << "Totals " << total << endl;
		

	cin.ignore(1); // used to pause the system instead of system("pause")
	//system("pause");
	return 0;

}
 
Last edited on
How about starting your "for" with col = 1?
It skips adding the bus numbers to the sum.
What if the input had just "one column" and you would need the sum of the values? You can do that without an array. Same principle applies here.
I need it to be an array. Also, my problem is I can not get any of the columns to output a sum. My only output is one 0 under the first column.
You don't read anything into the array 'num'. Then you nevertheless sum up all the (undefined) values of the array to one variable: 'total'.

When you do:
cout << "Totals " << total << endl;
you do print exactly one value.


You cannot have a 2D array of all the input values, because the first column is int and the rest are double.
You could have 1D array of struct type that has four members: busNum, fuel, equip, misc

Why do you need an array?

1
2
3
4
5
double sFuel = 0.0;
while (inputFile >> busNum >> fuel >> equip >> misc ) {
  sFuel += fuel;
}
std::cout << sFuel << '\n';

See? No array. Sum of specific column.
keskiverto,

I don't need the sum of the first column so maybe that is why the professor set it up that way?
I have actually already done the program with variables set up just like sFuel += fuel; so that I could finish the rest of it.
The array is part of the assignment or I would have just left it with the variables which work without issue. I found the code with the for loops online on several sites as well as in my book. I was unable to make it work that's when I turned to this forum.
I figured it must be an issue somewhere in my code preventing it.
If you do have to have an array:
1
2
3
4
5
6
7
8
9
10
double sFuel = 0.0;
size_t rows = 0;
while ( inputFile >> busNum >> fuel >> equip >> misc && rows < ROW ) {
  num[ rows ][ 1 ] = fuel;
  num[ rows ][ 2 ] = equip;

  sFuel += num[ rows ][ 1 ];
  ++rows;
}
std::cout << sFuel << '\n';


Or in separate stages:
1
2
3
4
5
6
7
8
9
10
11
12
size_t rows = 0;
while ( inputFile >> busNum >> fuel >> equip >> misc && rows < ROW ) {
  num[ rows ][ 1 ] = fuel;
  num[ rows ][ 2 ] = equip;
  ++rows;
}

double sFuel = 0.0;
for ( size_t row = 0; row < rows; ++row ) {
  sFuel += num[ row ][ 1 ];
}
std::cout << sFuel << '\n';

Last edited on
Thank you! I will get to work on that right now!
Topic archived. No new replies allowed.