Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please write in C99 Question 1 Overview Write a C program that simulates the Monty Hall game. Monty Hall is a scenario where you imagine
Please write in C99
Question 1 Overview Write a C program that simulates the Monty Hall game. Monty Hall is a scenario where you imagine being on a game show and you're given a chance to win a car by selecting one of the three doors. Behind one door is a car while behind the other two doors are goats. The car and the goats were placed randomly behind the doors before the show. Monty Hall works according to following rules. 1. The player choose one of the three doors. 2. Monty chooses another door and opens it revealing a goat. (Monty never chooses the door with the car behind it!) 3. Monty asks the player if they want to switch the door choice or keep the original choice. 4. The user may switch by pressing Y or y. Alternatively, the user may choose to not switch by pressing Norn. 5. The door selected by the player is opened and the player wins or loses. The message is displayed accordingly. 6. Monty asks the player if they want to play again. Additional Requirements Put your C code for this question in questioni.c . You should compile the program as ./questioni You must make a ./question 1.sh to compile and run your question 1.c program You must demonstrate the proper use of a switch statement in this question. stdlib.h has a random function that you need to use to generate a number randomly, rando. o To get help on using rand try man 3 rand at your bash prompt! o Do not use srand, or the car will be behind a different door and your output will be wrong! Wait for the user to press enter after the yes-or-no questions. If the user enters more than one letter, ignore the other letters. Only consider the first letter. o For example, Yes should count as yes, no should count as no, Ninetails should also count as no. This is just to keep things simple for you. If you need a string buffer, use a buffer big enough to hold a string 1024 chars long. Represent the doors as integers 0, 1, 2. o The int returned by rand() is a random integer that can be very large. Divide it by three and take the remainder to get a random integer 0, 1, or 2. At the beginning of a game, take 1 random number from rand to decide which door the car is behind. o If the player's first guess is the door with the car, Monty can choose to reveal either goat. For this assignment, have monty choose to reveal the goat behind the door with the lowest number. o Example: Car is behind #1. Player's initial guess is door #1. Monty reveals the goat behind door #2. o If the player's first guess is a door with a goat, Monty must reveal the other goat. 1 LIL PIUCIJIJL YUCJUJU OUI VILIU you, IVIOITTUJLCCU LIL ULICI yout. The car can only move between games. You should only need to modify one number and no strings in your code to change the number of doors. Example Runs Following is an example run of a program in which the car was behind door 2. Choose a door from 1 to 3: 1 You chose door #1. Monty opens door #3, revealing a goat! Would you like to switch to door #2? Y You chose door #2. Monty opens door #2, revealing a car! You win! You've won 1 games out of 1 games. Would you like to play again? n Following is an example of putting in invalid inputs Choose a door from 1 to 3: I don't know Choose a door from 1 to 3: There's only 3? Choose a door from 1 to 3: Okay um... Choose a door from 1 to 3: Choose a door from 1 to 3: 4 Choose a door from 1 to 3: Door 2 Choose a door from 1 to 3: 1 You chose door #1. Monty opens door #3, revealing a goat! Would you like to switch to door #2? Why? Error: please enter y or N: what's wrong with Door #12 Error: please enter y or N: Error: please enter y or N: Maybe. Error: please enter y or N: Error: please enter y or N: Yes Monty opens door #2, revealing a car! You win! Would you like to play again? Not really Marking 1 Point Program output is correct for a valid input. (Examples: test-la.sh) 1 Point Program output is correct for an invalid input. (Examples: test-q1b.sh) 1 Point Program uses switch statements and uses them correctly). 1 Point Quality of question1.c meets the quality standards, listed above. 1 Point question1.sh meets the requirements above. Hints . If you are left with an extra newline after doing scanf, try adding %*c to the scanf. This will eat 1 character (the newline character) but you do not need to provide an argument to scanf and it won't count towards the return value of scanf. See man 3 scanf. It's probably easier to use fgets to read all user responses. If you use fgets, you can convert the string it reads into its first argument to an int using the function atoi. See man 3 atoiStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started