Homework 2 - Chapter 3

Due Monday, October 2, 2006 at 5pm.

Name your files hw2_<problem>.cpp, such as hw2_1.cpp. Email all the cpp files to my Helios account.

  1. From the book, Chapter 3, Programming Project 1, p171:
    Write a program to score the rock-paper-scissor game. Each of two users types in either P, R or S. The program then announces the winner as well as the basis for determing the winner...
    See the book for the remainder of the programming project. You will have two input prompts asking each user for their P, R, S selection. You will then use if-else is-else statements to determine who has won. Use a loop to ask if the users wish to play again. Your output should appear to be something like the following:
    User 1 select P, R or S: R
    User 2 select P, R or S: S
    
    User 1 wins. Rock breaks scissors.
    Play again? (y|n) y
    
    User 1 select P, R or S: P
    User 2 select P, R or S: S
    
    User 2 wins. Scissors cut paper.
    Play again? (y|n) n
    
  2. Modified from the book, Chapter 3, Programming Project 10, p 175:
    The Fibonacci numbers Fn are defined as follows. F0 is 1, F1 is 1 and Fi+2 = Fi + Fi+1 i = 0, 1, 2, ... . In other words, each number is the sum of the previous two numbers. The first few Fibonacci numbers are 1, 1, 2, 3, 5, and 8. ...
    Write a program that computes the first x Fibonacci numbers and prints the numbers to the screen. The program should continue asking the user for x until the user enters 0. The output should appear as follows:
    Enter the number of Fibonacci numbers to compute (0 to quit): 1
    The first 1 Fibonacci numbers are:
    1
    
    Enter the number of Fibonacci numbers to compute (0 to quit): 3
    The first 3 Fibonacci numbers are:
    1 1 2
    
    Enter the number of Fibonacci numbers to compute (0 to quit): 0
    
  3. From the book, Chapter 3, Programming Project 14, p 177:
    Write a program that finds and prints all of the prime numbers between 3 and 100. A prime number is a number such that one and itself are the only numbers that evenly divide it (e.g., 3, 5, 7, 11, 13, 17, ...). ...
    See the book for the hint about how to solve this problem. There is actually a more optimal range for the inner loop than what the book suggests. Think about the nature of factoring to see if you can find the more optimal range for your program.
  4. Modify your simple menu program from Homework 1 as follows. Change the menu to support the following options:
      Welcome to the CS221 Homework 2 Menu
      ====================================
      1.  Multiply two integers
      2.  Divide two integers
      3.  Check if a number is within the range 10-20
      4.  Find the minimum of a list of 3 numbers
    
      0.  Exit
      ====================================
      Enter selection:
    
    Use the switch statment to implement the menu options instead of if-else statements. Within each case for the menu option, do the corresponding menu item. For example, in case 1, you would ask for two integers, multiply them and print the result of the multiplication to the screen.