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
|
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int SIZE = 7;
void getMonkeyFeedData(double[][SIZE]);
void avgFeedPerMonkey(double[][SIZE], double[]);
double averageMonkeyFeed(double [][SIZE]);
void highestMonkeyFeed(double[], int&);
void lowestMonkeyFeed(double[], int&);
void displayResults(double[], double, int, int, double[][SIZE]);
ifstream inFile;
ofstream outFile;
int main()
{
double monkeyFeed[SIZE][SIZE];
double averageFeedPerMonkey[SIZE];
int highest = 0;
int lowest = 0;
double average = 0;
inFile.open("monkeyFile.txt");
outFile.open("Monkey Feed Report.txt");
getMonkeyFeedData(monkeyFeed);
avgFeedPerMonkey(monkeyFeed, averageFeedPerMonkey);
average = averageMonkeyFeed(monkeyFeed);
highestMonkeyFeed(averageFeedPerMonkey, highest);
lowestMonkeyFeed(averageFeedPerMonkey, lowest);
displayResults(averageFeedPerMonkey, average, highest, lowest, monkeyFeed);
inFile.close();
outFile.close();
return 0;
}
void displayResults(double averageFeedPerMonkey[], double avgMonkeyFeed, int highest, int lowest, double monkeyFeed[][SIZE]) // takes all data from main then displays results in a report
{
outFile << "\n---------------------- RESULTS ----------------------\n";
outFile << endl << endl;
outFile << "\t Monkeys" << endl;
outFile << "Days";
for(int i = 0; i < SIZE; i++)
{
for(int j = 0; j < SIZE; j++)
{
outFile << "\t" << monkeyFeed[i][j] << " ";
}
outFile << endl << endl;
}
for (int i = 0; i < SIZE; i++)
{
outFile << "The average food per day for monkey # " << i + 1 << ": " << fixed << setprecision(2) << averageFeedPerMonkey[i] << endl;
}
outFile << "\nThe average food consumed by all the monkeys each day is: " << avgMonkeyFeed << endl;
outFile << "\nThe monkey that ate the most food for the week: " << highest << endl;
outFile << "\nThe monkey that ate the least food for the week: " << lowest << endl;
outFile << endl << endl;
}
void lowestMonkeyFeed(double averageFeedPerMonkey[], int &monkey) // Takes 2D array and uses a nested for loop to to find the lowest feed amounts
{
double lowest = averageFeedPerMonkey[0];
monkey = 1;
for (int i = 0; i < SIZE; i++)
{
if (averageFeedPerMonkey[i] < lowest)
{
lowest = averageFeedPerMonkey[i];
monkey = i + 1;
}
}
}
void highestMonkeyFeed(double averageFeedPerMonkey[SIZE], int &monkey) // Takes 2D array and uses a nested for loop to to find the highest feed amounts
{
double highest = averageFeedPerMonkey[0];
monkey = 1;
for (int i = 0; i < SIZE; i++)
{
if (averageFeedPerMonkey[i] > highest)
{
highest = averageFeedPerMonkey[i];
monkey = i + 1;
}
}
}
double averageMonkeyFeed(double monkeyFeed[][SIZE]) // Takes 2D array and uses a nested for loop to find the average of the feed
// amounts. Then returns average value.
{
double average = 0;
for (int column = 0; column < SIZE; column++)
{
for (int row = 0; row < SIZE; row++)
{
average += monkeyFeed[row][column];
}
}
return average / SIZE / SIZE;
}
void avgFeedPerMonkey(double monkeyFeed[][SIZE], double averagePerMonkey[]) // Takes 2D array and uses a nested for loop
{ // to find the average of the feed amounts per monkey.
int sum = 0;
for (int column = 0; column < SIZE; column++)
{
for (int row = 0; row < SIZE; row++)
{
sum += monkeyFeed[row][column];
}
averagePerMonkey[column] = (double)sum / SIZE;
sum = 0;
}
}
void getMonkeyFeedData(double monkeyFeed[][SIZE]) // Takes 2D array and uses a nested for loop to fill each week monkey by monkey
{
int monkey, day, units;
while (inFile >> monkey >> day >> units)
{
monkeyFeed[day - 1][monkey - 1] = units;
}
}
|