//
// main.cpp
// Monkey Final, Week 15
//
// Created by Austin Le on 4/27/19.
// Copyright © 2019 Austin Le. All rights reserved.
// email: le.austin2000@gmail.com
// this program takes in a monkeys eating portions per hour a in a day
// it will then find the total eaten, average per hour, and find the highest amount eaten
// and the lowest eaten at a certain time
#include
#include
using namespace std;
//array declaration with 24 elements, representing 24 hours
const int hours = 24;
double monkeyFood[hours];
//function prototypes
void totalAndAverage(double, double);
void findHighest();
void getGrams();
int main(int argc, const char * argv[]) {
//variables to be returned to in totalAndAverage array
double total = 0;
double average = 0;
cout << "***** Monkey Food Tracker *****" << "\n\n" << endl;
//call functions
getGrams(); //gets users input
totalAndAverage(total, average); //caluclates average and total;
findHighest(); //will find highest and lowest value in array
return 0;
}
//calculates the total and average function
void totalAndAverage(double total = 0, double average = 0)
{
//this loop will go through each element and add them together to get a total
for (int i = 0; i < hours; i ++)
{
total += monkeyFood[i];
}
//takes total and divided by 24 to get grams per hour
average = total / hours;
//outputs the total grams eaten and average grams per hour
cout << setprecision(2) << fixed << endl;
cout << "\nTotal grams eaten in 24 hours: " << total << "g\n" << endl;
cout << "Average grams eaten per hour: " << average << "g\n" << endl;
}
//this function finds the highest and loswest value in the array
void findHighest()
{
//variables that will hold the highest and lowest numbers
double max;
double min;
int location = 0;
int locationTwo = 0;
//initialize max as the first element in the array
max = monkeyFood[0];
//will go through each array and compare the numbers and assign it to the highest one
for (int i = 0; i < hours; i++)
{
if (max < monkeyFood[i])
max = monkeyFood[i];
}
//initialize minimum as the first element in the array
min = monkeyFood[0];
//will go through each array and compare the numbers and assign it to the lowest one
for (int j = 0; j < hours; j++)
{
if (min > monkeyFood[j])
min = monkeyFood[j];
}
//will go through each element to see what index is equivalent to max
for (int i = 0; i < hours; i++)
{
if (max == monkeyFood[i])
{
//assigns index number in array
location = (i + 1);
}
}
//will go through each element to see what index is equivalent to min
for (int i = 0; i < hours; i++)
{
if (min == monkeyFood[i])
{
//assigns the index number in array
locationTwo = (i + 1);
}
}
//out puts the most eaten and least eaten, as well as what specific hour
cout << "Most eaten was " << max << "g" << " at hour: " << location << "\n" << endl;
cout << "Least eaten was " << min << "g" << " at hour: " << locationTwo << "\n" << endl;
}
//function that gets the grams per hour from user
void getGrams()
{
//loop that goes through each element in the array to store value from user
for (int i = 0; i < hours; i++)
{
cout << "Enter the amount of grams (0-100) your monkey ate in hour " << (i + 1) << ": " << endl;
cin >> monkeyFood[i];
//if statement if users input is greater than zero or less than zero
if (monkeyFood[i] > 100 || monkeyFood[i] < 0)
{
//will ask the user to enter a valid number between 0-100
cout << "\nPlease enter a valid numer within the parameters\n" << endl;
cout << "Enter the amount of grams (0-100) your monkey ate in hour " << (i + 1) << ": " << endl;
cin >> monkeyFood[i];
}
}
}