Shuffling Fish

Pages: 12
Today there was a quiz in my class. I can not answer, because I did not know if there was a quiz today. So there is no preparation. My best to answer, but I am confused at some parts. The question something like this:

Create a class that will store the data of fish weight, price, and species of fish, do not forget to create setters and getters! In this class there will be a count method where the price of the fish will be calculated by the following formula

Price = 1000 + (weight of fish) * 200

For the program menu:

There are 3 menu
1. fishing
2. Selling fish
3. Exit

For menu 1, the program will catch fish results merandom user. Weight of the fish will be randomized from 10-30 will be randomized while species of "tilapia", "catfish", or "goldfish". Users may continue to catch fish, but only the last fish data will be stored and can later be sold by the user.

For menu 2, the program will add user revenue in accordance with the price of the last fish caught. Note that the user can only sell a maximum of 3 times as many fish, and every time a user sells fish, the number of user's chance to sell the fish will be reduced. Validate the user can not sell fish if the user has not caught fish or fish have been sold before last.

The program will be completed if the user selects the menu exit or already sold 3 times as many fish. At that time, the program will print the total user revenue from the sale of fish that has been done.


Help that I needed is:

1. How to make the class?

2. How to randomize the fish?

3. If number, it's:

shuffle_random(10, 30, rand) isnt it?

4. How to do the "...but only the last fish data will be stored and can later be sold by the user..."

If any of you willing to write the code outside my question, I appreciate it. That'll help me a lot understanding this kind of quiz.

Thanks before.
G-d bless you.
1. How to make the class?
http://www.learncpp.com/cpp-tutorial/82-classes-and-class-members/
http://www.cplusplus.com/doc/tutorial/classes/

2. How to randomize the fish?
3. If number, it's:
http://www.cplusplus.com/reference/cstdlib/rand/

4. How to do the "...but only the last fish data will be stored and can later be sold by the user..."
Overwrite variable holding current fish each time he catches fish.
You know, when you cheat you only cheat yourself.
Im not cheating, dont you it is "was" not "is" ? I swear in G-d :(
Then you should post what you have attempted.
Okay, for the class i made it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class fish
{
private:
    int weight;
    float price;
    string species;
    
public:
    void setfish()
    {
        weight = random_shuffle (10, 30);
        price = 1000+weight*200;
        species = ["Tilapia", "Catfish", "Goldfish"];
    }
    
    void showfish()
    {
        cout << "You got " << random_shuffle(species) << "with " << weight << " kg." << endl;
    }
};


any wrong?
Last edited on
weight = random_shuffle (10, 30); Shouldn't work
species = ["Tilapia", "Catfish", "Goldfish"]; Shouldn't work
This code should be in constructor.

cout << "You got " << random_shuffle(species) << "with " << weight << " kg." << endl; Shouldn't work too.
http://en.cppreference.com/w/cpp/algorithm/random_shuffle
You still need accessors for each data member of the fish class:
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

int getWeight()
{
   return weight;
}

float getPrice()
{
  return price;
}

std::string getSpecies()
{
   return species;
}

void setWeight(int w)
{
  weight = w;
}


void setPrice(float P)
{
  price = p;
}

void setSpecies(std::string Name)
{
  species = name;
}


Also, on line 13 you are defining species as a string array, but have declared it as a one item member. You'll want to do this instead:
1
2
3
4
5
6
std::string species[3];

//then in the constructor set each member
species[0] = "Tilapia";
species[1] = "Catfish";
species[2] = "Goldfish";


You need a count method to determine the price of the fish:
1
2
3
4
5

void setFishPrice()
{
   setPrice(1000+weight*200);
};


Hmm, like this?

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
class Fish
{
private:
    int weight;
    float price;
    string species[3];


public:
    void setFish(int w, float p, string sp)
    {
        weight = w;
        price = p;
        species = sp;
    }
    
    void setFishPrice()
    {
        setPrice(1000+weight*200);
    }
    
    void setFishSpecies()
    {
        species[0] = "Tilapia";
        species[1] = "Catfish";
        species[2] = "Goldfish";
    }
};
Yes, but there is not function called setPrice. You'll need to define this or you will get compile errors.

You also don't need a setFishSpecies method if you pass that information to the setFish function.

Don't forget the getters for the information.
I would do something like:
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
class Fish
{
  public:
    Fish()
    : weight(std::rand() % 21 + 10)
    , species(allSpecies[std::rand() % 3])
    { calculatePrice(); }

    void setWeight(int weight_) //I don't think that even that setter is needed
    {
        weight = weight_;
        calculatePrice();
    }

    int getWeight() {return weight;}
    std::string getSpecies() {return species;}
    int getPrice() {return price;}
  private:
    void calculatePrice() {price = 1000 + weight*200;}

    int weight;
    std::string species;
    int price;

    static const std::string allSpecies[3];
};

const std::string Fish::allSpecies[3] = {"Tilapia", "Catfish", "Goldfish"};
Last edited on
I have pass the setfish error, now I get the random_shuffle error.

My code looks like this:

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
class Fish
{
private:
    int weight;
    float price;
    string species[3];


public:
    void setfish()
    {

        species[0] = "Tilapia";
        species[1] = "Catfish";
        species[2] = "Goldfish";

        price = 1000+weight*200;
        weight = rand()%30 + 10;
    }

    void showfish()
    {
        cout << "You caught " << random_shuffle(species[3]) << " with " << wight << " lb."
    }
};



Frankly, random_shuffle is my first time learning.

I've around the internet, I found different style of random_shuffle.
like random.shuffle, random_shufle, and the one with the template, with no deep explanation. Im so confused.

if you mind, give me the easiest random_shuffle answer for this, please :(
im so dumb

I have posted link to what random_shuffle does. With deepest explanation possible and example.
It doesn't suit your needs.
In my first post I had posted link to function you will need here.
Yes, but your given link only randomize numbers, not string. I mean, how to shuffling the fish? (It is array of string)
Last edited on
Select one of three.
You have an array of string with numeric indexes 0-2. Choose one of them. That would be index of your string in array.
Do not use random)shuffle here. It is not chooses random number/string/whatever. It is, well, shuffles them. Like cards in a deck.
This program provides with an example of how you could write the Fish class. You are free to use it (if you think you can get way with it.) However the main program is not done for you. Please let me know if you have any questions, as my point of writing this was not to give you the answer, but show by example how you could approach the problem.

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
#include <ctime>
#include <iostream>
#include <random>
#include <string>

using namespace std;


class Fish {
 private:

	//You can ignore this line if you're not using C++11
	//	and replace the following refernces to prng with "rand()"
	//	found in the header <cstdlib>.
	static default_random_engine prng; //Pseudo-Random Number Generator (C++11)

	
	double weight; //weight of this fish, as a double
	string species; //species of this fish, stored as a string

	
	//Note: the constructor is private, as we don't need main() crateing fish this way.
	Fish(double w, string s) : weight(w), species(s) {
	}

	
 public:
 
	//Use to construct a fish of a random species, with random weight.
	//Syntax in main(): Fish::catchFish()
	static Fish catchFish() {
		string speciesArray[] = {"Tilapia", "Catfish", "Goldfish"}; //array of possible species
		string species = speciesArray[ prng() % 3 ]; //Pick a random element from ^this^ array.
		
		//Real fishs' weights are (likely) normally distributed. Ignore this and use prng
		//	(like done above for the species), if you don't care about real life distributions.
		//	In that case replce the next two lines with the following line:
		//	weight = 10 + (prng() % 21);
		normal_distribution<double> guass( 20.0, 3.0 );
		double weight = guass(prng);
		
		//Construct this fish using the private constructor, and return it.
		return Fish(weight, species);
	}
	
	
	//calculate the price of this fish
	double calculatePrice() const {
		double price = 1000 + weight * 200; //calculate "raw" price (using the requested formula)
		price = (int)(price * 100 + 0.5) / 100.0; //round price to the nearest cent
		return price;
	}
	
	
	//accessor methods
	double weigh() const {
		return weight;
	}
	
	string identify() const {
		return species;
	}
};

//Define static Fish member variable: prng
//	by seeding it with the current system time. (C++11)
//	If you're not using C++11 and you replaced prng with rand()
//	then ignore this line and replace it with a single call to
//	srand( time(NULL) );
//	in main(), before your first call to catchFish().
default_random_engine Fish::prng( time(NULL) );


//--------------------------------------------------------------------------------------------------


int main() {
	//example: catch one fish and display its characteristics
	Fish fishy = Fish::catchFish();
	cout << "Species: " << fishy.identify() << '\n';
	cout << "Weight:  " << fishy.weigh() << " lbs\n";
	cout << "Price:   $" << fishy.calculatePrice() << '\n';
	
	//example: catch 100,000 fish, and count how many were heavier then 30 lbs
	//	and lighter then 10 lbs
	unsigned int N = 100000;
	unsigned int heavyCount = 0;
	unsigned int lightCount = 0;
	for( unsigned int i = 0; i < N; i++ ) {
		Fish f = Fish::catchFish();
		if( f.weigh() > 30.0 )
			heavyCount++;
		else if( f.weigh() < 10.0 )
			lightCount++;
	}
	cout << "\n[Out of " << N << "]\n";
	cout << "Heavy Fish: " << heavyCount << " (" << (100.0 * heavyCount / N) << "%)\n";
	cout << "Light Fish: " << lightCount << " (" << (100.0 * lightCount / N) << "%)\n";
	
	return 0;
}
Ok, thanks for that, but how to do the "..but only the last fish data will be stored .." ?

Is it Linked List?
You can just store the last Fish in a variable... Why would you need a Linked List, or any List at all for that matter?
Would you mind giving me an example code that look like "You can just store the last Fish in a variable..." ? Please?
The part of storing the last Fish is simple.
Each time the user get a fish, the previous data is deleted, being replace for the new one.
Pages: 12