Homework 3 - Chapters 4 and 5

Due Monday, October 23, 2006 at 5pm.

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

  1. This purpose of this problem is to work with function overloading. You will write several versions of functions called find_min(), get_input() and print_min(). Use the following main function:
    int main()
    {
      int num1, num2, num3;
      double dnum1, dnum2;
    
      // Two integer version
      get_input(num1, num2);
      print_min(find_min(num1, num2));
    
      // Two double version
      get_input(dnum1, dnum2);
      print_min(find_min(dnum1, dnum2));
    
      // Three integer version
      get_input(num1, num2, num3);
      print_min(find_min(num1, num2, num3));
    
      return 0;
    }
    
  2. Write a void function that takes two integer arguments: hours (valid values 0-23) and minutes (valid values 0-59), and prints out the time in both 12h and 24h format. The function will first validate the input by checking that the value for each argument is within the valid range and printing an error message if either is not. For 12h format, AM is the hours 0 to 11 with the 0th hour being 12am and PM is the hours 12-23 with the 12th hour being 12pm, the 13th hour being 1pm and so on. You can use a second function to convert from 24h to 12h time if you wish, but it is not required. For both 12h and 24h formats, to fully emulate a clock, any minutes or hour less than 10 needs to be padded with 0 to two digits. For example, 5 minutes after the hour of 9 is written 09:05. Use the padding with 0 option of printf() to do this.

    Examples of converted time:

    Use the following main function:

    int main()
    {
      int hour, minute;
    
      cout << "Enter the hour in 24h format (0-23): ";
      cin >> hour;
      cout << "Enter the minute (0-59): ";
      cin >> minute;
      print_time(hour, minute);
    
      return 0;
    }
    
  3. From the book, programming project 5.13 on page 296:
    The area of an arbitrary triangle can be computed using the formula area = sqrt( s(s - a)(s - b)(s - c) ), where a, b, and c are the lengths of the sides and s is the semiperimeter. s = (a + b + c)/2. Write a void function that uses five parameters: three value parameters that provide the lengths of the edges, and computes the area and perimeter (not the semiperimeter) via reference parameters. Make your function robust. Note that not all combinations of a, b, c produce a triangle. Your function should produce correct results for legal data and reasonable results for illegal combinations.
    Making your function "robust" means your function should validate the input (the lengths of the sides) to make sure they form a triangle. A valid triangle is formed when all the sides are greater than 0 and the sum of the lengths of two of the sides is longer than the third side. Should this precondition fail, print an error message and set the area and semiperimeter equal to 0.

  4. Modify your simple menu program from Homework 2. The menu options will remain the same, but now each option will use a function call instead of having all the option statements within the switch() statement.
      Welcome to the CS221 Homework 3 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:
    
    From within each case statement, there will be a void function that gets the input from the user, does the task and prints the result to the screen. For example, the first menu item case statement would be:
      case 1:
        do_multiply();
        break;
    
    You should have at least 4 functions in your final code (more is fine if you wish to break each menu item task into subtasks):