Lab-2

If-else Statement and while loop

Announcements

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 Visual Studio or Xcode environment to compile & run your code. 

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;

}

1-17-2017