CSE110 Spring 2001

Homework #3

Due Friday, February 16th at 5pm

Note: bold text in the examples is input from the user.


  1. [25 points] One of your shoes has begun to think it's better than you. Your plan is to put it back in its place by humiliating it with your math skills. Write a program to show off these skills. The program should first ask for two real numbers, then ask what you think the sum will be (another real number), and finally report whether or not you were right. The program should run inside a loop that asks if you want to try another sum (accepting `y' or `n'), and should repeat if you do.

    Example:

    % a.out
    Give me a number: 6.4
    Give me another number: 2.8
    Guess the sum: 9.2
    Right!
    Another? y
    Give me a number: 6.4
    Give me another number: 1.6
    Guess the result: 7
    Wrong!
    Another? n
    
  2. [30 points] Your talent with sums didn't seem to have any effect on your shoe -- it still sticks out its tongue every time you put it on, and it's started to sour your relationship with your boots, to boot. Perhaps if you show it you can do subtraction, multiplication, and division, instead of only addition, it might start to show you some respect. Change your program to prompt you for a one-character operator (`+', `-', `*', or `/'), in addition to the real numbers for the operands and your guess at the result, and use this operator to determine the operation to check.

    Example:

    % a.out
    Give me a number: 6.4
    Give me an operator: -
    Give me another number: 2.8
    Guess the result: 3.6
    Right!
    Another? y
    Give me a number: 6.4
    Give me an operator: /
    Give me another number: 1.4
    Guess the result: 4
    Wrong!
    Another? n
    
  3. [30 points] Your plan doesn't seem to be working, but if you put yourself in your shoe's shoes for a minute you might see why: no longer content to follow in your footsteps, it craves the chance to stand toe-to-toe with its master in a competition to guess the factors of a number. Write a program that lets the two of you take turns doing just that. The program should first ask you for a number, then ask your shoe to guess a factor of that number, printing out whether the number can indeed be divided evenly by the factor (don't reward a guess of one or the number itself -- that's too easy). Then the shoe's on the other foot, and it's your turn to guess a factor of a number that your shoe picks. When both sides have finished, the program should ask you if you want to keep playing (again, `y' or `n' will suffice), and should start another round if you do.

    Also, won't it be fun to keep score? Of course it will, so have the program print the score when it asks if you want to keep playing.

    Example:

    % a.out
    Pick a number: 156
    Now let your shoe guess a factor: 3
    Yes, 3 is indeed a factor of 156.
    Now let your shoe pick a number: 19
    And you pick a factor: 3
    Nope, 3 is not a factor of 19.
    Score is 1 for your shoe, 0 for you.  Keep playing? y
    Pick a number: 154
    Now let your shoe guess a factor: 11
    Yes, 11 is indeed a factor of 154.
    Now let your shoe pick a number: 17
    And you pick a factor: 1
    Sorry, 1 is a factor of 17, but it's too easy.
    Score is 2 for your shoe, 0 for you.  Keep playing? n
    You have lost the game to your shoe.