Help With Simple Functions? Please Help me!
Mar 2, 2012 at 6:04am UTC
Hello, I am having trouble figuring out how to make these functions. I basically need a member function that will compute the total food needed for a zoo by type of animal. This is the data and program I have so far. The data displayed represents animal species ( 1 - 10 ) and then the amount of food each animal eats. How would I write a function to represent and return the total food needed for each species? Thanks for the help!
** NOTE this is what I have so far, I know it's not right because I don't know how to return all of the species. Please show me how I would do this, I really appreciate the help!
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
1 40
1 45
1 40
1 45
2 30
2 28
2 32
3 55
3 60
3 65
4 80
4 85
4 90
5 120
5 110
5 105
6 240
6 290
6 325
7 220
7 220
7 220
8 300
8 325
9 200
9 200
9 200
9 210
10 40
10 42
10 44
10 44
10 40
10 25
10 25
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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
class ZooClass
{
public :
int compute_total_food();
int total_food_by_type();
int count_animals_by_type();
int hungriest_animal();
void load_animals_cinci(string filename);
void load_animals_san_diego(string filename);
/*
using namespace san_diego;
{
void summary_food_report();
}
using namespace cincinnati;
{
void summary_food_report();
}
*/
private :
ifstream infile;
struct animal
{
int type;
int food;
} zoo[100];
};
int main()
{
ZooClass Cincinnati, San_Diego;
Cincinnati.load_animals_cinci("cincinnati.animals" );
cout << "The total pounds of food needed for the Cincinnati Zoo is: " << Cincinnati.compute_total_food() << "\n" ;
cout << "The hungriest animal was " << Cincinnati.hungriest_animal();
//Cincinnati.count_animals_by_type();
}
void ZooClass::load_animals_cinci(string filename)
{
for (int i = 0; i < 100; ++i)
{
zoo[i].type =0; zoo[i].food=0;
}
int ofst = 0;
infile.open("cincinnati.animals" );
if ( infile.fail() )
{
cerr << "cannot open infile input file\n\n" ;
exit(1);
}
while (infile >> zoo[ofst].type)
{
infile >> zoo[ofst].food;
ofst++;
}
}
void ZooClass::load_animals_san_diego(string filename)
{
for (int i = 0; i < 100; ++i)
{
zoo[i].type =0; zoo[i].food=0;
}
int ofst = 0;
infile.open("san_diego.animals" );
if ( infile.fail() )
{
cerr << "cannot open infile input file\n\n" ;
exit(1);
}
while (infile >> zoo[ofst].type)
{
infile >> zoo[ofst].food;
ofst++;
}
}
int ZooClass::compute_total_food()
{
int total = 0;
for (int i = 0; i < 100; i++)
{
total += zoo[i].food;
}
return (total);
}
int ZooClass::hungriest_animal()
{
int hungriest;
hungriest = zoo[0].food;
for (int i = 0; i < 100; i++ )
{
if ( zoo[i].food > hungriest ) hungriest = zoo[i].food;
}
return (hungriest);
}
int ZooClass::total_food_by_type()
int total_ones, total_twos, total_threes, total_fours, total_fives, total_sixes, total_sevens,
total_eights, total_nines, total_tens = 0;
for (int i = 0; i < 100; i++)
{
if (zoo[i].type == 1)
{
total_ones += zoo[i].food;
}
if (zoo[i].type == 2)
{
total_twos += zoo[i].food;
Topic archived. No new replies allowed.