Homework 1

Due Monday September 25, 2006 at 5pm.
Due Tuesday September 26, 2006 at noon due to power outage over the weekend.

Name your files hw1_<number>.cpp, such as hw1_1.cpp for the first program. Email all files as attachments to my Helios account (see Lab 1 for instructions).

  1. Write a program that asks the user for two doubles and two integers. Output the results of dividing double 1 by double 2, double 1 by integer 1 and integer 1 by integer 2. The output should resemble the following:
    Enter double 1:
    5.0
    Enter double 2:
    10.0
    Enter integer 1:
    2
    Enter integer 2:
    3
    
    5.0 / 10.0 is 0.5.
    5.0 / 2 is 2.5.
    2 / 3 is 0.
    
  2. Write a program that asks the user for two integers. Use if/else statements to output the minimum of the two integers. The output should resemble the following:
    Enter integer 1:
    3
    Enter integer 2:
    2
    
    The minimum of 3 and 2 is 2.
    
  3. From the book, Programming Project 2.6 on page 105:
    Write a program that determines whether a meeting room is in violation of fire law regulations regarding the maximum room capacity. The program will read in the maximum room capacity and the number of people to attend the meeting. If the number of people is less than or equal to the maximum room capacity, the program announces that it is legal to hold the meeting and tells how many additional people may legally attend. If the number of people exceeds the maximum room capacity, the program announces that the meeting cannot be held as planned due to fire regulations and tells how many people must be excluded in order to meet the fire regulations.
    Do the version that uses a loop to allow the calculation to be repeated as long as the user answers "y" to the prompt "Do you wish to run the calculation again? [y/n]". This problem uses both if/else statements (to determine if the room capacity has been exceeded) and loops (to ask the user if they want to run the calculation again).

    Hint: See Display 2.14 for how to prompt the user to run the calculations again.

  4. A common use of text interfaces is to write a menu to the screen and ask the user to select an option. We will start with a very simple menu and refine it throughout the quarter. First, print the following menu to the screen:
      Welcome to the CS221 Homework 1 Menu
      ====================================
      1.  Selection 1
      2.  Selection 2
    
      0.  Exit
      ====================================
      Enter selection:
    
    Read in the user's selection. If the selection is 1, print "You have selected menu item 1." to the screen. If the selection is 2, print "You have selected menu item 2." to the screen. If the selection is greater than 2, print "You have selected an invalid menu item." to the screen. Use a loop to continue printing the menu to the screen, reading the user's selection and printing one of those three messages until the user enters 0.