Lab 2
Announcements
We will have a simple 5 minutes’ quiz at the beginning of the lab on Blackboard. Please sign in to your Blackboard account and finish the quiz individually. Most of your time should be focussed on finishing the lab activity questions.
You must work with a single partner, in a group of 2. You must take turns being the “designated driver” and the “non-driver”. (If there are an odd-number of students in the class, your TA will make an adjustment.)
Lab Grading policy
You must finish at least question 0 to get any points. The maximum score is 2, where each question is worth 1 point. Question 3 is extra credit.
To get a grade you must raise your hand once you are ready to show your work to your TA during the last 10 minutes of lab. Late submission is not accepted.
Lab Activity
Guessing Game: Write a program to play a 0/1 number guessing game against computer. At each move first the computer makes a forecast of 0 or 1, then you make your guess. The score starts at 0. If the computer correctly forecasts (matches) your choice, we subtract 1 from the score. If the computer doesn't match your move, we add 1 to the score. In this way the score either goes up or goes down on each move.
A sample of this game (written in Scratch) is shown here. Our version will not be graphical but will be text-based, and should be written in stages.
Steps You Need to Do
0. (0 points) Use the lab Mac machines XCode environment to run a sample code.
Ahead of time you should watch this video on how to run a simple Hello World program in XCode
(1 point) Declare all the required variables:
userinput should be an integer variable
Initialize the computerForecast variable as 1
Initialize the score variable as 0
Prompt the user for input once.
Compare the user input with the computer forecast and update the score accordingly.
Display the score as the output.
(Preparation: Refer Zyante section 2.1 for how to declare and initialize a variable and Zyante section 3.1 for if-else branching.)
Sample lab stage 1 output (where user input is shown in bold):
Enter your input (0 or 1): 0
Computer's forecast was 1
Score is 1
Program ended with exit code: 0
(1 point) Use a loop to repeatedly get the user’s input until the user enters -1. On every new user input, set the computer’s prediction to be the opposite of the user’s input.
Display the computer’s forecast and the current score each time through the loop.
(Preparation: Refer Zyante section 4.2 for While loops. )
Sample lab stage 2 output (where user input is shown in bold):
Question 1 sample run:
Enter your input (0 or 1): 1
Computer's forecast was 1
Score is -1
Question 2 sample run:
Enter your input (0 or 1): 1
Computer's forecast was 0
Score is 0
Enter your input (0 or 1): 1
Computer's forecast was 0
Score is 1
Enter your input (0 or 1): -1
Program ended with exit code: 0
(Extra Credit, for 1 point) : Use a random number generator to change the computer behavior such that it makes its 0 or 1 prediction at random every 4th move, so that the human opponent can't discover its algorithm.
(Preparation: Refer Zyante section 2.16 for the random number generator.)
Sample Code
Refer the sample code below as a starting point. Your code doesn't necessarily have to look this way.
/*
* Refer to this code as a starting point
*/
#include <stdio.h>
int main() {
int userInput;
// Question 1
// Declare and initialize variables for score and forecast here
// Get user input similar to the 2 lines below
printf("Enter a positive integer: ");
scanf("%d", &userInput);
// Write code to compare user input with the initial value of forecast and compute the score
if (...) {
//statements
} else {
//statements
}
// Write code to display the user input, forecast and score here
/* Question 2 - complete the code below
- Get the user input inside a loop until the user enters -1.
- Make forecast as the opposite of user input
- display user input, forecast, score
*/
while ( ... ) { //Check if the input is -1 here
// move the code to compare user input with
// the initial value of forecast and compute the score
//Write code to display the user input, forecast and score here
// Write code to make predictions based on user input
// The prediction is the opposite of the user input
//prompt to get the user input
}
return 0;
}