In: Computer Science
Objective
Make a function to serve as a Craps dice game simulator. If you are not familiar Craps is a game that involves rolling two six-sided dice and adding up the total. We are not implementing the full rules of the game, just a function that returns the value of BOTH dice and their total.
Details
In the function you create: Make a function that uses pointers and pass by reference to return THREE separate outputs (arrays are not allowed). Inside the function you will call rand() to represent a random dice roll of a six sided dice. One output will be the first call to rand() and represent the first dice roll. The second output will be a second call to rand() and represents the second dice roll. The third output is the total of the two other outputs added together.
You may choose either options for defining the function:
Option A:
Option B:
In the Main Function: Call srand() to initialize the pseudo-random algorithm. Create three variables to hold your outputs, the two dice rolls and the total. In a for loop call your dice roll function FIVE times, each time it should set the three variables to new values. Also within the for loop print the values of the dice rolls and the total.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void dice_roll(int *roll_1, int *roll_2, int *total);
int main(void)
{
// Initialize srand function
srand(time(NULL));
// Create three variables to hold your outputs
int roll_1, roll_2, total;
// In a for loop call your dice roll function FIVE times
for(int i=0; i<5; i++)
{
// call dice_roll each time to set the three variables to new values
dice_roll(&roll_1, &roll_2, &total);
// Print the result
printf("Roll 1: %d, Roll 2: %d, Total: %d\n", roll_1, roll_2, total);
}
return 0;
}
// Dice roll function
void dice_roll(int *roll_1, int* roll_2, int* total)
{
// Generate a random number between 1 and 6 and assign to roll_1 and roll_2
*roll_1 = rand() % 6 + 1;
*roll_2 = rand() % 6 + 1;
// Add the two roll to get the total
*total = *roll_1 + *roll_2;
}
*******************************************OUTPUT******************************************
Roll 1: 5, Roll 2: 1, Total: 6
Roll 1: 2, Roll 2: 5, Total: 7
Roll 1: 6, Roll 2: 5, Total: 11
Roll 1: 6, Roll 2: 3, Total: 9
Roll 1: 5, Roll 2: 4, Total: 9