Passing 2D array to a function

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.

#include <iostream>
#include <iomanip>
#include <array>
#include <vector>
using namespace std;

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

averageFood = average(monkeyFood);




return 0;
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.

1. As jonnin said, it should be int average(int monkeyFood[numRows][numColumns])

2. The code as posted won't compile after the change, there is a missing closing bracket after return 0;

3.
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

4. Relying on global variables way too much. x an y should not be globals, they should be defined locally within each loop.

Using globals the way they are used can create to hard-to-find bugs.

5. M'ok, you went to all the trouble to calculate an average amount of food, and don't do anything with that value before exiting.

6. Having so many blanks lines spacing out lines of code makes reading and understanding the code harder than it should be.
7. Why are you including headers you don't need?

You are not using std::array or std::vector in your code, so including the headers is useless.

I don't see where you use any of the I/O manipulators as well.

The only standard library header you use is <iostream>.
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?"
Personally I prefer to use std::vector instead of regular array, even 2D.

Passing a vector to a function the vector retains its size, a regular array devolves to a pointer.

Because a passed vector retains its size that means you can pass any size vector you want.

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
#include <iostream>
#include <vector>

void print(const std::vector<int>&);
void print(const std::vector<std::vector<int>>&);

int main()
{
   std::vector<int> v1;

   print(v1);

   v1 = { 1, 3, 5, 9, 12 };

   print(v1);

   std::vector<std::vector<int>> v2 { { 1, 2 }, { 3, 4, 8 },  { 3 }, { 8, 9 } };

   print(v2);
}

void print(const std::vector<int>& vec)
{
   std::cout << "The 1D vector's size is: " << vec.size() << '\n';
}

void print(const std::vector<std::vector<int>>& vec)
{
   std::cout << "The 2D vector has " << vec.size() << " rows\n";
   
   for (size_t i { }; i < vec.size(); ++i)
   {
      std::cout << "Row #" << i << " has " << vec[i].size() << " columns.\n";
   }
}
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.

You can increase or decrease the number of stored elements in a vector at run time as well.
https://en.cppreference.com/w/cpp/container/vector
Perhaps something like:

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

constexpr size_t numRows {3};
constexpr size_t numColumns {5};

int average(const int 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(const int 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(const int 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';
}

Topic archived. No new replies allowed.