1 2 3 4 5 6 7
|
Cspinner()
{
fruit[0] = apple; // 30% Probability
fruit[1] = orange; // 25% Probability
fruit[2] = cherry; // 20% Probability
fruit[3] = banana; // 15% Probability
fruit[4] = peach; // 10% Probability
|
Notice that you're assigning fruit to values that were never initialized.
Since fruit was initialized in declaration, there is no need for this default constructor.
But I notice that you've written void main. So in that case initializing fruit at declaration would be incorrect right (I'm not sure)? In that case assign integer values to fruit at the default constructor and correct the declaration.
Seems like you have no use for these variables
int apple;
int orange;
int cherry;
int banana;
int peach; |
1 2 3 4 5 6 7
|
void spin() // Procedure to spin fruit on spinner
{
int fruit[5];
result = rand() % 5;
}
|
Why are you declaring 'fruit[5]' here?
Your probability logic is very incorrect. You need to find the cumulative probability. That is,
if
int fruit[5] = {30, 55, 75, 90, 100};
then
cumulative[5] = {30, 85, 160, 250, 300}; |
And generate a random number from 0 to cumulative[4] that is 300.
Then you check whether the randomly generated number is the ranges [1, 30] [31, 85] [86, 160] [161, 250] [251, 300] using simple if statements. Corresponding range will give you the fruit that has been selected.
To display the probability of getting that fruit simply divide the range's length by cumulative[4].