CS 010 - Introduction to Computer Science I
Lab 2.
Fundamental Data Types

Points(10 overall)


Collaboration policy:
You will be working in pairs during labs.  Pairs will be randomly selected and will be announced by the TA.  Each week you will have a new random partner.  You will discuss the programs with your partner, but you will still be typing in your own code to show to the TA.  You can help each other debug, give plenty of suggestions and hints, **explain** why things work or don't work, etc.


Lab Objectives

To gain experience with


Getting Ready

Commenting and Style


Program 1: Arithmetic Operators

You will be writing a program that will calculate the mix of coins given back by a vending machine.  Use cents, not dollars for price amounts (thus if something cost $1.25, you would refer to it as 125 cents).  You should obtain two pieces of information from the user, the amount (in cents) of their purchase, and the amount (in cents) they put in the machine.  Assume that they always put in enough money.  Your program should then output the total change returned and the breakdown of that change (number of quarters, dimes, nickels, and pennies).  Use the most of each largest coin possible.  Hints for the program are below.

 

Here are some examples showing the program output and user input.

Example 1

How much does the item you want to buy cost (in cents)?   37

How much money did you enter (in cents)?   100

 

Total change returned: 63 cents

Quarters: 2

Dimes: 1

Nickels: 0

Pennies: 3

 

Example 2

How much does the item you want to buy cost (in cents)?   115

How much money did you enter (in cents)?   150

 

Total change returned: 35 cents

Quarters: 1

Dimes: 1

Nickels: 0

Pennies: 0

 

Example 3

How much does the item you want to buy cost (in cents)?   250

How much money did you enter (in cents)?   1000

 

Total change returned: 750 cents

Quarters: 30

Dimes: 0

Nickels: 0

Pennies: 0

 

 

Hints:

To find out the maximum number of times a quarter can be taken out of the cents that are going to be returned, you would simply divide the amount to be returned by 25, forgetting the remainder (take advantage of integer division).  Then adjust the amount to be returned and do the same with dimes, and so on.

Writing the Program:

If you are not inside of your lab2 directory, use the change directory command (cd lab2) to descend into that directory.  Now that you are in that directory, make another directory for the above program, maybe call it money.  Then descend into your new directory.  Now you are ready to start writing your program.  Now you can execute the command emacs main.cpp & to start typing your program.

 


Program 2: Arithmetic Operators

Write a program to compute a student's weighted course score.  There are three tests in the class.  The user should enter the maximum points for each test (they do not have to be the same) (integer), the student's score on each test (integer), and the weight of each test (as a percent of 1.0 (0.4 = 40%)) (floating-point value).  Your code should output the student's weighted course grade.

 

Example 1:

Weight of first test: 30

Maximum on first test:  100

Score on first test:  93

 

Weight of second test: 30

Maximum on second test:  100

Score on second test:  87

 

Weight of third test: 40

Maximum on third test:  150

Score on third test:  101

 

Your overall grade is 80.93%

 

Writing the Program:

Move yourself back into the lab2 directory (to go up a directory, use cd .., to go into a directory, use cd directory_name).  If you were to do an ls here you should see your money directory.  Now make another directory for your second program, maybe call it grade.  Go into this directory and begin to write your program using emacs main.cpp &.

 


Bonus Program (1 pt)

One can use their age to calculate their target heart rate zone for aerobic exercise.  Write a program that takes a users age and outputs their target heart rate zone (output both the lower and upper limits as a floating-point value).  The formula to calculate the zone in beats per minute (bpm) is:

            Lower limit (bpm) = 60% of the difference between 220 and your age

            Upper limit (bpm) = 75% of the difference between 220 and your age

 

Example:

If a person’s age was 25, then their target heart rate would be between 117 bpm and 146.25 bpm.