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;
}
|