# Classwork 12: more while loops

## Objectives

more practice with while loops.

## The Assignment: I'm thinking of a number (again)

For this assignment, you may work in a team of 2 people if you would like to. To do this, submit using one person's account. The other person should submit a file that says who they worked with (include both real name and user name). One person should be the "driver". The driver is the person who has the keyboard and does the typing. The other person watches out for bugs.

Write a program that plays the other side of the game "I'm thinking of a number" from Homework 5. This time, the user is thinking of a number and your program will "guess".

Your program should have two variables: lowEnd and highEnd. The loop invariant is that your program "knows" that the user's number is between lowEnd and highEnd inclusively. Initially, lowEnd is 1 and highEnd is 100. Each time your program "guesses" the number half-way between lowEnd and highEnd. If the guess is low, it becomes the new lowEnd. If the guess is high, it becomes the new highEnd.

For example, a sample run of your program might look like (user's response in orange):

```PT[154]% ./a.out
Think of a number between 1 and 100.
I will guess the number, then tell me if my guess is
too high (enter 'h'), too low (enter 'l') or correct
(enter 'y' for 'yes').

Is it 50? [(h)igh, (l)ow, (y)es] h
Is it 25? [(h)igh, (l)ow, (y)es] l
Is it 37? [(h)igh, (l)ow, (y)es] l
Is it 43? [(h)igh, (l)ow, (y)es] h
Is it 40? [(h)igh, (l)ow, (y)es] h
Is it 38? [(h)igh, (l)ow, (y)es] y
Yay! I got it!
PT[155]%
```

## Notes

• Use scanf() and char variables to read in a single character response from the user, like this:

```scanf("%c%c", &reply, &cr) ;
```

The character entered by the user will be stored in reply and the carriage return will be stored in cr. (You will just ignore the cr variable.)

• You can use == to compare character values. Also, character constants are in single quotes. So you can use:

```if (reply == 'y') {
...
}
```

• To help debugging and to help you see what is happening with your while loop, print out the values of lowEnd and highEnd inside the while loop.

## Submit

Use the script command to record a sample run of your program. Don't forget to exit from script. Then, submit your program and sample run using.

```submit cs104_chang cw12 guess.c
submit cs104_chang cw12 typescript
```

Be sure to logout completely when you have finished!