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???