I am able to add the elements of a row together in C++ and want to add all elements together in all rows and columns. What I have written out adds the the values of rows together but prints out the columns seperately.
#include <iostream>
using namespace std
int main()
{
const int ROW = 10;
const int COL = 20;
int dimenArray[ROW][COL] ={{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}};
for (int i = 0; i < ROW; i++)
{
double totalArray = 0.0;
for (int j = 0; j < COL; j++)
{
totalArray += dimenArray[i][j];
}
cout <<"The total of dimenArray is " << totalArray << "." << endl;
}
return 0;
}
The output I get is.
The total of dimenArray is 55.
The total of dimenArray is 210.
The total of dimenArray is 0.
The total of dimenArray is 0.
The total of dimenArray is 0.
The total of dimenArray is 0.
The total of dimenArray is 0.
The total of dimenArray is 0.
The total of dimenArray is 0.
The total of dimenArray is 0.
I would like to add the 55 and 210 together and produce one value. Any tips are appreciated.
you need another variable for the total across all rows.
double grandTotal = 0.0;
for (int i = 0; i < ROW; i++)
{
double totalArray = 0.0;
for (int j = 0; j < COL; j++)
{
totalArray += dimenArray[i][j];
}
cout <<"The total of dimenArray is " << totalArray << "." << endl;
grandTotal += totalArray;
}
cout << "The total of all rows is " << grandTotal << "." << endl;
return 0;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments