CPTR 124 Fundamentals of Programming


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

  1. 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 strategy to solve each problem. Next, consider how you would implement that strategy in a C++ program.

  2. 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.

  3. Housekeeping

    This assignment has four parts. You should write four small C++ programs, with each program addressing one of the parts. You should create a new Visual Studio project for each program.

    Use the std::cin console input stream 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.

  4. What to do

    1. Midpoint Calculation In mathematics, you can compute the midpoint between the two points (x1,y1) and (x2,y2) with the following formula:

      Midpoint formula

      Write a C++ program that receives two mathematical points from the user and computes and prints their midpoint.

      A sample run of the program produces

      Please enter the first point: (0,0) Please enter the second point: (1,1) The midpoint of (0,0) and (1,1) is (0.5,0.5)

      The user literally enters "(0,0)" and "(1,1)" with the parentheses and commas as shown. To see how to do this, suppose you want a user to enter the point (2.3,9) assigning the x component of the point to a variable named x and the y component to a variable named y. You can add the following code fragment to your program to achieve the desired effect:

      double x, y; char left_paren, comma, right_paren; std::cin >> left_paren >> x >> comma >> y >> right_paren;

      If the user literally types (2.3,9), the statement with std::cin will assign the ( character to the variable named left_paren. It next will assign 2.3 to the variable x. It assigns the comma character to the variable named comma, the value 9 to the y variable, and the ) character to the right_paren variable. The left_paren, right_paren, and comma variables are just placeholders for the user's input and are not used elsewhere within the program. In reality, the user can type in other characters in place of the parentheses and comma as long as the numbers are in the proper location relative to the characters; for example, the user can type *2.3:9#, and the program will interpret the input as the point (2.3,9).

    2. Hours, Minutes, Seconds to Seconds

      Write a C++ program that receives three integer values from the user. These three integer values represent respectively hours, minutes, and seconds. The program should display the total 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 and literals to solve this problem.

    3. Seconds to hours, minutes, seconds

      Write a C++ program that does the reverse of the previous 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 exact time expressed by the number of seconds the user entered. For example, if the user enters

      11570

      your program should display

      3 hrs 12 min 50 sec

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

    4. ExerciseComputer

      The following table lists the Calorie contents of several foods at a popular fast-food restaurant:

      FoodCalories
      Bean burrito357
      Salad w/dressing185
      Milkshake388

      Also, running or walking burns off about 100 Calories per mile. Write a C++ program that requests 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 a person must run or walk to burn off the Calories represented by that quantity of 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.

  5. 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.e.southern.edu.

  6. Log out

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