Katie Stevers - 6 months ago 23

C++ Question

this is my first time posting on here. I am using c++. My assignment is to read in data from a .txt file into a 10 by 10 array. Then I am supposed to add each row and put their total on the end. Then each column needs added and the totals in a new row at the bottom. I also need the sums sum in the corner. Her is an example of what the output has to look like: Example of output screen.

Here is the code I currently have:

`#include <iostream>`

#include <iomanip>

using namespace std;

int main()

{

cout << "Kaitlin Stevers" << endl;

cout << "Exercise 9A - Arrays" << endl;

cout << "October 31, 2016" <<endl;

const int ROWS = 10;

const int COLS = 10;

float numbers[10][10];

ifstream inputFile;

inputFile.open("Ex9data.txt");

int countRows = -1;

int countCols = -1;

while(++countRows < ROWS)

{

countCols = -1;

while(++countCols < COLS)

{

inputFile >> numbers[countRows][countCols];

}

}

inputFile.close();

}

As you can see I figured out how to read the file into an array that is 10 by 10 but I have not figured out how to sum the columns or rows. The book I go by has examples of how to do it but it does not have examples of how to do it with a .txt file being read in. I'm sure you can see why I'm lost! Any who, thanks in advance for the help!

Answer

The term *summing* means to add up numbers. Summing the rows means to add all the numbers in the row.

```
// Pick a row to sum
const unsigned int row_to_sum = 3;
// Declare and initialize a summation variable.
float sum = 0.0f;
// Add all the columns in the given row.
for (unsigned int i = 0; i < COLS; ++i)
{
sum += numbers[row_to_sum][i];
}
```

To sum up the a column, keep the column index constant and iterate the rows.

```
const unsigned int column_to_sum = 8;
sum = 0.0f;
for (unsigned int j = 0; j < ROWS; ++j)
{
sum += numbers[j][column_to_sum];
}
```