columns and rows

Feb 14, 2019 at 5:17am
I am trying to figure out how I would store the values for the total cols and rows so my print_array function would output them. I am having issues understanding what I am doing wrong.

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <iomanip>
using namespace std;

const int row = 4, col = 5, width = 8;
void format();
void print_array(double n[][col]);
void total_cols(double[][col]);
void total_rows(double[][col]);

int main()
{
	double numbers[row][col] = {{4.2,4.1,6.3,6.2},{5.5,4.6,4.7,5.0},{6.3,5.8,4.6,5.5}};
	format();
	print_array(numbers);

return 0;
}

void print_array(double n[][col])
{
	for (int i = 0; i < row - 1; i++)
	{
		for (int j = 0; j < col; j++)
			cout << setw(width) << n[i][j];
		cout << endl;
	}

	for (int k=0;k<col;k++)
		cout << setw(width) << "-------";
	cout << endl;

	for (int j = 0; j < col; j++)
		cout << setw(width) << n[row - 1][j];
	cout << endl;

}

void format()
{
	cout << setprecision(2);
	cout << setiosflags(ios::fixed | ios::showpoint);
}

void total_cols(double n[][col])
{
	double i, j, total = 0;

	for (i = 0; i < 4; ++i)
	{
		for (j = 0; j < 4; ++j)
		{
			total = total + arr[i][j];
		}
		cout << i << "=" << total << endl;

		total = 0;
	}
}

}

void total_rows(double n[][col])
{
	double i, j, total = 0;

	for (i = 0; i < 4; ++i)
	{
		for (j = 0; j < 4; ++j)
		{
			total = total + arr[i][j];
		}
		cout << i << "=" << total << endl;

		total = 0;
	}
}
Last edited on Feb 14, 2019 at 5:17am
Feb 14, 2019 at 5:53am
Are you trying to end up with something like this?

1 2 = 3
3 4 = 7
--------
4 6
Feb 14, 2019 at 6:07am
yep! exactly!
Feb 14, 2019 at 6:37am
Something like this (untested).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void print_array(double n[][col])
{
	double colTotals[col] = { 0 };
	for (int r = 0; r < row; r++)
	{
		double rowTotal = 0.0;
		for (int c = 0; c < col; c++) {
			cout << setw(width) << n[r][c];
			rowTotal += n[r][c];
			colTotals[c] += n[r][c];
		}
		cout << setw(width) << rowTotal << endl;
	}

	for (int c = 0; c < col; c++)
		cout << setw(width) << "-------";
	cout << endl;

	for (int c = 0; c < col; c++)
		cout << setw(width) << colTotals[c];
	cout << endl;
}

Feb 14, 2019 at 9:22am
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
#include <iostream>
#include <iomanip>
#include <string>
#include <valarray>
using namespace std;

const int WIDTH = 8;
#define SW << setw( WIDTH ) <<


template <typename T> ostream &operator << ( ostream &strm, const valarray<T> &V )
{
   for ( T e : V ) strm SW e << " ";
   return strm;
}


int main()
{
   valarray<double> A = { 4.2, 4.1, 6.3, 6.2,   5.5, 4.6, 4.7, 5.0,   6.3, 5.8, 4.6, 5.5 };
   int row = 3, col = A.size() / row;

   cout << fixed << setprecision( 1 );
   for ( int n = 0; n < A.size(); n += col )
   {
      valarray<double> ROW = A[slice(n,col,1)];
      cout << ROW << " | " SW ROW.sum() << '\n';
   }
   cout << string( col * ( WIDTH + 1), '-' ) << '\n';
   for ( int c = 0; c < col; c++ )
   {
      valarray<double> COL = A[slice(c,row,col)];
      cout SW COL.sum() << " ";
   }
   cout << '\n';
}


     4.2      4.1      6.3      6.2  |     20.8
     5.5      4.6      4.7      5.0  |     19.8
     6.3      5.8      4.6      5.5  |     22.2
------------------------------------
    16.0     14.5     15.6     16.7 


Last edited on Feb 14, 2019 at 1:30pm
Feb 14, 2019 at 11:01pm
I appreciate the help everyone, I was kind of hoping to figure out how I can fix the code I wrote rather than get a whole new code. I am doing something wrong just not sure how to fix it. I think the print_array function is correct but the total_cols and total_rows are in need of some fixing, I could be wrong though. The comment from my professor said "How would you store those values back in the array so the print_array function would output them?" I am not sure the answer.
Last edited on Feb 14, 2019 at 11:26pm
Feb 14, 2019 at 11:33pm
Your code doesn't compile.

Make sure you call your arrays consistent things within one function. You can't call them n[][] on lines 45 and 63, then arr[][] on lines 53 and 71.

Also i < 4 doesn't correspond to your number of rows, and there isn't much logic to writing i=total.

From what your professor is saying, and the fact that your declared size of array has a spare row and column, I presume he wants you to put the row totals in the last column and the column totals in the last row, rather than printing them out separately.
Last edited on Feb 14, 2019 at 11:40pm
Feb 15, 2019 at 2:07am
So I definitely understand my inconsistency of the arrays, I thank you for pointing that out. When I thange arr to n int the total_cols and total_rows functions I am getting an error that says an expression must have an intergral or an unscoped enum type attached to n[i][j]. Not sure what its meaning.

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <iomanip>
using namespace std;

const int row = 4, col = 5, width = 8;
void format();
void print_array(double n[][col]);
void total_cols(double[][col]);


int main()
{
	double numbers[row][col] = { { 4.2,4.1,6.3,6.2 },{ 5.5,4.6,4.7,5.0 },{ 6.3,5.8,4.6,5.5 } };
	format();
	print_array(numbers);

	return 0;
}

void print_array(double n[][col])
{
	for (int i = 0; i < row - 1; i++)
	{
		for (int j = 0; j < col; j++)
			cout << setw(width) << n[i][j];
		cout << endl;
	}

	for (int k = 0; k<col; k++)
		cout << setw(width) << "-------";
	cout << endl;

	for (int j = 0; j < col; j++)
		cout << setw(width) << n[row - 1][j];
	cout << endl;

}

void format()
{
	cout << setprecision(2);
	cout << setiosflags(ios::fixed | ios::showpoint);
}

void total_cols(double n[][col])
{
	double i, j, total = 0;

	for (i = 0; i < 5;i++)
	{
		for (j = 0; j < 5; ++j)
		{
			total = total + n[i][j];
		}
		cout << i << "=" << total << endl;

		total = 0;
	}
}
void total_rows(double[][col])
{
	double i, j, total = 0;

	for (i = 0; i < 4; i++)
	{
		for (j = 0; j < 4; ++j)
		{
			total = total + n[i][j];
		}
		cout << i << "=" << total << endl;

		total = 0;
	}
}
Feb 15, 2019 at 6:15am
In both of your totalling routines you have declared i, j to be doubles. If they are array indices they should be ints. In fact, a good place to declare them as such is in the "for" statements themselves (lines 49, 51, 64 and 66), just as was done in routine print_array.

Array n[][] is not declared on line 60.

In neither totalling routine do your loop ranges reflect the actual size of your array.

You are not doing what your professor asked - re-read my last post.
Last edited on Feb 15, 2019 at 9:45am
Topic archived. No new replies allowed.