Crazy Pet Store Program

Pages: 12
**updated -- see comment below

*Ive updated the post to include all missing information

Im working on the following problem for my c++ programming class and am at a complete loss. I am not looking for someone to simply give me the answer but rather guide me in the right direction. Tutoring at my school isnt available during the times I study. The problem is as follows:

The local pet store has 35 different bowls with fish in them arranged in a circle in the “fish room.” The first six
have goldfish, the next seven have guppies, the next nine have angel fish, the next eight have goldfish, and the
last five hold tiger fish. As the bowls vary in size, the number of fish that are in each bowl at the start of the
day also varies. The count of fish in each bowl is as follows:
Bowls 1 – 3: 15 fish
Bowls 4 – 7: 8 fish
Bowl 8: 19 fish
Bowls 9 – 12: 16 fish
Bowls 13 – 22: 14 fish
Bowls 23 – 24: 31 fish
Bowls 25 – 29: 9 fish
Bowls 30 – 33: 26 fish
Bowls 34 – 35: 8 fish


A trained seal finds its way into the store. The seal begins to eat fish in the following manner.
a. It walks over to bowl #1.
b. It counts four bowls and then eats a fish (the first fish it eats comes from bowl #4 and it’s a goldfish).
c. It again counts four, starting with the next available bowl, and eats a fish (the second fish it eats
comes from bowl #8 and it’s a guppy.

Method:
The conditions that controls the seal are:
a. If there are no fish left in a bowl, the seal does not count the bowl and skips it and goes to the next
bowl containing fish. (It starts counting four from the bowl it ate from.)
b. The bowls are arranged in a circle, there is no last bowl.
c. After the seal eats a bowl #35, it counts over 4 bowls and finds itself at bowl #4. (Bowl #3 if four
bowls away from 34, etc.)
d. The seal continues to eat until 361 fish are consumed.

Your Task:
You are to write a C++ program that will calculate and report the following (put breaks in so the user can read):
a. The number and type of fish in each bowl before the seal begins eating.
b. The total number of each type of fish before the seal begins to eat.
c. After each time the seal eats:
a. The type of fish that was just eaten and how many fish are left in the bowl.
b. The total number of each type of fish left in all the bowls.
d. The total number of each type of fish remaining after the seal eats.

I know that i have to make a parallel array but im not sure how to
Last edited on
It seems to be missing some info?
what are the types of fish available? I see goldfish and guppy, what else?
What are the initial conditions of the bowls, or do you have to figure that out too?

do you know 2-d arrays?
what if you had a 2-d array where the rows were the bowls, and the columns were the fish types, of type integer?
eg
int bowls[36][2]; //35 bowls each with guppies and goldfish, and lets ignore bowl # 0 to make it simple.

now, if you don't know the power of modulus (%), a big help.. you can write a statement such that, if x is the current bowl, and you add 4 to it, you go from 35 to 4 using a modulus... do you see it? Easier example using 10 ... x%10 0%10 is 0, 1 %10 is 1, ... 8 is 9, 10 is 0, 11%10 is 1, ... 19 is 9, ...

Does this get you started? The 2-d array could be 2 parallel arrays but parallel arrays are almost always more trouble than any other way of doing things. The only real exception there is a lookup table, for example enums to strings, and even that is a still a little C-ish. Doing it for dynamic data (what I call a lookup table is an array of constants) is a bit messy.

speaking of which, it may be wise to label your columns with an enum,
enum fishtype{guppy, goldfish, maxfish};
int bowls[36][maxfish];
...
cout << bowls[11][guppy] //Very Readable!
Last edited on
This sounds like a modeling problem since it doesn't look like you're given any code to begin with.

lovelylizxox wrote:

Your Task:
You are to write a C++ program that will calculate and report the following (put breaks in so the user can read):
a. The number and type of fish in each bowl before the seal begins eating.
b. The total number of each type of fish before the seal begins to eat.
After each time the seal eats:
c. The type of fish that was just eaten and how many fish are left in the bowl.
d. The total number of each type of fish left in all the bowls.
e. The total number of each type of fish remaining after the seal eats.


Some questions I have for you:
1. Do you know what data structures are? Arrays? Vectors? List? Maps? Heard of em? Have you used them before?
2. Have you programmed in any other programming languages?

Some questions I have about the bowls and the fish inside: These questions should be answered by whoever gave you this task/your "dungeon master"

1. It's implied that there's more than one fish in each bowl. Is it also true that all fish in a bowl are of the same type?

2. Is it possible, initially, for there to be one or more empty fish bowls among the 35? What should you report for (a) if a bowl was initially empty?

Some questions about the tasks: Stuff you should think about

1. To answer (b), we need the collection of 35 bowls, presumably with references to Fish objects that "live" inside each bowl. What data structure (vector, array, list, map) do you think is appropriate for modeling bowls? Does the DS give you the count of the number of objects it contains?

2. To model the seal object eating fish from a collection of bowls of fish, you'd need a method: either one that resides in a seal object (e.g.,Seal.EatFromBowl(vector<Bowl> bowls)) or the more procedural style: void EatFromBowl(vector<Bowl> bowls, Seal seal).

3. (c) requires you to think which actor is likely to contain information about "the type of fish that was just eaten" and which would know about "how many fish are left in the bowl". The SEAL knows what it'd just eaten and the BOWL knows how many fish are left inside, right?

4. (d) requires you think about how to store the 35 bowls. Say you decided each bowl should be a list. Do you create 35 list variables in your program? Pretty unwieldy if you ask me. Maybe you need a datastructure to store bowls of fish! This is an example of nested data structures which can get pretty wild and complex in real programs.

5. If you've made use of the hints I've given so far, this one should be within your reach. No hint for you.


Last edited on
Jonin,

Im familiar with 2d arrays but the professor says we should use parallel arrays. I think I have to make a for loop that traverses through the array but im not exactly sure.
ElusiveTau,

I'm only familiar with array. Professor says we should use a parallel array but im not exactly sure how. I've updated the post so that it shows the entire problem.
If you are to use parallel arrays, then what you need is an array of enums describing the type of fish in each bowl, and a second array with the count of fish in each bowl.

This should get you started. I'll leave the implementation of the seal up to you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//  We're going to ignore bowls[0] and cnt[0]
constexpr int MAX_BOWLS = 36;   

enum FISH { EMPTY, GOLDFISH, GUPPIES, ANGEL, TIGERFISH };
//  Populate the types of fish in each bowl
FISH bowls[MAX_BOWLS] = {  EMPTY, 
    /* 6 */         GOLDFISH, GOLDFISH, GOLDFISH, GOLDFISH, GOLDFISH, GOLDFISH, 
    /* 7 */         GUPPIES, GUPPIES, GUPPIES, GUPPIES, GUPPIES, GUPPIES, GUPPIES,
    /* 9 */         ANGEL, ANGEL, ANGEL, ANGEL,ANGEL,ANGEL,ANGEL,ANGEL,ANGEL,
    /* 8 */         GOLDFISH, GOLDFISH, GOLDFISH, GOLDFISH, GOLDFISH, GOLDFISH, GOLDFISH, GOLDFISH, 
    /* 5 */         TIGERFISH, TIGERFISH, TIGERFISH, TIGERFISH, TIGERFISH };
/* Populate the numbe5r of fish in each bowl*/
int cnt[MAX_BOWLS] = {  0,
    /* 1-3 */           15, 15, 15, 
    /* 4-7 */           8, 8, 8, 8,
    /* 8 */             19, 
    /* 9-12 */          16, 16, 16, 16,
    /* 13-22 */         14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
    /* 23-24 */         31, 31,
    /* 25-29 */         9, 9, 9, 9, 9, 
    /* 30-33 */         26, 26, 26, 26,
    /* 34-35 */         8, 8 };

Last edited on
Hello lovelylizxox,

Using what jonnin said a parallel arrays might look like this:
1
2
3
4
5
6
7
8
9
constexpr int MAXSIZE{ 36 };

const std::string fishTypes[MAXSIZE]
{
    "",
    "Gold fish", "Gold fish", "Gold fish", "Gold fish", "Gold fish", "Gold fish",
    "Guppies"
};
int NumOfFish[MAXSIZE]{ 0, 15, 15, 15, 8, 8, 8, 8 };

Note that element zero of each is just a place holder and that you actually start at element 1.

I broke up the fish type with each type being on 1 line. Or you could use 1 line for each if that helps.

I would work on getting this part set first. The rest will be easier to understand as you proceed.

Andy
Hi all,

thanks for you responses. this problem is kicking my rear end! I havent learned anything about 'enums' -- my class is an introductory class.

Andy, why is element 0 a place holder? shouldt element 0 represent bowl #1?

this is my updated code:
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
int main()
{
	//variable declaration
	int bowl, goldCount = 0, gupCount=0, angelCount=0, tigerCount=0, totalFish, fishEaten;

	//array with # of fish in bowls
	int fishNum[35] = { 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};	// initialize the number of fish in bowls
	
	//initialize type of fish in bowls
	string fishType[35] = { "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",
							"tiger", "tiger", "tiger", "tiger", "tiger"};	


	//calculate number and type of fish in each bowl QUESTION B1
	//go through array and link fishNum to fishType
	for (int x = 0; x <= 35; x++)
	{
		if (fishType[x] == "goldfish")
		{
			goldCount = fishNum[x] + goldCount;
			//cout << "There are " << goldCount << " total goldfish before the seal starts eating." << endl;
		}
		else if (fishType[x] == "guppies")
		{
			gupCount = fishNum[x] + gupCount;
			//cout << "There are " << gupCount << " total guppies before the seal starts eating." << endl;
		}
		else if (fishType[x] == "angel")
		{
			angelCount = fishNum[x] + angelCount;
			//cout << "There are " << angelCount << " total angel fish before the seal starts eating." << endl;
		}
		else if (fishType [x] == "tiger")
		{
			tigerCount = fishNum[x] + tigerCount;
			//cout << "There are " << tigerCount << " total tiger fish before the seal starts eating." << endl;
		}
	}

	//THIS ANSWERS QUESTION B1
	cout << "To begin, there are " << goldCount << " gold fish, " << gupCount << " guppies, " << angelCount << " angel fish, and " << tigerCount << " tiger fish." << endl;

	//how to make program go to bowl 1 after bowl 35??*/
	while (fishEaten < 361)
		for (bowl = 4; bowl <= 35; bowl + 4)
			bowl = 0;
Last edited on
If you're learning C++, you should really be writing this as a class rather than parallel arrays.

Here's an example:
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
//  We're going to ignore bowls[0] and fish[0]
constexpr int MAX_BOWLS = 36;   

enum FISH { EMPTY, GOLDFISH, GUPPIES, ANGEL, TIGERFISH };

class Bowl
{   enum FISH       fish;
    int             qty;
public:
    Bowl (enum FISH f, int cnt);
    void EatFish ();
    void Print ();
};

Bowl bowls[MAX_BOWLS] = 
    {   /* 0 */ { EMPTY, 0}, 
        /* 1 */ { GOLDFISH, 15},
        /* 2 */ { GOLDFISH, 15 },
        /* 3 */ { GOLDFISH, 15 },
        /* 4 */ { GOLDFISH, 8 },
        /* 5 */ { GOLDFISH, 8 },
        /* 6 */ { GOLDFISH, 8 },
        /* 7 */ { GUPPIES, 8 },
        /* 8 */ { GUPPIES, 19 },
        /* 9 */ { GUPPIES, 16 },
        /* 10 */{ GUPPIES, 16 },
        /* 11 */{ GUPPIES, 16 },
        /* 12 */{ GUPPIES, 16 },
        /* 13 */{ GUPPIES, 14 },
        /* 14 */{ ANGEL, 14 },
        /* 15 */{ ANGEL, 14 },
        /* 16 */{ ANGEL, 14 },
        /* 17 */{ ANGEL, 14 },
        /* 18 */{ ANGEL, 14 },
        /* 19 */{ ANGEL, 14 },
        /* 20 */{ ANGEL, 14 },
        /* 21 */{ ANGEL, 14 },
        /* 22 */{ ANGEL, 14 },
        /* 23 */{ GOLDFISH, 31 },
        /* 24 */{ GOLDFISH, 31 },
        /* 25 */{ GOLDFISH, 9 },
        /* 26 */{ GOLDFISH, 9 },
        /* 27 */{ GOLDFISH, 9 },
        /* 28 */{ GOLDFISH, 9 },
        /* 29 */{ GOLDFISH, 9 },
        /* 30 */{ GOLDFISH, 26 },
        /* 31 */{ TIGERFISH, 26 },
        /* 32 */{ TIGERFISH, 26 },
        /* 33 */{ TIGERFISH, 26 },
        /* 34 */{ TIGERFISH, 8 },
        /* 35 */{ TIGERFISH, 8 } };

Bowl::Bowl (enum FISH f, int cnt)
{   fish = f;
    qty = cnt;
}
Last edited on
abstractionanon, we havent learned class so i dont know how to use it. the professor stated we use parallel arrays
Hello lovelylizxox,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Andy, why is element 0 a place holder? shouldt element 0 represent bowl #1?


Sometimes it is easier to work with when you skip the first element. A better example would be the months of the year. When you would write "01/01/2020" everyone knows that the "01" means January. Thinking of the string as 3 separate int you could then use this as an index to an array. the problem would be that January would be in element 0 and February would be in element 1. It makes it easier to use what a user would normally enter and use it as an index to an array with out having to remember to subtract 1 before using for an index.

And you fell right into this problem for (bowl = 4; bowl <= 35; bowl + 4). So you are actually starting at the 5th bowl and not the 4th which would be element 3 in the array. Also it is better to define "bowl" in the for loop for ([b]int bowl = 4[/b]; bowl <= 35; bowl + 4). Unless there is some reason to need it outside the for loop.

Using (<=) in a for loop tends to do 1 more loop than you expect. There are occasions when it is useful.

When I got a better look at your code for (int x = 0; x <= 35; x++). This will loop 36 times leaving "x = 35" which is 1 past the end of the array.

In the end you will need more in the for condition than just bowl <= 35. I am thinking something like
bowl < 35 && totalFish. As long as "totalFish" is > zero it is considered true. When "totalFish" becomes zero it is considered false. This is the same as saying "totalFish > 0". This may not be the only way of doing this, but it is 1 possibility.

Then inside the for loop use an if statement:
1
2
3
4
for loop
{
    if (bowl == 34)
        bowl = 3;

Since the bowl numbers start at 1 skipping element zero of the array helps to visualize better what you are using.

I will see what I can come up with to give your code a test.

It is always better to post enough code that can be compiled and tested. Proper and complete code inside code tags comes with a gear icon and the words "Edit & Run". Clicking on this will put your code in a shell program that will allow you to compile and test the code. Also it eliminates any guess work at what you may have done.

Andy
Andy, this helps me incredibly! Thank you!
Hello lovelylizxox,

I made a few changes to your program that should be helpful:
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
#include <iostream>
#include <iomanip>
#include <string>
#include <limits>

int main()
{
    //variable declaration
    constexpr int MAXSIZE{ 35 };

    int goldCount = 0, gupCount = 0, angelCount = 0, tigerCount = 0, totalFish{}, fishEaten{};

    //array with # of fish in bowls
    int fishNum[MAXSIZE] = { 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 }; // initialize the number of fish in bowls

        //initialize type of fish in bowls
    const std::string fishType[MAXSIZE] = { "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",
        "tiger", "tiger", "tiger", "tiger", "tiger" };


    //calculate number and type of fish in each bowl QUESTION B1
    //go through array and link fishNum to fishType
    for (int idx = 0; idx <= 35; idx++)
    {
        if (fishType[idx] == "goldfish")
        {
            //goldCount = fishNum[idx] + goldCount;
            goldCount += fishNum[idx];  // <--- The same as above.

            totalFish += fishNum[idx];  // <--- Added these lines.
        }
        else if (fishType[idx] == "guppies")
        {
            gupCount += fishNum[idx];

            totalFish += fishNum[idx];
        }
        else if (fishType[idx] == "angel")
        {
            angelCount += fishNum[idx];

            totalFish += fishNum[idx];
        }
        else if (fishType[idx] == "tiger")
        {
            tigerCount += fishNum[idx];

            totalFish += fishNum[idx];
        }
    }

    std::cout  // <--- Used for testing. You may or may not want to keep.
        << "There are " << goldCount << " total goldfish before the seal starts eating.\n"
        << "There are " << gupCount << " total guppies before the seal starts eating.\n"
        << "There are " << angelCount << " total angel fish before the seal starts eating.\n"
        << "There are " << tigerCount << " total tiger fish before the seal starts eating.\n"
        << "\nThere are " << totalFish << " total fish before the seal starts eating.\n";


	// <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;  // <--- Not required, but makes a good break point.
}

In line it is best to initialize all variables, especially the last 2.

Lines 65 to 69 I use for testing and debugging. It is not necessary to keep these lines.

Andy
if no one said, enum is just a group of constant, named integers. the default is start at 0 and keep going, but you can override it if you want to.
Hi all

Thank you so much for responding and helping me with this problem. I was able to figure out how to answer question A & B but am now stuck on C. Im trying to figure out how to tell the program to go back to bowl 1 after reaching bowl 35. My issue starts in line 43. I think I should do a while loop that tells the program to go around the room (traverse through the arrays) while the seal has eaten less than 361 fish. Then i thought maybe i should call a function that does this but again im stuck on how to move forward. Please help. My program is below

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
int main()
{
	//variable declaration
	int bowl, goldCount = 0, gupCount=0, angelCount=0, tigerCount=0, totalFish=0, fishEaten=0, x=0, num=0;

	//array with # of fish in bowls
	int fishNum[35] = { 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};	// initialize the number of fish in bowls
	
	//initialize type of fish in bowls
	string fishType[35] = { "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",
				"tiger", "tiger", "tiger", "tiger", "tiger"};	

	//THIS ANSWERS QUESTION A1 which lists the # and type of fish in each bowl
	cout << "The number and type of fish in each bowl is as follows: " << endl;
	for (bowl = 0; bowl <= 34; bowl++)
	{
		fishNum[x], fishType[x];
		num++;
		cout << num << " - " << fishNum[bowl] << " " << fishType[bowl] << endl;
	}

	//go through array and link fishNum to fishType
	for (int x = 0; x <= 34; x++)
	{
		if (fishType[x] == "goldfish")
			goldCount = fishNum[x] + goldCount;
		else if (fishType[x] == "guppies")
			gupCount = fishNum[x] + gupCount;
		else if (fishType[x] == "angel")
			angelCount = fishNum[x] + angelCount;
		else if (fishType [x] == "tiger")
			tigerCount = fishNum[x] + tigerCount;
	}

	//THIS ANSWERS QUESTION B1
	cout << "\nTo begin, there are " << goldCount << " gold fish, " << gupCount << " guppies, " << angelCount << " angel fish, and " << tigerCount << " tiger fish." << endl;

	//traverse through bowls
	while (fishEaten < 361)
	{
		for (bowl = 4; bowl <= 35; bowl + 4)				//seal starts @ bowl 4 and picks every 4th bowl thereafter
		{
			fishNum[bowl]--;
			fishEaten++;
			//call function
		}
		
			//how do i tell the program to skip a bowl if its empty?

	//funtion to reset bowl # to 1 after bowl 35
	void reset ()

	//how to make program go to bowl 1 after bowl 35??*/
	/*while (fishEaten <= 361)
		for (bowl = 4; bowl <= 35; bowl + 4)
			bowl = 0;*/


	
		

	return 0;
}
/*write a C++ program that will calculate and report the following (put breaks in so the user can read):
c. After each time the seal eats :
a.The type of fish that was just eaten and how many fish are left in the bowl.
	fishNum [3] = 7,  fishType [3] = goldfish 
b.The total number of each type of fish left in all the bowls.
d.The total number of each type of fish remaining after the seal eats */
See my post... the % operator can be set up to wrap back to bowl 1 as I explained. then you can do away with the inner for loop and just keep moving the bowl forward with the wrapping += statement on 'bowl'

besides there will be cases where it isnt += 4 and inst 35 back to 1..
Last edited on
 
fishNum[x], fishType[x];


I'm not sure what you intended here, but this doesn't do what you might think. It obtains the value of fishNum[x], doesn't use it, then obtains the value of fishType[x] and doesn't use it. ie this statement does nothing.

For info this uses the , comma operator. expressions between the commas are evaluated left to right. The value of the statement is the value of the last expression evaluated.
Using an array for the counts - rather than separate variables - and an array for the fish names and using a function to display the stats required, consider:

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);
}

Hello lovelylizxox,

I made some changes to your program.

When I first tried to compile the program it had errors.

The function you tried to write is in the body of "main". This might work, but it is not the proper way to write a function. Second the function is not complete, so all it did was cause errors. For the moment I moved it outside "main" and commented it out. You could use a function, but it is not necessary. I believe an if statement in the right place would replace the function.

Again I offer these suggestions to your code. It makes it easier to read and follow.
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
#include <iostream>
#include <iomanip>
#include <string>
#include <limits>

int main()
{
    constexpr int MAXSIZE{ 35 };

    //variable declaration
    int bowl, goldCount = 0, gupCount = 0, angelCount = 0, tigerCount = 0, totalFish = 0, fishEaten = 0, x = 0, num = 0;

    //array with # of fish in bowls
    int fishNum[MAXSIZE] = { 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 };	// initialize the number of fish in bowls

//initialize type of fish in bowls
    const std::string fishType[MAXSIZE] = { "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",
        "tiger", "tiger", "tiger", "tiger", "tiger" };

    //THIS ANSWERS QUESTION A1 which lists the # and type of fish in each bowl
    std::cout << "The number and type of fish in each bowl is as follows: \n";

    for (bowl = 0; bowl < MAXSIZE; bowl++)
    {
        //fishNum[x], fishType[x];
        //num++;
        std::cout << "Bowl#" << std::setw(3) << bowl + 1 << " - " << std::setw(2) << fishNum[bowl] << " " << fishType[bowl] << '\n';
    }

    //go through array and link fishNum to fishType
    for (int idx = 0; idx <= 34; idx++)
    {
        if (fishType[idx] == "goldfish")
            goldCount = fishNum[x] + goldCount;
        else if (fishType[idx] == "guppies")
            gupCount = fishNum[idx] + gupCount;
        else if (fishType[idx] == "angel")
            angelCount = fishNum[idx] + angelCount;
        else if (fishType[idx] == "tiger")
            tigerCount = fishNum[idx] + tigerCount;
    }

    //THIS ANSWERS QUESTION B1
    std::cout << "\nTo begin, there are " << goldCount << " gold fish, " << gupCount << " guppies, " << angelCount << " angel fish, and " << tigerCount << " tiger fish." << '\n';

    std::cout << "\n There are " << totalFish << " total fish in all bowls.\n";
}

Not the complete program, but you really need to get this part working first or the rest will not work.
Looking at lines 28 - 33.

You wrote: for (bowl = 0; bowl < MAXSIZE; bowl++). "bowl" is defined outside the for loop. if you did this because you are thinking that "bowl" is used in more than 1 for loop that would be wrong. It is better to define "bowl" in the for loop and make it local to the for loop as:for (int bowl = 0; bowl < MAXSIZE; bowl++). This way when the for loop is finished the variable "bowl" is destroyed and is able to be used in another for loop or while loop.

For line 30 seeplus has covered this well. I would add that the variable "x" is defined outside the for loop and is initialized to zero, but inside the for loop it never changes, so even if the line of code worked you would always be accessing the same element of the array.

For line 31 this is not really needed. It is better to make use of what you have than to over think ti and write extra code that you not need.

Line 32 shows you how to use the loop iterator to get the number that you want. This makes use of what you have.

When I first ran this part the output is:

The number and type of fish in each bowl is as follows:
Bowl#  1 - 15 goldfish
Bowl# 10 - 16 guppies
Bowl# 11 - 16 guppies
Bowl# 33 - 26 tiger
Bowl# 34 -  8 tiger
Bowl# 35 -  8 tiger

To begin, there are 202 gold fish, 105 guppies, 126 angel fish, and 94 tiger fish.

There are 0 total fish in all bowls.


I cut the first part down for brevity, but notice the last line.

Changing the for loop to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (int idx = 0; idx <= 34; idx++)
{
    if (fishType[idx] == "goldfish")
    {
        //goldCount = fishNum[x] + goldCount;
        goldCount += fishNum[idx];  // <--- Same as above. Much easier to write and does the same thing.
        totalFish += fishNum[idx];  // <--- Repeat in the else if statements.
    }
    else if (fishType[idx] == "guppies")
        gupCount = fishNum[idx] + gupCount;
    else if (fishType[idx] == "angel")
        angelCount = fishNum[idx] + angelCount;
    else if (fishType[idx] == "tiger")
        tigerCount = fishNum[idx] + tigerCount;
}

Note line 7.

I get this output:

The number and type of fish in each bowl is as follows:
Bowl#  1 - 15 goldfish
Bowl# 10 - 16 guppies
Bowl# 11 - 16 guppies
Bowl# 33 - 26 tiger
Bowl# 34 -  8 tiger
Bowl# 35 -  8 tiger

To begin, there are 202 gold fish, 105 guppies, 126 angel fish, and 94 tiger fish.

There are 202 total fish in all bowls.


Of course this is only counting "goldfish". But when fixed it will have the total number of fish in all bowls.

For your while loop consider: while (fishEaten < totalFish). Use the variable that you created. Not a number that may change.

Another possibility.
1
2
3
4
5
6
fishEaten = totalFish;

while(fishEaten)
{
    fisheaten--;
}

In this example when "fishEaten" becomes zero the while loop will be false and fail.

The for loop inside your while loop should be:
 
for (int bowl = 4; bowl < MAXSIZE && fishEaten; bowl + 4)  //seal starts @ bowl 4 and picks every 4th bowl thereafter 

The problem I see is that the array starts with element 0 - 34 for 35 bowls. That means that bowl 1 is in element (0) and bowl 4 would be in element (3). Starting the for loop at (4) this would be bowl (5). Is that what you want because the comment seems to say different.

I have not tested this yet, but inside the for loop I am thinking:
1
2
3
4
5
6
7
for (int bowl = 4; bowl < MAXSIZE && fishEaten; bowl + 4)
{
    if (bowl == MAXSIZE -1)
        bowl = 3;

    // Other code.
}

This works when "fishEaten" is set to "totalFish" and you subtract 1 in the for loop.

I just realized that the while loop may not be need because the for loop should keep going until "fishEaten" reaches (0).

Your for loop would also have to account for when a bowl becomes empty and you have to move on to the next 1.

Just some thoughts I have.

Andy
Seeplus,

BOWLS, not BOWELS. There's a difference. :)

1
2
3
4
5
	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);

I think that isn't quite right. Basically, you're skipping 4 bowls and finding the next occupied bowl. But you need to find the 4th occupied bowl from the beginning. So if all 4 of the first bowls are empty, then you need to find the 4th occupied bowl, not the first.

Handy Andy,
1
2
3
4
5
6
7
for (int idx = 0; idx <= 34; idx++)
{
    if (fishType[idx] == "goldfish")
    {
        //goldCount = fishNum[x] + goldCount;
        goldCount += fishNum[idx];  // <--- Same as above. Much easier to write and does the same thing.
        totalFish += fishNum[idx];  // <--- Repeat in the else if statements. 

It would be better to increment totalFish once:
1
2
3
4
5
6
7
or (int idx = 0; idx <= 34; idx++)
{
    totalFish += fishNum[idx];
    if (fishType[idx] == "goldfish")
    {
        //goldCount = fishNum[x] + goldCount;
        goldCount += fishNum[idx];  // <--- Same as above. Much easier to write and does the same thing. 


lovelylizxox,
Be careful when the seal first enters the room! Don't start his position at bowl #1. Depending on how you structure the loop, upon entering it the first time, you want him in front of bowl #35 and ready to move , or in front of bowl #4 and ready to eat.

Regarding using 0-34 or 1-35 to represent bowl numbers, I find it helpful to distinguish between the numbers that are convenient for the human vs. ones that are convenient for the computer. In case like this where they are different, I like to convert to computer
Pages: 12