Homework 2 - More Flow Control

Due Wednesday September 28, 2011 at 5:00pm
NOTE: No late assignments will be accepted beyond 5:00pm on Friday September 30th so that the solutions can be posted early enough to allow studying before Midterm 1 on Monday October 3rd.

The purpose of this assignment is to work with while and do-while loops to do more complex and realistic programs, such as calculating math sequences or presenting a text based menu program.

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

  1. (10 points) Write a program that uses while loops to calculate the first n Fibonacci numbers. Recall from math the following definition of the Fibonacci sequence:
    The Fibonacci numbers Fn are defined as follows. F0 is 1, F1 is 1 and Fi+2 = Fi + Fi+1 for 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, 8, and 13.
    The program should prompt the user for n (the number of Fibonacci numbers) and print the result to the screen. If the user enters an invalid value for n (n <= 0), print an error message and ask the user to re-enter n (an input validation loop for n). This MUST be a loop, not an if statement like Lab 2.

    The output should be similar to the following:

    Enter the number of Fibonacci numbers to compute: 3
    The first 3 Fibonacci numbers are:
    1 1 2
    
    You should verify that your output prints just the desired number of Fibonacci numbers and not any extra numbers. Note that you will need to make special cases when the user wants just the first or first two Fibonacci numbers since you will not need to enter the Fibonacci loop in those cases.

    Hint: Declare variables to store the Fibonacci number calculated in the last iteration of the loop (Fi+1) and the number calculated two iterations ago (Fi).

  2. (10 points) Create a program that uses do-while loops and the if-else if-else statement to create the following menu:
      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 if-else if-else statment to implement the menu options. Within each block for a menu option, do the corresponding menu item. This may involve using a nested if statement. For example, with option 1, you would ask for two integers, multiply them and print the result of the multiplication to the screen. Embed the if-else if-else statement inside a do-while statement.

    The pseudocode is:

    start of do-while:
      print menu to screen
      read in menu selection
      if the user selected 1
        prompt for two integers
        read in two integers
        print out the result of multiplication
      else if the user selected 2
        prompt for two integers
        read in two integers
        print out the result of division (use type casting to do double division)
      else if the user selected 3
        prompt for one integer
        read in the integer
        if the integer is in the range 10-20
          print out that it is in range
        else
          print out that it is out of range
        end-if
      else if the user selected 4
        prompt for three integers
        read in three integers
        if the first integer is the smallest (you will need to figure out the Boolean expression)
          print out that the first is the smallest
        else if the second is the smallest
          print out that the second is the smallest
        else
          print out that the third is the smallest
        end-if
      else if the user selected 0
        print out a goodbye message
      else
        print out an error message that the option is not supported
      end-if
    repeat until 0 is selected (Boolean for do-while)