arrays issue

Im having a problem with this program that uses a multidimensional array. Everything works as expected except the minimum amount of food always spits out a massive negative number regardless of what the greatest input was.

heres a description of the problem:

A local zoo wants to keep track of how many pounds of its three monkeys eats each day during a typical week. Write a program that stores this information in a two dimensional 3 x 7 array, where each row represents a different monkey and each column represents a different day of the week. The program should first have the user input the data for each monkey. Then it should create a report that includes the following information:

- Average amount of food eaten per day by the whole family of monkeys.
- The least amount of food eaten during the week by any one monkey.
- The greatest amount of food eaten during the week by any one monkey.

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
 #include<iostream>
using namespace std;

int main()
{
	const int MONKEYS = 3;
	const int DAYS = 7;
	int food[MONKEYS][DAYS];
	int maximum = food[0][0];
	int minimum = food[0][0];
	float total = 0.0f;
	float average = 0.0f;


	cout << "Enter the amount of food consumed for each monkey on each day: " << endl;

	//get data from user and sum it
	for (int monk = 0; monk < MONKEYS; monk++)
	{
		for (int day = 0; day < DAYS; day++)
		{
			cout << "Monkey " << (monk + 1) << ", day " << (day + 1) << ": ";
			cin >> food[monk][day];

			total += food[monk][day];

		}
		cout << endl;
	}
	//calculate average
	average = total / (MONKEYS * DAYS);

	//get most eaten
	for (int monk = 0; monk < MONKEYS; monk++)
	{
		for (int day = 0; day < DAYS; day++)
		{
			if (food[monk][day] > maximum)
				maximum = food[monk][day];
		}

	}

	//get least eaten
	for (int monk = 0; monk < MONKEYS; monk++)
	{
		for (int day = 0; day < DAYS; day++)
		{
			if (food[monk][day] < minimum)
				minimum = food[monk][day];
		}

	}

	cout << "            Report" << endl;
	cout << "-----------------------------" << endl;
	cout << endl;
	cout << "The total amount of food consumed is " << total << endl;
	cout << "The average amount of food consumed by the monkeys each day is " << average << endl;
	cout << "The most eaten by a monkey is " << maximum << endl;
	cout << "The least eaten by a monkey is " << minimum << endl;


	system("PAUSE");
	
	return 0;
}
1
2
3
int food[MONKEYS][DAYS];
	int maximum = food[0][0];
	int minimum = food[0][0];


1
2
if (food[monk][day] > maximum)
				maximum = food[monk][day];


1
2
if (food[monk][day] < minimum)
				minimum = food[monk][day];


You set your max and min values from uninitialized array value therefore they are undefined themselves. Then you use them in an expression with their still undefined values therefore the expression is undefined.

EDIT : The only reason maximum worked is because the undefined value happened to meet the requirements of the if statement, else it would have failed also.
Last edited on
fixed it, thanks.

One thing that still has me puzzled though: why did the maximum work? it was also undefined.
Last edited on
see edit
Before you calculate the minimum amount you have to add this line of code:

minimum = maximum;

Because when you initialized minimum you set it equal to food[0][0] which wasn't initialized in itself, hence the large negative number.

I'd recommend initializing both maximum and minimum with 0 and then later on assigning minimum to maximum.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
        //get most eaten
	for (int monk = 0; monk < MONKEYS; monk++)
	{
		for (int day = 0; day < DAYS; day++)
		{
			if (food[monk][day] > maximum)
				maximum = food[monk][day];
		}

	}

	//get least eaten
	minimum = maximum; // <- This line is important!
	for (int monk = 0; monk < MONKEYS; monk++)
	{
		for (int day = 0; day < DAYS; day++)
		{
			if (food[monk][day] < minimum)
				minimum = food[monk][day];
		}

	}
Last edited on
Before you calculate the minimum amount you have to add this line of code:

minimum = maximum;

Because when you initialized minimum you set it equal to food[0][0] which wasn't initialized in itself, hence the large negative number.

I'd recommend initializing both maximum and minimum with 0 and then later on assigning minimum to maximum.


still bad they are both undefined there is no guarantee that maximum will get set properly either so setting minimum = to maximum is not helping. But setting them both to zero will fix this. Assuming food eaten is more than zero which it should be or this would be an animal welfare violation
Last edited on
CodeGoggles:
still bad they are both undefined there is no guarantee that maximum will get set properly...

I'd recommend initializing both maximum and minimum with 0 and then later on assigning minimum to maximum.

I don't see a flaw in setting minimum to maximum in any case, because minimum is always going to be less or equal to maximum. Provided (of course), that maximum was initialized to 0.

Of course, if you wanted to get really picky, you could #include <limits> and init minimum and maximum like so:

1
2
int minimum = std::numeric_limits<int>::max();
int maximum = std::numeric_limits<int>::min();
Last edited on
Provided (of course), that maximum was initialized to 0.


And that the monkeys actually got fed. :P poor monkeys

Of course, if you wanted to get really picky


pickyness <-- hehe my new word. Is what programmers are good at without pickyness there is room for error/s.
Last edited on
Topic archived. No new replies allowed.