sum and average in multidimensional array

Working on an assignment. The requirement is to had a multidimensional array with 5 rows and 5 columns. The end goal is to have the program output the sum and average of each individual row. I got this far, and I believe it is correct so far. I just cannot figure out how to output the required way. Another requirement is to have the addition and average coding take place in its own function. Thanks in advance. I am new to C++ and trying to absorb all this but cannot seem to find the next thing. Can someone point me in the right direction.

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
  #include "stdafx.h"
#include <iostream>
using namespace std;



int input[5][5]; // [ROW][COLUMN]; 5 Rows and 5 Columns
int main()
{
	cout << "This program will calculate the sum and average"
		<< "of the values in each row. There will be five rows"
		<< "and five columns" << endl;
	
	cout << "Please Enter the array values. " << endl;
	for (int i = 0; i < 5; i++){
		for (int j = 0; j < 5; j++) {
			cin >> input[i][j];
			}
		}

	cout << "Row 1" << endl;
	cout << "Total = " << sum << endl;
	cout << "Average = " << average << endl;
	
	cout << endl;
    return 0;
}
int sumRow(int input[5][5])
	{
	int sum = 0;
	for (int i = 0; i < 5; ++i)
		sum += input[i][5];
	return sum;
}
int average(int sum)
{
	return sum / 5;
}
Last edited on
This is the actual requirements for the assignment

Create a two-dimensional array
int input[5][5]; // [row][column]; 5 rows and 5 columns

Generate 25 random numbers to populate the two-dimensional array. Please use a separate function to calculate total and average of 5 integers in each row (one function for total, one function for average) and output the results using below format.
Row 1
Total =
Average =

Row 2
Total =
Average =

Row 3
Total =
Average =

Row 4
Total =
Average =

Row 5
Total =
Average =
Okay, I changed a little and now the program will run, but the sum and average are way off

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
#include "stdafx.h"
#include <iostream>
using namespace std;


int const rows = 5;
int const cols = 5;
int input[rows][cols]; // [ROW][COLUMN]; 5 Rows and 5 Columns

int sumRow(int input[rows][cols])
{
	int sum = 0;
	for (int Row = 0; Row < rows; ++Row)
		sum += input[rows][cols];
	return sum;
}

int average(int sum, int rows)
{
	return sum / cols;
}

int main()
{
	cout << "This program will calculate the sum and average"
		<< "of the values in each row. There will be five rows"
		<< "and five columns" << endl;
	
	cout << "Please Enter the array values. " << endl;
	for (int Row = 0; Row < cols; Row++){
		for (int Col = 0; Col < cols; Col++) {
			cin >> input[rows][cols];
			}
		}

	cout << "Row 1" <<  endl;
	cout << "Total = " << sumRow << endl;
	cout << "Average = " << average << endl;

    return 0;
}

also to have the code automatically change the row so I don't have to keep writing it, would I use a loop for that?
I’m sure you’ve already read here:
http://www.cplusplus.com/forum/beginner/1/
Don't post homework questions
Programmers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions.


- - -

would I use a loop for that?

Definitely.

Your assignment is so self-explanatory that’s hard to give further hints. You just need to follow the instruction as you can read them.

It seems you are a bit confused about global variables and function parameters.
From where you can see (= access) a global variable?
Why do we declare parameters for functions? What’s their purpose?
If you can access a variable because it’s global, do you need to pass it to functions anyway?
If you ‘pass’ a variable to a function, does it need to be global?

What about function returning values? What’s their purpose?

And what about random values generator? Do you know about it?

Please have a second look to your code:
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
// This is the actual requirements for the assignment
// Create a two-dimensional array
// int input[5][5]; // [row][column]; 5 rows and 5 columns
// Generate 25 random numbers to populate the two-dimensional array. Please use a
// separate function to calculate total and average of 5 integers in each row
// (one function for total, one function for average) and output the results
// using below format.
#include <iostream>

int const rows = 5;
int const cols = 5;

int input[rows][cols]; // <--- Here you declare your bidimensional array global
int sumRow(int input[rows][cols]) // <-- here it seems you want to pass it to a function
{
    int sum = 0;
    for (int Row = 0; Row < rows; ++Row) {
        sum += input[rows][cols];
    }
    return sum;
}

int average(int sum, int rows) // where do you use parameter "rows" ?
{
    return sum / cols;  // <-- this is the global "cols"
}

int main()
{
    std::cout << "This program will calculate the sum and average "
                 "of the values in each row.\nThere will be five rows "
                 "and five columns.\n";

    // ASSIGNMENT: Generate 25 random numbers to populate the two-dimensional
    //             array.
    // 1 -- initialize random number generator
    // ...

    for (int Row = 0; Row < cols; Row++){
        for (int Col = 0; Col < cols; Col++) {
            input[rows][cols] = // random number
        }
    }

    // ASSIGNMENT: output the results using below format:
    //             Row 1        1
    //             Total =      use a separate function to calculate total...
    //             Average =    ...and average of 5 integers
    //
    //             Row 2        2
    //             Total =      use a separate function to calculate total...
    //             Average =    ...and average of 5 integers
    //             ...
    
    // So you need to get through each row and calculate the sum of all the
    // columns; then start again by the following row.
    // What you need is... ?
    // You also need a number which start from 1 and grows up to 5, so what sort
    // of ############ would you choose?

    return 0;
}

Topic archived. No new replies allowed.