-
Need help!!!
so i was given the assignment where i am suppose to use the random number generator to simulate dice throws and also calculate the probability of the dice landing on a specific facevalue.
Here is my program....
/* put this in a program */
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 6 /* symbolic constant */
int main()
{
int faceValue = 0 ;
float probability[MAX] = {0} ;
int run = 0 ;
int counter = 0 ;
srand(time(0));/*random number generator*/
printf("How many times do you want to throw the die? \n");
scanf("%d",&run);
faceValue = rand() % 6 + 1;
switch(faceValue){
case 1:
faceValue == 1;
break;
case 2:
faceValue == 2;
break;
case 3:
faceValue == 3;
break;
case 4:
faceValue == 4;
break;
case 5:
faceValue == 5;
break;
case 6:
faceValue == 6;
break;
}
for (counter =1; counter <= MAX; counter++)
{
printf(" The probability for number %d is %0.2f %\n", counter, (probability[counter]/12)* 100);
}
return 0;
}
the problem is when i compile i don't get the calculations correct for the probability....could someone please point me to the write direction cause i do not no where to go from here
-
This is comparison. Use = for assignment.
And array indexes run from 0 to elements - 1 (so your array is 0...5, not 1...6).
-
1)
Your switch statement makes no sense. The variable being tested is faceValue, so let's say faceValue is 5:
Now at case 5, you compare faceValue to 5 ?? Of course it's five, that's how your code got to this line! :)
And please DON'T assign faceValue to 5 either, because faceValue ALREADY IS five (or whatever number).
You need another variable to be altered in the switch statement, not faceValue.
2) You need a for loop wrapped around the switch statement, starting right after the prompt "How many times do you want to throw the dice?"
Code:
for(i = 0; i < throws_wanted; i++) {
//other code for the throw here
//switch statement here
} //end of for
//print up probabilities for all the throws here.