Homework 1: Data types and Flow control

Due: Friday September 24, 2010 at 5:00pm

Name your files hw1_<number>.cpp, such as hw1_1.cpp for the first program. Email all source files as attachments to my account.

  1. (5 points) Building off of the work in Lab 2, 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 / 10 is 0.5.
    5 / 2 is 2.5.
    2 / 3 is 0.
    
  2. (5 points) 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. (10 points) Expand on Problem 1 to now allow the user to select the math operation they wish to do. You will ask the user for two doubles and then ask the user which math operation they wish to perform. Use an if/else if/else statement to perform the requested math operation. Valid math operations are +, -, * and /. Your if/else if/else statement should follow this design:
    if requested operation is +
      add doubles and print result
    else if requested operation is -
      subtract double2 from double1 and print result
    else if requested operation is *
      multiply doubles and print result
    else if requested operation is /
      divide double2 by double1 and print result
    else
      print error message "Requested operation is not supported. Use +, -, * or /"
    
    The output should resemble the following:
    Enter double 1: 5.5
    Enter double 2: 10.2
    Enter operation (+, -, *, /): +
    
    5.5 + 10.2 is 15.7.