CPSC170A
Fundamentals of Computer Science II

Lab 7

Iteration

Gradient

Details

Create a C++ program that prints a PPM file of a color gradient like the following:

Recall that a PPM file begins with the three lines:

P3
WIDTH HEIGHT
255

Where WIDTH and HEIGHT are the actual width and height of the image in pixels. All subsequent lines contain the RGB values of pixels left to right, top to bottom. Each pixel is three numbers, the red, green, and blue value of the color. And each RGB value is an integer from 0, dark, to 255, light.

Use file redirection to create a PPM file that you can open.

$ ./gradient > gradient.ppm


Validate Input

Details

Create a C++ program that prompts the user to enter a single digit number. If the user enters a number that isn’t a single digit number, it should inform the user that the input was invalid and prompt the user for another single digit number. This should repeat until the user enters a single digit number. When the user enters a single digit number, the program should end.

Example

Enter an integer in the range 0-9: 10
Sorry, 10 is not in the range 0-9.
Enter an integer in the range 0-9: -1
Sorry, -1 is not in the range 0-9.
Enter an integer in the range 0-9: 0


Higher Lower

Details

Create a program that plays the game higher lower. The program should pick a random number and repeatedly prompt the user to enter a number until the user guesses the number. After each guess, the program should inform the user if their guess was too high or too low.

The program should only allow the user to enter valid numbers. Encapsulate and generalize your validate input code from the previous problem to make this easier.

Example

I have generated an integer in the range 0-20.  Guess the number!
You have 3 guesses left.
Enter an integer in the range 0-20: 100
Sorry, 100 is not in the range 0-20.
Enter an integer in the range 0-20: -1
Sorry, -1 is not in the range 0-20.
Enter an integer in the range 0-20: 10
Too low.
You have 2 guesses left.
Enter an integer in the range 0-20: 15
Too low.
You have 1 guess left.
Enter an integer in the range 0-20: 17
Too high.
You didn't guess it.  It was 16.