Program Description [4 points] You have just purchased a zoo. Along with it, you
ID: 3648779 • Letter: P
Question
Program Description [4 points]You have just purchased a zoo. Along with it, you are given a dossier file of the number of types
of animals, how many of each animal there is, and how many pounds of food they (as a group)
eat a day. Using either classes or structs (your choice) keep a list of those animals and answer
the following questions:
1) How many animals are there?
2) How much total food is consumed each day?
3) Which animals (individually) eat the most food (rank top 3)?
4) Which animals (individually) eat the least amount of food (rank top 3)?
Print your answers to the console screen.
Input: A file with number of animal groups, and for each group, group name, quantity,
and food consumption.
Output: Number of animals, total food consumed, top 3 animals that eat the most, top 3
animals that eat the least.
Example output:
====================================
Total food consumed: 100 lbs
Most food consumed by
1. Bears
2. Lions
3. Jaguars
Least food consumed by
1. Iguanas
2. Pythons
3. Penguins
The average of those three numbers is 13.5
====================================
the text file is:
9
Bear
3
100
Lion
6
90
Zebra
10
150
Penguin
6
12
Python
3
6
Elephant
3
1600
Eagle
2
2
Stingray
20
10
Seaturtle
3
10
and so far I have written the data on the txt to the program
but I cannot do the last two parts of the question the least and the most the sum and the average
this is my code so far
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Animal
{
string animal_type;
int qty;
int food_consumed;
};
int main()
{
Animal *animal;
int num_animals;
ifstream file;
Animal *most_food;
Animal *least_food;
file.open("animals.txt", ifstream::in);
if (file.fail())
{
cout << "Error!!! File could not be opened. ";
return 0;
}
file >> num_animals;
animal = new Animal [num_animals];
cout << "There are " << num_animals << endl << endl;
for (int i =0; i < num_animals; i ++)
{
file >> animal[i].animal_type;
file >> animal[i].qty;
file >> animal[i].food_consumed;
cout << "The type of the animal is: " << animal[i].animal_type << endl;
cout << "Qty: " << animal[i].qty << endl;
cout << "Consumption: " << animal[i].food_consumed << endl << endl;
}
if (num_animals > 0)
{
most_food = &animal[0];
least_food = &animal[0];
}
cout << "Most food consumed by ";
for (int i = 0; i < 1 ; i++)
{
if (animal[i].food_consumed > most_food -> food_consumed)
{
most_food = &animal[i];
}
if (animal[i].food_consumed < least_food -> food_consumed)
{
least_food = &animal[i];
}
if (num_animals > 0)
{
cout << most_food -> animal_type << endl
<< least_food -> animal_type << endl;
}
else
cout << "The animals escape ";
}
file.close();
return 0;
}
Explanation / Answer
//try this code.... //works for me //Modified Code #include #include #include #include using namespace std; struct Animal { string animal_type; int qty; int food_consumed; }; int main() { Animal *animal; int num_animals; ifstream file; Animal *most_food; Animal *least_food; file.open("animals.txt", ifstream::in); if (file.fail()) { cout > num_animals; animal = new Animal [num_animals]; coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.