CS 16, Winter 2018

Programming Assignment 4

Due: Friday, February 23, 11:59pm
Worth: 100 points

Note due date extended: was Tuesday, now Friday.

PA4 can be done either in two-people teams (using pair programming) or individually. If you are working with a partner, be sure that both partners' names are in a comment at the top of the source code file, and be sure to properly form a group for this project in the submit.cs system.

  1. Write a C++ program in a file named lengths.cpp to solve a variation (as specified below) of the problem described in Chapter 5, Practice Program 4:
    "Write a program that ... asks the user if he or she wants to convert from feet and inches to meters and centimeters or from meters and centimeters to feet and inches. The program then performs the desired conversion. ... Include a loop that lets the user repeat this computation for new input values until the user says he or she wants to end the program."

    The text includes some guidance where the ellipses (...) are in the quote above, including references to the two previous practice problems. Find additional guidance in Lab05 - it requires you to solve one of those problems, and the optional extra challenges encourage you to solve the other one.

    Review the lessons of Lab05 too, particularly how procedural abstraction makes your job simpler in the long run. Then realize that using many small functions is probably the best way to solve this problem. Our solution included more than 10 functions plus main, but each of our functions is quite simple, because each one has just a single job to do. Even our main function has less than 10 statements.

    Here is a typical session (user input is bold):

    -bash-4.3$ ./lengths
    enter 1 for feet,inches to meters,centimeters, or
    enter 2 for meters,centimeters to feet,inches
    1
    enter feet and inches
    10 3
    conversion: 3 meters and 12 centimeters
    convert more?
    y
    enter 1 for feet,inches to meters,centimeters, or
    enter 2 for meters,centimeters to feet,inches
    2
    enter meters and centimeters
    1 42
    conversion: 4 feet and 7 inches
    convert more?
    no
    -bash-4.3$

    If the user enters any number except 1 or 2 in response to the request to enter 1 or 2, the program prints "enter 1 or 2 only" and then waits for a correct response. Here is an example of that type of dialog:

    enter 1 for feet,inches to meters,centimeters, or
    enter 2 for meters,centimeters to feet,inches
    3
    enter 1 or 2 only
    0
    enter 1 or 2 only
    1
    enter feet and inches

    Only the first character of the user's input matters in response to the "convert more?" question. If the first character entered is 'Y' or 'y' then the program continues. Any other response is interpreted as no, and the program finishes executing. Be sure to read about character I/O in section 6.3 of the textbook. And here is a sample session to demonstrate proper program behavior:

    convert more?
    You bet!
    enter 1 for feet,inches to meters,centimeters, or
    enter 2 for meters,centimeters to feet,inches
    1
    enter feet and inches
    100 0
    conversion: 30 meters and 48 centimeters
    convert more?
    maybe
    -bash-4.3$

  2. Compile and test your program at CSIL. Please don't just let the submit program test it for you. As usual, your results must match our results exactly for identical input data (but expect other data to be used in the tests), including all strings printed to cout.

    Remember that you can and should try to test at least the most important functions individually. Such "unit testing" requires drivers and/or stubs like the textbook discusses in section 5.4. Once you know that each part works correctly, you can more confidently put the pieces together to solve the whole problem.

  3. Submit PA4 at https://submit.cs.ucsb.edu/, or use the following command from a CS terminal:
    ~submit/submit -p 956 lengths.cpp
    Be sure to wait for the test results. If you score 100/100 and you've followed all of the other rules, then you'll earn full credit.

Updated 2/20/2018, by C. Michael Costanzo