Homework 2

The homework is due on Wednesday, April 19, 2006 at 5pm.

Name your files hw02_<problem>.cpp. Email all files to my Helios account.

  1. 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 function that computes the first x Fibonacci numbers and prints the numbers to the screen. The program should loop asking the user to enter x and calling the function to computing the first x Fibonacci numbers, until the user enters 0.
  2. From the book, Chapter 3, Programming Project 14, page 117:
    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, ...).
    As the book suggest, you use nested loops 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.
  3. 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:
    
    As before, the menu should loop until the user enters the selection 0. Change your switch() statement to use function calls for each menu option rather than placing the statements within each case statement. You will actually have functions calling functions within this program. Here is a list of functions you will need to define:
    1. From within each case statement, there will be a void function that handles the task of getting user input, calling the function to do the task and printing output to the screen. For example, the first menu item case statement would be:
        case 1:
          do_multiply();
          break;
      
      The do_multiply() function asks the user for two integers, calls the multiply function and prints the result of that multiplication to the screen. The do_division() function asks the user for two integers, calls the division function and prints the result to the screen. The do_check() function asks the user for an integer, calls the check range function and prints "within range" if it is within range or "outside of range" if it is not. The do_minimum() function asks the user for three integers, calls the minimum function and prints the result to the screen.
    2. The function to multiply two integers should take two integers as the arguments and return an integer.
    3. The function to divide two integers should take two integers and return a double. Use type casting within the function on one of the integers so you do double division and not integer division.
    4. The function to check the range takes an integer and returns a boolean. It should return true only if the integer x is 10 ≤ x ≤ 20.
    5. The function to find the minimum of a list of numbers takes three integer arguments and returns an integer.