Function will not calculate the correct average? (2D Arrays/Functions)

I suck pretty bad with functions and 2D arrays. I am having a very hard time getting the program to execute the correct average for me. Any advice? Am I calling the function wrong? Am I not writing the function correctly? Do I need to start completely over? ...I'm desperate!

The objective is to create a 3 x 7 array that holds three monkeys and the seven days of the week. The user is to input the data for each monkey. Then the program should:
>Create a report that includes the average amount of food eaten per day by the whole family of monkeys
>The least amount of food eaten during the week by one monkey.
>The greatest amount of food eaten during the week by one monkey.

(I'm not completely finished writing the code by the way)

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const int NUM_MONKEYS = 3;
const int NUM_DAYS = 7;
double food[NUM_MONKEYS][NUM_DAYS];

void getAverage(double array[][NUM_DAYS]);
double getLeast(double array[][NUM_DAYS]);
double getMost(double array[][NUM_DAYS]);

int main()
{

	cout << "Enter the amount of food each monkey ate on each day." << endl;

	for (int monkey = 0; monkey < NUM_MONKEYS; monkey++)
	{
		cout << endl;

		for (int day = 0; day < NUM_DAYS; day++)
		{
			cout << "Monkey #" << (monkey + 1) << ", Day " << 
                        (day + 1) << ": ";

			cin >> food[NUM_MONKEYS][NUM_DAYS];
		}

		cout << endl;
	}

	getAverage(food);
	
	system("PAUSE");
}

void getAverage(double array [][NUM_DAYS])
{
	double average = 0;

	for (int day = 0; day < NUM_DAYS; day++)
	{
		int total = 0;
		for (int monkey = 0; monkey < NUM_MONKEYS; monkey++)
		{
			total += food[monkey][day];	
		}
		average = total / 7;
		cout << "The average amount of food each monkey ate on day " << (day + 1) << " was: " << average;
		cout << endl;
	}

}

double getLeast(double array[][NUM_DAYS])
{
	int leastAmount = array[NUM_MONKEYS][NUM_DAYS];

	for (int monkey = 0; monkey < NUM_MONKEYS; monkey++)
	{
		for (int day = 0; day < NUM_DAYS; day++)
		{
			if (leastAmount > array[monkey][day])
			{
				leastAmount = array[monkey][day];
			}
			cout << endl;
		}
	}
		return leastAmount;
}

double getMost(double array[][NUM_DAYS])
{
	int greatestAmount = array[NUM_MONKEYS][NUM_DAYS];

	for (int monkey = 0; monkey < NUM_MONKEYS; monkey++)
	{
		for (int day = 0; day < NUM_DAYS; day++)
		{
			if (greatestAmount < array[monkey][day])
			{
				greatestAmount = array[monkey][day];
			}
		}
	}

	return greatestAmount;
}
Last edited on
average = total / 7;

total is an int, so total/7 is an int. For example, 13 / 7 is 1. You can fix this by making total double type.

Anyway, if you're supposed to be working out the average amount of food eaten by the monkeys, and there are three monkeys, why are you dividing by seven?
Last edited on
Thank you so much Moschops! And I'm sorry...I knew I was supposed to divide by 3, but the formula wasn't giving me the correct answer, so I changed it to 7 instead...just to see if the answer would change. But unfortunately, the program still gave me zero. I've fixed it now, thanks to you!
Topic archived. No new replies allowed.