CPTR 124 Fundamentals of Programming


This lab provides experience with variables, input, output, and simple arithmetic in C++.
  1. Forming a Team

    You are encouraged to choose a partner from this class as a teammate. You two will work together on this assignment.

    You are not required to work with someone else; you may work alone if you prefer.

  2. Familiarize yourself with the assignment

    Read over the complete lab before you begin creating your project and C++ source files. Be sure you can work out the solutions with pencil and paper before you start coding. If you cannot do the arithmetic by hand, you have little hope of writing a computer program to do so.

    A good approach is to work out by yourself the mathematical stategy to solve each problem. Next, see how your partner solved each of the problems. Agree on the best strategy for each problem and then work together on implementing that strategy in a C++ program. If you both feel confident in your C++ programming skills, you could work separately coding each program and then get together to build a final version incorporating the best ideas from both.

  3. Ground rules

    You may use any of the techniques described in the textbook in Chapters 1–4. Specifically, you may not use conditional statements in any of these programs. The purpose of this lab is to better understand C++'s mathematical operators. All the requirements of this lab can be met by the clever application of these mathematical operators.

  4. Housekeeping

    This assignment has four parts. You may write one C++ program that does four different things when it runs, or you may write four different programs. You will need to create a new Visual Studio project for each program.

    Use a cin object when user input is required (see the first part of Chapter 4).

    Choose your variable names thoughtfully. The names you choose should reflect their purpose within the program. For example, don't use the name x (a popular variable name in algebra class) when the name seconds would better represent the purpose of a variable participating in a time calculation.

  5. What to do

    Write a C++ program to solve each of the following problems:

    1. Hours, Minutes, Seconds to Seconds

      Receive three integer values from the user. These three integer values represent respectively hours, minutes, and seconds. The program should display the number of seconds represented by this time value. For example, if the user enters
      3 12 50
      

      your program should display
      11570
      

      Use only integer variables to solve this problem.

    2. Seconds to hours, minutes, seconds

      Next, do the reverse of the above problem. Your program should request a single integer value from the user. This integer represents some number of seconds. The program should display the number of hours, minutes, and seconds that correspond to the number of seconds the user enters. For example, if the user enters
      11570 
      

      your program should display
      3 hrs 12 min 50 sec  
      

      Again, use only integer (int) variables to solve this problem. Hint: A good way to solve this problem is to use the division (/) and modulus (%) operators in succession.

    3. Money

      Next, your program should request four integers from the user representing the number of quarters, dimes, nickels, and pennies. The program should display the number of dollars in the normal way currency is printed. For example, if the user enters
      3 12 5 36
      

      the program would display

      $2.56
      

      Use only integer variables and calulations to solve this problem. This means you should not use numeric literals like 0.25 within your calculations.

      Be careful! If the user enters
      0 0 1 0
      

      the program should display

      $0.05
      

      not

      $0.5
      

      Note the zero in the correct result. Hint: Your strategy here can be similar to the Seconds to hours minutes seconds problem. Once you have figured out the total number of cents, determine how that amount can be expressed in hundreds of cents (dollars), tens of cents (remaining dimes), and units of cents (remaining pennies). Print these computed values separately, with the dollar sign ($) and decimal point (.) appropriately placed.

    4. ExerciseComputer

      The following table lists the Calorie contents of several foods:

      FoodCalories
      Bean burrito357
      Salad w/dressing185
      Milkshake388

      Also, running or walking burns off about 100 Calories per mile. The program should request three values from the user: the number of bean burritos, salads, and shakes consumed (in that order). The program should then display the number of miles that must be run or walked to burn off the Calories represented in that food. The program should run as follows (the user types in the 3 2 1):

      How many bean burritos, bowls of salad, and milkshakes did you consume?   3 2 1
      
      You ingested 1829 Calories
      You will have to run 18.29 miles to expend that much energy
      

      Observe that the result is a floating-point value, so you should use floating-point arithmetic to compute the answer for this problem.

  6. Check out

    I will review your lab with you before you leave. Be prepared to answer questions about any parts of your program. After you have been checked out, please submit your C++ source code (the .cpp file) to eclass.

  7. Log out

    Don't forget to log out on your lab workstation and take your USB drive before you leave the lab.