I am working on a Homework assignment and have to pass a 2D array to a function. Can anyone help me with the correct syntax on how to do this? I keep getting errors from the average function.
int x;
int y;
const int numRows = 3;
const int numColumns = 5;
int average(int *monkeyFood[numRows][numColumns]){
int aveFood = 0; //declare and initialize integer for average
for (x = 0; x < numRows; x++){ //sum all elements of the array
for (y = 0; y < numColumns; y++){
aveFood = aveFood + monkeyFood[x][y];
}
}
aveFood = aveFood / (numRows * numColumns); //calculate and return the average
return aveFood;
}
int least(int array[numRows][numColumns]){
int min = array[0][0]; //declare and initialize integer with 1st array element
for (x = 0; x < numRows; x++){ //iterate through all elements of the array
for (y = 0; y < numColumns; y++){
if (min > array[x][y]){
min = array[x][y];
}
}
}
return min; //return the value for min
}
int greatest(int array[numRows][numColumns]){
int max = 0; //declare and initialize integer for max
for (x = 0; x < numRows; x++){ //iterate through all elements of the array
for (y = 0; y < numColumns; y++){
if (max < array[x][y]){
max = array[x][y];
}
}
}
return max; //return the value for max
}
int main() {
int userInput;
int averageFood;
int leastFood;
int greatestFood;
int monkeyFood[numRows][numColumns];
for (x = 0; x < numRows; x++){
for (y = 0; y < numColumns; y++){
cout << "Input the food eaten by Monkey " << x + 1 << " on day " << y + 1 << endl;
cin >> userInput;
monkeyFood[x][y] = userInput;
}
}
remove the *
and I think it will work.
consider +=
x +=y; //same as x = x+y
same for all the arithmetic and logic to modify the left with itself on the right ...
so average /= totalcount and so on
its not wrong to be explicit, just redundant.
Thank you! I can't believe I hadn't tried that yet, I was pretty sure I had tried everything I could think of. George P, thank you for taking the time to write such a detailed response. A few of the things you pointed out are because I haven't finished the project yet but much of your advice is incredibly helpful and I'm sure will help me write cleaner, more efficient code. I read about code tags, and will definitely use them in the future. I appreciate your time.
Including headers you don't need isn't wrong, as such, it won't affect the compile time, nor change the size and speed of the resulting executable. It is just excess typing and looks sloppy.
Adding excess blank lines also looks IMO sloppy.
Even quick throw-away code snippets I write I try to format them consistently. No excess blank lines, indentation, etc.
I try to write code that 6 months from now I can still read and understand, too often older code can be "just what the hell was I thinking and what the fork is this?"
The 1D vector's size is: 0
The 1D vector's size is: 5
The 2D vector has 4 rows
Row #0 has 2 columns.
Row #1 has 3 columns.
Row #2 has 1 columns.
Row #3 has 2 columns.
#include <iostream>
constexpr size_t numRows {3};
constexpr size_t numColumns {5};
int average(constint monkeyFood[numRows][numColumns]) {
int aveFood {};
for (size_t x {}; x < numRows; ++x)
for (size_t y {}; y < numColumns; ++y)
aveFood += monkeyFood[x][y];
return aveFood / (numRows * numColumns);
}
int least(constint array[numRows][numColumns]) {
int min {array[0][0]};
for (size_t x {}; x < numRows; ++x)
for (size_t y {}; y < numColumns; ++y)
if (min > array[x][y])
min = array[x][y];
return min;
}
int greatest(constint array[numRows][numColumns]) {
int max {array[0][0]};
for (size_t x {}; x < numRows; ++x)
for (size_t y {}; y < numColumns; ++y)
if (max < array[x][y])
max = array[x][y];
return max;
}
int main() {
int monkeyFood[numRows][numColumns];
for (size_t x {}; x < numRows; ++x)
for (size_t y {}; y < numColumns; ++y) {
std::cout << "Input the food eaten by Monkey " << x + 1 << " on day " << y + 1 << ": ";
std::cin >> monkeyFood[x][y];
}
std::cout << "Average food eaten is " << average(monkeyFood) << '\n';
std::cout << "Greatest food eaten is " << greatest(monkeyFood) << '\n';
std::cout << "Least food eaten is " << least(monkeyFood) << '\n';
}