Need help with source code

Hi, I'm really new at this and would really appcreciate if someone could help me with my source code for this assignment. Here's the details:

YOU MUST create a function, monkeyFood, that will load the 3x7 array (the array MUST store real numbers); you will pass the array you declare in main as an argument into the function.

Psuedo Code for function monkeyFood:
1. Create for loop to go through each row (each monkey)
a. Create another for loop (nested) to go through each day of the week
- Ask user for amount of food in pounds for a given day (DO NOT except Negative Values)
- Read in the food amount from the user

Next you will create 3 more functions aveFood, leastFood, and greatestFood that will calculate the average amount of food for a given monkey, the least amount of food given during the week, and the greatest amount of food.

Your program must first ask the user to enter the amount of food for each day for all three monkeys and then create a Report (output to the screen) which contains the average amount of food eaten per day by the whole family of monkeys (i.e. average for all three monkeys for each day of the week); The least amount of food eaten during the week by any one monkey and the greatest amoun of food eaten by any one monkey. .

And here's what I have:
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;


// Function prototypes
double AvgArray(double [], int);
double getHighest(double[], int);
double getLowest(double[], int);

int main(int argc, char *argv[])
{
    const int NUM_DAYS = 7; // Number of days
    const int NUM_MONKEYS = 3; //Number of monkeys
    double food[NUM_DAYS], 
    total,
    average,
    highest, 
    lowest;  
    
    

    
   cout << "\t\t\tMonkey Business" << endl;
   cout << endl;
   

   
   cout << " \tEnter the number of pounds eaten for monkey # 1\n";
    for (int day = 0; day < NUM_DAYS; day++)
   {
        cout << " \tEnter the number of pounds eaten for monkey # 2\n";
          for (int day = 0; day < NUM_DAYS; day++)
          {
              cout << " \tEnter the number of pounds eaten for monkey # 3\n";
              for (int day = 0; day < NUM_DAYS; day++)
              {
                  for (int day = 0; day < NUM_DAYS; day++)
                  {
                      cout << " \tDay " << (day + 1) <<": ";
                      cin >> food[day];
                      average = total / NUM_DAYS;
                  }
              }
          }
   }
   
   cout << endl;
  
  cout << " \tEnter the number of pounds eaten for monkey # 2\n";
   
   
   for (int day = 0; day < NUM_DAYS; day++)
   {
         cout << " \tDay " << (day + 1) <<": ";
         cin >> food[day];
         average = total / NUM_DAYS;
   }
   
   cout << endl;
   
  cout << " \tEnter the number of pounds eaten for monkey # 3\n";
   
   for (int day = 0; day < NUM_DAYS; day++)
   {
         cout << " \tDay " << (day + 1) <<": ";
         cin >> food[day];
         average = total / NUM_DAYS;
   }
  
   // Get total
   total += AvgArray(food, NUM_DAYS);
   

   // Get highest and lowest lbs
   highest = getHighest(food, NUM_DAYS);
   lowest = getLowest(food, NUM_DAYS);
   average = AvgArray (food, NUM_DAYS);

   // Display results
    cout << fixed << showpoint << setprecision(2) << endl;
    
    cout << " \tThe total amount of food eaten per day by the" << endl;
    cout << " \twhole family of monkeys is: " << total << " lbs" << endl;
    cout << endl;
    
    cout << " \tThe average amount of food eaten per day by the" << endl;
    cout << " \twhole family of monkeys is: " << average << " lbs" << endl;
    cout << endl;
    
    cout << " \tThe most amount of food eaten during the week by" << endl; 
    cout << " \tany one monkey is: " << highest << " lbs" << endl;
    cout << endl;
    cout << " \tThe least amount of food eaten during the week by" << endl;
    cout << " \tany one monkey is: " << lowest << " lbs" << endl;
    cout << endl;
    
        
    system("PAUSE");
    return EXIT_SUCCESS;
}


//****************************************************
// Definition of AvgArray *
// This function computes and returns the sum of the *
// values in the double array passed to it. *
//****************************************************

 double AvgArray(double array[], int size)
{
   double total = 0; // Accumulator

   for (int count = 0; count < size; count++)
   total += array[count];
   return total;
   
}

  //*****************************************************
  // Definition of getHighest *
  // This function finds and returns the largest value *
  // in the double array passed to it. *
  //*****************************************************
   double getHighest(double array[], int size)
{
   double highest = array[0];

   for (int count = 1; count < size; count++)
   { 
       if (array[count] > highest)
       highest = array[count];
   }
   return highest;
}

//*****************************************************
// Definition of getLowest *
// This function finds and returns the smallest value *
// in the double array passed to it. *
//*****************************************************

    double getLowest(double array[], int size)
{
    double lowest = array[0];

    for (int count = 1; count < size; count++)
    { 
        if (array[count] < lowest)
        lowest = array[count];
    }
    return lowest;
}


But it keeps crashing :( Can you please help???
This sections of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 cout << " \tEnter the number of pounds eaten for monkey # 1\n";
    for (int day = 0; day < NUM_DAYS; day++)
   {
        cout << " \tEnter the number of pounds eaten for monkey # 2\n";
          for (int day = 0; day < NUM_DAYS; day++)
          {
              cout << " \tEnter the number of pounds eaten for monkey # 3\n";
              for (int day = 0; day < NUM_DAYS; day++)
              {
                  for (int day = 0; day < NUM_DAYS; day++)
                  {
                      cout << " \tDay " << (day + 1) <<": ";
                      cin >> food[day];
                      average = total / NUM_DAYS;
                  }
              }
          }
   }


runs the line cin >> food[day]; 7*7*7*7 = 2401 times. I doubt very much that you intend the user to enter 2401 values, given that there are only 7 days and 3 monkeys. Looks like some kind of cut and paste fiasco. Is it meant to be:

1
2
3
4
5
6
7
8
9
10
11
cout << " \tEnter the number of pounds eaten for monkey # 1\n";
   
   
   for (int day = 0; day < NUM_DAYS; day++)
   {
         cout << " \tDay " << (day + 1) <<": ";
         cin >> food[day];
         average = total / NUM_DAYS;
   }
   
   cout << endl;



That aside, when you take in the data for monkey two, you are writing over the top of the data you took in for monkey 1, and then you're doing the same thing again when you take in the data for monkey 3. You also write over the previously calculated avlue of avarage.

Since you have 3 monkeys, shouldn't you have three storage arrays? One for each monkey. And perhaps three average values? One for each monkey.

Your AvgArray function (which should be name aveFood according to the instructions) does not calculate an average. It calculates a sum.
Last edited on
Oh ok, so now I have this:

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;


// Function prototypes
double aveFood(double [], int);
double getHighest(double[], int);
double getLowest(double[], int);

int main(int argc, char *argv[])
{
    const int NUM_DAYS = 7; // Number of days
    const int NUM_MONKEYS = 3; //Number of monkeys
    double food[NUM_DAYS], 
    total,
    average,
    highest, 
    lowest;  
    
    

    
   cout << "\t\t\tMonkey Business" << endl;
   cout << endl;
   

   
   cout << " \tEnter the number of pounds eaten for monkey # 1\n";
   
   
  
   // Get the sales data
   for (int day = 0; day < NUM_DAYS; day++)
   {
         cout << " \tDay " << (day + 1) <<": ";
         cin >> food[day];
         average = total / NUM_DAYS;
   }
   
   cout << endl;
  
  cout << " \tEnter the number of pounds eaten for monkey # 2\n";
   
   
   for (int day = 0; day < NUM_DAYS; day++)
   {
         cout << " \tDay " << (day + 1) <<": ";
         cin >> food[day];
         average = total / NUM_DAYS;
   }
   
   cout << endl;
   
  cout << " \tEnter the number of pounds eaten for monkey # 3\n";
   
   for (int day = 0; day < NUM_DAYS; day++)
   {
         cout << " \tDay " << (day + 1) <<": ";
         cin >> food[day];
         average = total / NUM_DAYS;
   }
  
   // Get total
   total += aveFood(food, NUM_DAYS);
   

   // Get highest and lowest lbs
   highest = getHighest(food, NUM_DAYS);
   lowest = getLowest(food, NUM_DAYS);
   average = aveFood (food, NUM_DAYS);

   // Display results
    cout << fixed << showpoint << setprecision(2) << endl;
    
    cout << " \tThe total amount of food eaten per day by the" << endl;
    cout << " \twhole family of monkeys is: " << total << " lbs" << endl;
    cout << endl;
    
    cout << " \tThe average amount of food eaten per day by the" << endl;
    cout << " \twhole family of monkeys is: " << average << " lbs" << endl;
    cout << endl;
    
    cout << " \tThe most amount of food eaten during the week by" << endl; 
    cout << " \tany one monkey is: " << highest << " lbs" << endl;
    cout << endl;
    cout << " \tThe least amount of food eaten during the week by" << endl;
    cout << " \tany one monkey is: " << lowest << " lbs" << endl;
    cout << endl;
    
        
    system("PAUSE");
    return EXIT_SUCCESS;
}


//****************************************************
// Definition of AvgArray *
// This function computes and returns the sum of the *
// values in the double array passed to it. *
//****************************************************

 double aveFood(double array[], int size)
{
   double total = 0; // Accumulator

   for (int count = 0; count < size; count++)
   total += array[count];
   return total;
   
}

  //*****************************************************
  // Definition of getHighest *
  // This function finds and returns the largest value *
  // in the double array passed to it. *
  //*****************************************************
   double getHighest(double array[], int size)
{
   double highest = array[0];

   for (int count = 1; count < size; count++)
   { 
       if (array[count] > highest)
       highest = array[count];
   }
   return highest;
}

//*****************************************************
// Definition of getLowest *
// This function finds and returns the smallest value *
// in the double array passed to it. *
//*****************************************************

    double getLowest(double array[], int size)
{
    double lowest = array[0];

    for (int count = 1; count < size; count++)
    { 
        if (array[count] < lowest)
        lowest = array[count];
    }
    return lowest;
}


But why is it still not working? I'm sorry for taking up your time, I just want to get this right.
Try to turn on more warnings. My compiler warns me that total is uninitialized. Could that be a problem?
When you take in the data for monkey two, you are writing over the top of the data you took in for monkey 1, and then you're doing the same thing again when you take in the data for monkey 3.

Here, this is the line that stores the monkey1 data:
cin >> food[day];
and this is the line that stores the monkey2 data:
cin >> food[day];

You store monkey2 data in exactly the same place as the first set of data. When you do this, you overwrite the first set of data. Given that you need to hold onto that data to calculate monkey1's average food, you need to think of a way to not do this. Lucikly, your instructions already specify: the 3x7 array .

This: double food[NUM_DAYS], is not a 3x7 array. Change it so that it is a 3x7 array.

Your AvgArray function (which should be name aveFood according to the instructions) does not calculate an average. It calculates a sum. Make it calculate an average.
To make it a 3x7 array would it be double array[3][7];
Yes, although since you have some nice constants hanging around it might be nicer to use:

double array[NUM_MONKEYS][NUM_DAYS];
So, I would replace double food[NUM_DAYS] with double array[NUM_MONKEYS][NUM_DAYS]?
Only if you want the array to be named "array". If you'd rather stick with naming if "food", which seems sensible, replace double food[NUM_DAYS] with double food[NUM_MONKEYS][NUM_DAYS]
Topic archived. No new replies allowed.