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

Objectives: Understand MATLAB syntax for: Comment headers switch statement Bool

ID: 3801293 • Letter: O

Question

Objectives: Understand MATLAB syntax for: Comment headers switch statement Bool data type while loop if-else statements Random generator function (randi) printf function hw08. Background Craps is a dice game in which the players make wagers on the outcome of the roll, or series of rolls, of a pair of dice'. To start a round, the shooter makes a "come-out" roll. A come-out roll of 2, 3 or 12 is called "craps" and is an immediate loss. A come-out roll of 7 or 11 is a "natural", and is an immediate win. The other possible rolls are called the "point" numbers: 4, 5, 6, 8, 9, and 10. If the shooter rolls one of these numbers on the come-out roll, this establishes the "point", and the point number must be rolled again before a seven on subsequent rolls in order to win. Assignment: You will rewrite the craps game using MATLAB. The code will be written as a script file. Give the user 5 chips initially. Subtract 1 for a loss and add one for a win. Stop when the user is out of chips or has 10 chips. Output the number of chips at the end of each game. Your program must do the following Use a while loop for when 0 chips 10. Use a while loop when attempting the point value Stop when chips are 0 or greater than 10. Sample output You rolled 6 and a 3 You rolled 5 and a 2 Point is 9. Now you will roll again You win! You rolled 6 and a 4 CHIPS Push on roll again. You rolled 5 and a 4 You rolled 3 and a 4 Point is 9. Now you will roll again Bad roll you lose You rolled 1 and a 3 Push on roll again. CHIPS 4 You rolled 2 and a 1 You rolled 5 and a 4 craps. You lose Rolled the point. You win. CHIPS 3 Chips 5

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

int main ()
{
int i,d1,d2,sumd,sumd2;
double winf = 0, lostf = 0, winp = 0, lostp = 0;
printf("This program will simulate the game of craps for 100 times. ");

for (i=0; i<100; i++) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd = d1 + d2;

if (sumd==7 || sumd==11) {
printf("You rolled a 7 or an 11, you win. ");
winf++;
}
if (sumd==2 || sumd==3 || sumd==12) {
printf("You rolled a 12, a 3, or a 2, you lose. ");
lostf++;
}
if (sumd==4 || sumd==5 || sumd==6 || sumd==8 || sumd==9 || sumd==10) {
while (1) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd2 = d1 + d2;

if (sumd2==sumd){
printf("You rolled your points, you win. ");
winp++;
break;}
if (sumd==7){
printf("You rolled a 7, you lose. ");
lostp++;
break;}
}
}
}

printf("First roll wins: %lf, First roll loses: %lf, Second roll wins: %lf, Second roll loses: %lf. ", winf, lostf, winp, lostp);
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

int main ()
{
int i,d1,d2,sumd,sumd2;
double winf = 0, lostf = 0, winp = 0, lostp = 0;
printf("This program will simulate the game of craps for 100 times. ");

for (i=0; i<100; i++) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd = d1 + d2;

if (sumd==7 || sumd==11) {
printf("You rolled a 7 or an 11, you win. ");
winf++;
}
if (sumd==2 || sumd==3 || sumd==12) {
printf("You rolled a 12, a 3, or a 2, you lose. ");
lostf++;
}
if (sumd==4 || sumd==5 || sumd==6 || sumd==8 || sumd==9 || sumd==10) {
while (1) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd2 = d1 + d2;

if (sumd2==sumd){
printf("You rolled your points, you win. ");
winp++;
break;}
if (sumd==7){
printf("You rolled a 7, you lose. ");
lostp++;
break;}
}
}
}

printf("First roll wins: %lf, First roll loses: %lf, Second roll wins: %lf, Second roll loses: %lf. ", winf, lostf, winp, lostp);
}