How do I Print my array and my sums?

closed account (ENhkSL3A)
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
#include <iostream> 
#include <iomanip>
#include <fstream>

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[11][11];
    float row, column;


    ifstream inputFile;
    inputFile.open("Ex9data.txt");

    for(int countRows = 0; countRows < ROWS; countRows++)
    {
        for(int countColumns = 0; countColumns < COLS; countColumns++)
        {
            inputFile >> numbers[countRows][countColumns];
        }
    }

    inputFile.close();

    for(int i=0;i<10;i++)
    {
        column = 0;
        numbers[10][i]=0;
        for(int j=0;j<10;j++)
        {
            numbers[10][i] +=numbers[j][i];
        }
        //do the printing here & storing... column based sum here.
    }
    for(int i=0;i<10;i++)
    {
        row = 0;
        numbers[i][10]=0;
        for(int j=0;j<10;j++)
        {
        numbers[i][10] +=numbers[i][i];
        }
        //do the printing here & storing... row based sum here.
    }
    return 0;   
}



Lets say the array looks like this:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

I need the sums of the rows and columns to be added to a bottom row and extra column:
1 2 3 4 5 |15
1 2 3 4 5 |15
1 2 3 4 5 |15
--------------
3 6 9 12 15 |30
With Rows = 5, Cols = 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5


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

using namespace std;
int main()
{
    cout << "Kaitlin Stevers" << endl;
    cout << "Exercise 9A - Arrays" << endl;
    cout << "October 31, 2016" <<endl;

    const int ROWS = 5;
    const int COLS = 5;
    float numbers[ROWS][COLS];

    ifstream inputFile;
    inputFile.open("Ex9data.txt");

    for(int countRows = 0; countRows < ROWS; countRows++)
    {
        for(int countColumns = 0; countColumns < COLS; countColumns++)
        {
            inputFile >> numbers[countRows][countColumns];
        }
    }

    inputFile.close();

	int i, j;
	float sumCols;
	float sumRows;
	float sumRowsTotal = 0;
    for(i = 0; i < ROWS; i++)
	{
		sumCols = 0;
		for(j = 0; j < COLS; j++)
		{
			cout << numbers[i][j] << ' ';
			sumCols += numbers[i][j];
		}
		cout << "| " << sumCols;
		cout << endl;
	}

    for(i = 0; i < COLS; i++)
	{
		sumRows = 0;
		for(j = 0; j < ROWS; j++)
		{
			sumRows += numbers[j][i];
		}
		sumRowsTotal += sumRows;
		cout << sumRows << ' ';
	}
	cout << "| " << sumRowsTotal << endl;
    return 0;   
}


Kaitlin Stevers
Exercise 9A - Arrays
October 31, 2016
1 2 3 4 5 | 15
1 2 3 4 5 | 15
1 2 3 4 5 | 15
1 2 3 4 5 | 15
1 2 3 4 5 | 15
5 10 15 20 25 | 75
Press any key to continue


Edit : Farewell, your account is closed so hopefully this is my last help.
Last edited on
Apparently the person above can't read. I'm sorry you are having to deal with them. Also I ran their code and got a bazaar output.
Last edited on
Also I ran their code and got a bazaar output.

What do you mean? Are you having the same assignment as him you might be interested?
No. I'm messaging them. When I ran your code I got this: Kaitlin Stevers
Exercise 9A - Arrays
October 31, 2016
3.11457e+12 4.59163e-41 3.11457e+12 4.59163e-41 3.11458e+12 | 9.34373e+12
4.59163e-41 5.34205e-32 1.4013e-45 0 1.4013e-45 | 5.34205e-32
1.10456e-28 1.4013e-45 1.10455e-28 1.4013e-45 0 | 2.20911e-28
0 0 0 0 0 | 0
0 0 0 0 3.11458e+12 | 3.11458e+12
3.11457e+12 5.34205e-32 3.11457e+12 4.59177e-41 6.22916e+12 | 1.24583e+13
You need to create a file "Ex9data.txt" and paste the following :
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

If the program can access and read the file, it will output the results you should expect.
I did... Maybe it's because I'm using an iMac. I've had trouble before with txt documents... Let me try it in ubuntu.. brb. BTW i sent you a message sakuraouBusters
Last edited on
Topic archived. No new replies allowed.