Program For this assignment, you will need to write a header and source file for
ID: 3799939 • Letter: P
Question
Program
For this assignment, you will need to write a header and source file for a class called Monkey.
You should define two symbolic constants inside the Monkey.h header file but before the class declaration:
Use them in all of your methods, rather than hardcoding values. We want our program to continue working if we eventually get more monkeys, or we want to track a different number of days.
The Monkey class which will contain the following private data members:
An array of C++ strings containing the names of the monkeys, initialized to "Curious George", "Mojo" and "Marcel".
A two dimensional NUMMONKEYS by NUMDAYS array of float variables that will hold the number of pounds of food each monkey ate each day.
Your class must contain the following public methods:
Monkey()
The constructor takes no parameters and returns nothing. It will read the contents of the file monkeyfood.txt and store each float value into the two dimensional array.
getName()
This method takes an integer index as its parameter and returns a C++ string. This method will return the name of the monkey at the specified index in the monkey names array.
printArray()
This method takes no parameters and returns nothing. It will print a report similar to the following:
totalFoodEaten()
This method takes no parameters, and returns a float, the total pounds of food consumed for all of the monkeys for the week.
avgDailyConsumption()
This method takes no parameters, and returns a float, the average consumption for all of the monkeys for the week. (Hint, you can use totalFoodEaten() in this function. Divide by the number of monkeys * the number of days. Do not hardcode 21!)
avgDailyPerMonkey()
This method takes one parameter, an integer representing the monkey for whom we want to calculate the 7 day food average. The method returns a float which is the average food consumption for that monkey.
mostEaten()
This method takes no parameters and returns nothing. This method will go through the array and find the greatest amount of food eaten during the week by any one monkey on any one day and print the name of the monkey and the greatest amount eaten.
Program Output
Driver Program
A driver program, assign3.cpp, is provided for this assignment. The purpose of a driver program is to test other pieces that you code. You do not need to write the driver program yourself. A copy of the driver program can also be found on turing at /home/turing/t90kjm1/CS241/Code/Spring2017/Assign3/assign3.cpp.
Implementation Hints
The driver program should not be modified for your final submission. But while you're developing, modifying the driver program can definitely be in your best interest. For example, you may want to comment out various parts of main and develop one method at a time.
Makefile
A makefile is required for this assignment, and all future 241 assignments. A sample makefile is given below.
Explanation / Answer
#include<iostream>
#include<string>
#include<fstream>
#include <sstream>
using namespace std;
static const int NUMMONKEYS = 3;
static const int NUMDAYS = 7;
class Monkey
{
string names[NUMMONKEYS];
float food_eaten[NUMMONKEYS][NUMDAYS];
public:
Monkey();
string getName(int index);
void printArray();
float totalFoodEaten();
float avgDailyConsumption();
float avgDailyPerMonkey(int);
void mostEaten();
};
----------------------------------------------------------------------------------------
//Monkey.cpp
#include"Monkey.h"
Monkey::Monkey()
{
ifstream in;
//open file called
in.open("monkeyfood.txt");
string value;
//check if file open before reading
if (!in)
{
cout << "Error opening input file" << endl;
return;
}
string line;
int count = 0;
string name,str1;
while (!in.eof())
{
getline(in, line);
istringstream ss(line);
ss >> name;
istringstream ss1(name);
ss1 >> str1;
names[count] = str1;
for (int j = 0; j < NUMDAYS; j++)
ss >> food_eaten[count][j];
count++;
}
}
string Monkey::getName(int index)
{
return names[index];
}
void Monkey::printArray()
{
for (int i = 0; i < NUMMONKEYS; i++)
{
cout << names[i]<<" " ;
for (int j = 0; j < NUMDAYS; j++)
cout << food_eaten[i][j] << " " ;
cout << endl;
}
}
float Monkey::totalFoodEaten()
{
float total = 0;
for (int i = 0, j = 0; i < NUMMONKEYS; i++)
{
for (int j = 0; j < NUMDAYS; j++)
{
total += food_eaten[i][j];
}
}
return total;
}
float Monkey::avgDailyConsumption()
{
float daily_consumption;
daily_consumption = totalFoodEaten() / (NUMMONKEYS*NUMDAYS);
return daily_consumption;
}
float Monkey::avgDailyPerMonkey(int index)
{
float total = 0,avgDaily;
for (int j = 0; j < NUMDAYS; j++)
total += food_eaten[index][j];
avgDaily = total / NUMDAYS;
return avgDaily;
}
void Monkey::mostEaten()
{
float large = food_eaten[0][0];
//find largest food eaten
for (int i = 0, j = 0; i < NUMMONKEYS; i++)
{
for (int j = 0; j < NUMDAYS; j++)
{
if (large < food_eaten[i][j])
large = food_eaten[i][j];
}
}
//now find which monkey has eaten largest food and print
for (int i = 0, j = 0; i < NUMMONKEYS; i++)
{
for (int j = 0; j < NUMDAYS; j++)
{
if (large == food_eaten[i][j])
{
cout << "Name of the monkey: " << names[i] << " and the greatest amount of food eaten: " << large << endl;
break;
}
}
}
}
-----------------------------------------------------------------------------
//output
The names of all the monkeys:
CuriousGeorge
Mojo
Marcel
Weekly consumption for all monkeys
CuriousGeorge 6 7.3 5.5 6 5.5 6.5 7.4
Mojo 6.3 7.6 5.6 8 7.5 6.9 5.7
Marcel 8.1 9 6.5 7 6.7 8 7.5
Total Food Eaten This Week: 144.6 pounds
Average Daily Consumption for All Monkeys: 6.88571 pounds
Average Daily Consumption per Monkey
CuriousGeorge 6.31429
Mojo 6.8
Marcel 7.54286
Name of the monkey: Marcel and the greatest amount of food eaten: 9
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.