SI204 Lab 4

1. Write a program that reads in a list letter grades via the keyboard and reports the GPA of the list and creates a histogram for each letter grade.  Assume all courses have 4 credit hours.  Instruct the user to terminate the list in any manner you wish at any time they wish, i.e. sentinel, flag, or end-of-file via keyboard.  The input should be read in using the char variable type.  Ensure the input is only an upper or lower case 'A', 'B', 'C', 'D', or 'F', or a key signifying the end of input data. 

 

Note that:

 

A = 4.0

B = 3.0

C = 2.0

D = 1.0

F = 0.0

 

Example run:

 

Input a letter grade: A

Input the next letter grade ('Q' to quit): a

Input the next letter grade ('Q' to quit): b

Input the next letter grade ('Q' to quit): C

Input the next letter grade ('Q' to quit): c

Input the next letter grade ('Q' to quit): d

Input the next letter grade ('Q' to quit): E

Error: only input a letter grade ('Q' to quit): F

Input the next letter grade ('Q' to quit): Q

 

A: **

B: *

C: **

D: *

F: *

 

GPA:  2.285714

 

 

2.  Write a program that asks the user to think of a number between 0 and 1,000,000 (inclusive) and then guesses the number in 20 attempts or less.  After each computer guess, the user indicates higher, lower, or correct using the 'H', 'L', and 'Y' keys (upper or lower case).  Any other input should generate an error statement.

 

A strategy is to find a midpoint between the upper and lower bounds.  Ask the user if that's their number.  When the user answers higher or lower make the midpoint the new lower or upper bound, respectively.

 

Example run  (The number is '112233'):

 

Let's play Twenty Questions

Think of an integer between 0 and 1,000,000 (inclusive) and DON'T FORGET IT!

 

Directions:  The computer will attempt to guess your number.

             Enter 'H' for higher.

             Enter 'L' for lower.

             Enter 'Y' for yes.

             You win if the computer makes more than twenty guesses.

 

Guess #1:  Is your number 500000?  l

Guess #2:  Is your number 250000?  l

Guess #3:  Is your number 125000?  L

Guess #4:  Is your number 62500?  h

Guess #5:  Is your number 93750?  H

Guess #6:  Is your number 109375?  H

Guess #7:  Is your number 117187?  l

Guess #8:  Is your number 113281?  L

Guess #9:  Is your number 111328?  P

ERR: The only valid entries are 'H' for higher, 'L' for lower and 'Y' for yes.

Guess #9:  Is your number 111328?  H

Guess #10:  Is your number 112304?  l

Guess #11:  Is your number 111816?  h

Guess #12:  Is your number 112060?  H

Guess #13:  Is your number 112182?  H

Guess #14:  Is your number 112243?  l

Guess #15:  Is your number 112212?  H

Guess #16:  Is your number 112227?  h

Guess #17:  Is your number 112235?  L

Guess #18:  Is your number 112231?  H

Guess #19:  Is your number 112233?  Y

The computer guessed your number in 19 tries!

 

 

3. Modify your solution to Problem 1 so that the histogram gets printed out vertically, not horizontally. For the example provided, you should get:
*   *    
* * * * *
A B C D F