Computer Science 173
Intermediate Computer Programming

Denison
CS173 Homework 1


Homework 1

Overall homework comments:

Program grading will not be based solely on whether or not the programs "works." It will also be based on the organization, structure, and documentation (in the form of program comments) for the submitted work.

Be sure and document your code well. Include:

  1. Your name in a comment block at the top of every source file.
  2. A high level description of the problem solved by the program.
  3. Descriptive comments for each function.

Please see the C++ Programming Style Guide and follow the conventions described there. Up to 20% of the grade for this homework will be based on following these conventions and practicing good documentation.

Calculation Programs

  1. Write a short program that asks for your height in integer inches and then converts your height to feet and inches. Have the program use the underscore character to indicate where to type the response. Also, use a const symbolic constant to represent the conversion factor. Name your source file in2ft.cpp.

  2. Write a short program that asks for your height in feet and inches and your weight in pounds. (Use three variables to store the information.) Have the program report your BMI (Body Mass Index). To calculate the BMI, first convert your height in feet and inches to your height in inches. Then, convert your height in inches to your height in meters by multiplying by 0.0254. Then, convert your weight in pounds into your mass in kilograms by dividing by 2.2. Finally, compute your BMI by dividing your mass in kilograms by the square of your height in meters. Use symbolic constants to represent the various conversion factors. Name your source file calcBMI.cpp.

  3. Write a program that asks how many miles you have driven and how many gallons of gasoline you used and then reports the miles per gallon your car got. Or, if you prefer, the program can request distance in kilometers and petrol in liters and then report the result European style, in liters per 100 kilometers. (Or, perhaps, you can use liters per 100 kilometers.) Name your source file fuelrate.cpp.

  4. Write a program that asks you to enter an automobile gasoline consumption figure in the European style (liters per 100 kilometers) and converts to the U.S. style of miles per gallon. Note that in addition to using different units of measurement, the U.S approach (distance / fuel) is the inverse of the European approach (fuel / distance). One hundred kilometers is 62.14 miles miles, and one gallon is 3.875 liters. Thus, 19 mpg is about 12.4 l/100-km, and 27 mpg is about 8.7 l/100-km. Name your source file convertfuel.cpp.

  5. You often hear someone make the claim that by using his or her "system" you can double your investment in a few months or years. The "Rule of 72" provides an easy way to compute how long it takes to double your money. The formula for this rule is:

    years to double investment = 72.0 / (interest rate)
    where the interest rate (as a % of 100) is the interest you earn annually on the investment. The rule is based on the assumpution that interest is compounded annually and is not taxed.

    Create a program that asks the user for an annual interest rate, computes the time for an investment to double, and outputs that time to the console in an informative message. Name your source file rule72.cpp.

Loop and Array Programs

    1. You sell a book titled C++ for Java Programmers. Write a program that has you enter a year's worth of monthly sales (in terms of number of books, not of money). The program should use a loop to prompt you by month, using an array of char * initialized to the month strings and storing the input data in an array of int. Then, the program should find the sum of the array contents and report the total sales for the year. Name your source file booksales.cpp.

    2. Daphne invests $100 at 10% simple interest. That is, every year, the investment earns 10% of the original investment, or $10 each and every year:

      interest = 0.10 x original balance

      At the same time, Cleo invests $100 at 5% compound interest. That is, interest is 5% of the current balance, including previous additions of interest:

      interest = 0.05 x current balance

      Cleo earns 5% of $100 the first year, giving her $105. The next year she earns 5% of $105, or $5.25, and so on. Write a program that finds how many years it takes for the value of Cleo's investment to exceed the value of Daphne's investment and then displays the value of both investments at that time. Name your source file invest.cpp.