I am trying to create a function that rolls a die, and then returns its number t
ID: 3624162 • Letter: I
Question
I am trying to create a function that rolls a die, and then returns its number to main. However, every time I compile, I get an error code stating 'expected expression before 'int' in line 11' i.e. in the y =... line. Can anyone help me figure out what change to make so it will compile and execute?#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int roll_dice(void);
int main(void){
srand(time(NULL));
int y;
y = int roll_dice(y);
printf("%d" , y);
return 0;
}
int roll_dice(void){
int x;
x = 1 + (rand() % 6);
return x;
}
Explanation / Answer
#include #include #include main() { int rollADie = 0, rollCounter = 0, key = 0, stop = 0, BigCounter = 20000, firstRollWins = 0, otherWins = 0, firstRollLoses = 0, otherLoses = 0, total,totalWins, totalLoses; double percentage; srand(time(NULL)); for(BigCounter; BigCounter != 0; BigCounter--){ system("cls"); stop = 0; key = 0; rollADie = 0; rollCounter = 0; do{ //first roll rollADie += rand() % 6 + 1; //in the range of 1-6 rollCounter++; }while(rollCounter < 2); key = rollADie; printf("You rolled: %i ", key); //if first roll is 7 or 11 you win if(key == 7 || key == 11){ printf("You Win! "); firstRollWins++; } else if(key != 2 & key != 3 & key != 12){ //keep rolling till key is rolled while(stop != key){ rollADie = 0; rollCounter = 0; do{ rollADie += rand() % 6 + 1; //in the range of 1-6 rollCounter++; }while(rollCounter < 2); stop = rollADie; printf("You rolled: %i ", stop); if(stop == key){ printf("You Win! "); otherWins++; } //if you roll 7 now, you lose if(stop == 7){ printf("You Lose! "); otherLoses++; stop = key; } } } // if you roll 2, 3 or 12 on first roll, you lose else if(key == 2 || key == 3 || key == 12){ printf("You Lose! "); firstRollLoses++; //counts first roll loses } } system("cls"); //clear screan total = firstRollWins + otherWins + firstRollLoses + otherLoses; totalWins = firstRollWins + otherWins; totalLoses = firstRollLoses + otherLoses; printf("First roll loses: %i ", firstRollLoses); printf("Other loses: %i ", otherLoses); printf("First roll wins: %i ", firstRollWins); printf("Other wins: %i ", otherWins); percentage = ((double)totalWins / total) * 100; printf("Chances of winning: %.2lf percent ", percentage); percentage = ((double)firstRollWins / total) * 100; printf("Chances of winning on first roll: %.2lf percent ", percentage); percentage = ((double)otherWins / total) * 100; printf("Chances of winning on key roll: %.2lf percent ", percentage); system("pause"); }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.