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 strategy 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 six parts. You should write six small C++ programs, with each program addressing one of the parts. 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

    1. Simple Arithmetic with Integers

      Write a C++ program that receives two integer values from the user. The program then should print the sum (addition), difference (subtraction), product (multiplication), quotient (division), and remainder after division (modulus). Your program must use only int variables and int literals.

      A sample program run would look like (the user enters the 10 and the 2 after the colons, and the program prints the rest):

      Please enter the first number: 10
      Please enter the second number: 2
      10 + 2 = 12
      10 - 2 = 8
      10 * 2 = 20
      10 / 2 = 5
      10 % 2 = 0
      

      Can you explain the results it produces for all of these operations?

    2. Simple Arithmetic with Floating-point Numbers

      Write a C++ program that receives two double-precision floating-point values from the user. The program then should print the sum (addition), difference (subtraction), product (multiplication), and quotient (division). Your program should use only double variables and double literals.

      A sample program run would look like (the user enters the 10 and the 2.5 after the colons, and the program prints the rest):

      Please enter the first number: 10
      Please enter the second number: 2.5
      10 + 2.5 = 12.5
      10 - 2.5 = 7.5
      10 * 2.5 = 25
      10 / 2.5 = 4
      

      Can you explain the results it produces for all these operations? What happens if you attempt to compute the remainder after division (modulus) with double-precision floating-point values?

    3. Midpoint Calculation In mathematics, the midpoint between the two points (x1,y1) and (x2,y2) is computed by the 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;
      cin >> left_paren >> x >> comma >> y >> right_paren;
      

      If the user literally types (2.3,9), the cin statement 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 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).

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

    5. 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 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 and literals to solve this problem. Hint: A good way to solve this problem is to use the division (/) and modulus (%) operators in succession.

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

  7. Log out

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