Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a program in which I am given a pre-written code framework that has some

ID: 3545812 • Letter: I

Question

I have a program in which I am given a pre-written code framework that has some missing functions. I need to write the functions underneath every "Fill code here" prompt. Because it would be tedious for me to write down all of the code instructions, they can be accessed through this link:


https://www.dropbox.com/s/q515blhah2xi3ge/rules.docx


The document may seem long, but it is mostly just filled with example test cases so you can get a clearer view of how exactly the program should look like. I have already begun to try to write each function, but I know that I'm very wrong because my program doesn't work and I am not that familiar with pointers. The given code framework, along with my current efforts, can be found in the following link:


https://www.dropbox.com/s/s0ljrtjidnkwnth/current_work.docx


As stated above, the only part of the code that needs to be created is under each fill in prompt. I have put all of the prompts in bold so they stand out. All of the other code is correct, so the functions created should work with that code.


Also, due to the long length of the program, I would really appreciate if the answer/correction to the program is provided through a link in which I can view it with all of its indentations (chegg doesn't show indentations).


Thank you so much!

Explanation / Answer

Please see here: https://app.box.com/s/tygia0ccwd1ob0m5e95t




#include <stdio.h>

#include <stdlib.h>

#include <time.h>

// Costs for buying and selling a chip.

#define CHIP_BUY 11

#define CHIP_SELL 10

// Used to determine the status of a game.

#define WIN 1

#define LOSS 0

#define CONTINUE 2


int pairofdice();

int craps();

int craps_outcome(int roll);

int gamedice();

int game_outcome(int roll);

void statusreport(int numchips, int cash);

int buychips(int *cash);

int sellchips(int numchips);

void dobuy(int *cash, int *numchips);

void dosell(int *cash, int *numchips);

int menu();

int validchips(int *chipsbet, int numchips);


int main() {

// Set up variables and initial values.

int choice, cash=1000;

int numchips=0, chipsbet, numsell;

srand(time(0)); // Initialize random number generator.

// Continue until the user quits.

while ((choice = menu()) != 6) {

// Execute the appropriate choice.

if (choice == 1) {

dobuy(&cash, &numchips);

}

else if (choice == 2) {

dosell(&cash, &numchips);

}

else if (choice == 3) { // Only play if a valid number of chips is bet.

printf("How many chips would you like to bet? ");

scanf("%d", &chipsbet);

if (validchips(&chipsbet, numchips)) {

// Adjust current number of chips based on the game outcome.

if (craps())

numchips += chipsbet;

else

numchips -= chipsbet;

}

}

else if (choice == 4) {

// Only play if a valid number of chips is bet.

printf("How many chips would you like to bet? ");

scanf("%d", &chipsbet);

if (validchips(&chipsbet, numchips)) {

// Adjust current number of chips based on the game outcome.

if (gamedice())

numchips += chipsbet;

else

numchips -= chipsbet;

}

}

else if (choice == 5) {

statusreport(numchips, cash);

}

}

// Sell the remaining chips and print out the final message.

cash += sellchips(numchips);

printf("After selling your chips, you have $%d.",cash);

printf(" Thanks for playing! ");

return 0;

}

// Returns an integer in between 2 and 12, inclusive, representing the

// result of rolling two fair six-sided dice.

int pairofdice() {

/*** FILL IN CODE ***/

int die1, die2, roll_sum = 0;

die1 = 1 + rand()%6;

die2 = 1 + rand()%6; roll_sum = die1 + die2;

return roll_sum;

}

// Let's the user play craps. Returns 1 if the user wins, 0 otherwise.

int craps() {

/*** FILL IN CODE ***/

int first_roll, second_roll, status;

char dummy[50];

// Execute the first die roll.

printf("Press 'r' and hit enter for your first roll. ");

scanf("%s", dummy);

first_roll = pairofdice();

printf("You rolled a %d. ", first_roll);

// Determine the current game status.

status = craps_outcome(first_roll);

// Go ahead and execute the second die roll if necessary.

if (status == CONTINUE) {

// Execute the second die roll.

printf("Press 'r' and hit enter for your next roll. ");

scanf("%s", dummy);

second_roll = pairofdice();

printf("You rolled a %d. ", second_roll);

// Determine if the user won or not.

while (second_roll != 7 && second_roll != first_roll){

printf("Press 'r' and hit enter for your next roll. ");

scanf("%s", dummy);

printf("You rolled a %d. ", second_roll);

}

if (second_roll == first_roll)

status = WIN;

else if (second_roll == 7)

status = LOSS;

}

// Return the final outcome of the game.

if (status == WIN) {

printf("You win! ");

return WIN;

} else {

printf("Sorry, you have lost. ");

return LOSS;

}

return 0;

}

// Returns the status of a craps game based on the first roll.

int craps_outcome(int roll) {

/*** FILL IN CODE ***/

if (roll == 7 || roll == 11)

return WIN;

else if (roll == 2 || roll == 3 || roll == 12)

return LOSS;

return CONTINUE;

}

// Let the user play Game of Dice.

// Returns 1 if the user wins, 0 otherwise.

int gamedice() {

int first_roll, second_roll, status;

char dummy[50];

// Execute the first die roll.

printf("Press 'r' and hit enter for your first roll. ");

scanf("%s", dummy);

first_roll = pairofdice();

printf("You rolled a %d. ", first_roll);

// Determine the current game status.

status = game_outcome(first_roll);

// Go ahead and execute the second die roll if necessary.

if (status == CONTINUE) {

// Execute the second die roll.

printf("Press 'r' and hit enter for your next roll. ");

scanf("%s", dummy);

second_roll = pairofdice();

printf("You rolled a %d. ", second_roll); // Determine if the user won or not.

if (second_roll > first_roll)

status = WIN;

else

status = LOSS;

}

// Return the final outcome of the game.

if (status == WIN) {

printf("You win! ");

return WIN;

}

else {

printf("Sorry, you have lost. ");

return LOSS;

}

}

// Returns the status of Game of Dice, based on the first roll.

int game_outcome(int roll) {

/*** FILL IN CODE ***/

if (roll == 11 || roll == 12)

return WIN;

else if (roll == 2)

return LOSS;

return CONTINUE;

}

// Prints out the user's current status.

void statusreport(int numchips, int cash) {

/*** FILL IN CODE ***/

printf("You currently have $%d left and %d chips. ", cash,

numchips);

}

// Adjusts the variable pointed to by cash to equal the change received

// from purchasing the maximum possible number of chips for the value

// of that variable, originally. Returns the total number of purchased

// chips.

int buychips(int *cash) {

/*** FILL IN CODE ***/

int numchips;

numchips = *cash/CHIP_BUY;

*cash = *cash - numchips*CHIP_BUY;

return numchips;

}

// Returns the cash gained by selling numchips chips.

int sellchips(int numchips) {

/*** FILL IN CODE ***/

int cash;

cash = numchips*CHIP_SELL;

return cash;

}

// Prints out the menu for the user and returns the user's choice.

int menu() {

/*** FILL IN CODE ***/

int answer = 0;

printf("Welcome to the Casino. Here are your choices: ");

printf("1) Buy chips ");

printf("2) Sell chips ");

printf("3) Play Craps ");

printf("4) Play Game of Dice ");

printf("5) Status Report ");

printf("6) Quit ");

while(1){

scanf( "%d", &answer);

if (answer < 1 || answer >6)

printf("Please enter a valid choice between 1 and 6. ");

else

break;

}

return answer;

}

// Determines if the user has entered a valid number of chips to bet.

// chipsbet is a pointer to the variable storing the number of chips the

// user is betting and numchips is the current number of chips the user

// has. 1 is returned if the bet is valid, 0 is returned otherwise.

int validchips(int *chipsbet, int numchips) {

/*** FILL IN CODE ***/

if (*chipsbet > numchips) {

printf("Sorry, you do not have that many chips to bet. No game played. ");

return 0;

}

else if (*chipsbet == 0) {

printf("Sorry, that is not allowed. No game played. ");

return 0;

}

return 1;

}

// Executes buying chips. Both cash and numchips are pointers to the

// variables storing the user's amount of cash and number of chips,

// respectively.

void dobuy(int *cash, int *numchips) {

/*** FILL IN CODE ***/

int money;

int num;

printf("How much cash do you want to spend for chips? ");

scanf("%d", &money);

if (money > *cash)

printf("Sorry, you do not have enough money. No chips bought. ");

else

{

(*cash) -= money;

num = buychips(&money);

(*numchips) += num;

(*cash) += money; // add left

}

}

// Executes selling chips. Both cash and numchips are pointers to the

// variables storing the user's amount of cash and number of chips,

// respectively.

void dosell(int *cash, int *numchips) {

int numsell;

// Determine the number of chips to be sold.

printf("How many chips do you want to sell? ");

scanf("%d", &numsell);

// Print out the error message if this is too much.

if (numsell > *numchips)

printf("Sorry, you do not have that many chips. No chips sold. ");

// Execute the transaction.

else {

(*cash) += sellchips(numsell);

(*numchips) -= numsell;

}

}