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
|
#include <string>
#include <iostream>
const size_t NO_BOWELS {35};
const size_t MISS {4};
const size_t FISH_TO_EAT {361};
const size_t GOLDFISH = 0, GUPPIES = 1, ANGEL = 2, TIGERFISH = 3, FISH_TYPE = 4;
// or
//enum {GOLDFISH, GUPPIES, ANGEL, TIGERFISH, FISH_TYPE};
void display(size_t fishNum[], const size_t fishType[], const std::string names[])
{
size_t counts[FISH_TYPE] {};
size_t totalFish {};
std::cout << "The number and type of fish in each bowl are as follows:\n";
for (size_t bowl = 0; bowl < NO_BOWELS; bowl++)
std::cout << bowl + 1 << " - " << fishNum[bowl] << " " << names[fishType[bowl]] << '\n';
for (size_t x = 0; x < NO_BOWELS; ++x) {
counts[fishType[x]] += fishNum[x];
totalFish += fishNum[x];
}
std::cout << "\nThere are a total of " << totalFish << " fish\n";
for (size_t x = 0; x < FISH_TYPE; ++x)
std::cout << counts[x] << " " << names[x] << '\n';
std::cout << '\n';
}
int main()
{
size_t fishNum[NO_BOWELS] {15, 15, 15, 8, 8, 8, 8, 19, 16, 16, 16, 16,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 31,
31, 9, 9, 9, 9, 9, 26, 26, 26, 26, 8, 8};
const size_t fishType[NO_BOWELS] = {GOLDFISH, GOLDFISH, GOLDFISH, GOLDFISH, GOLDFISH, GOLDFISH,
GUPPIES, GUPPIES, GUPPIES, GUPPIES, GUPPIES, GUPPIES, GUPPIES,
ANGEL, ANGEL, ANGEL, ANGEL, ANGEL, ANGEL, ANGEL, ANGEL, ANGEL,
GOLDFISH, GOLDFISH, GOLDFISH, GOLDFISH, GOLDFISH, GOLDFISH, GOLDFISH, GOLDFISH,
TIGERFISH, TIGERFISH, TIGERFISH, TIGERFISH, TIGERFISH};
const std::string names[FISH_TYPE] {"Goldfish", "Guppies", "Angel", "Tiger"};
std::cout << "At the beginning,\n";
display(fishNum, fishType, names);
size_t fishEaten {};
for (size_t bowel {3}; fishEaten != FISH_TO_EAT; bowel = (bowel + MISS) % NO_BOWELS) {
if (fishNum[bowel] == 0)
do {
bowel = (bowel + 1) % NO_BOWELS;
} while (fishNum[bowel] == 0);
--fishNum[bowel];
++fishEaten;
std::cout << "Ate " << names[fishType[bowel]] << " from bowel " << bowel + 1 << '\n';
std::cout << "Bowel " << bowel + 1 << " has " << fishNum[bowel] << " left\n";
//std::cin.get();
}
std::cout << "\nAt the end, " << fishEaten << " fish have been eaten.\n";
display(fishNum, fishType, names);
}
|