Skip Navigation
 

In Class Exercises

In Class Exercise 2

C++ Operators and Expressions

 

  1. Assume a,b, and c are integers and that a = 0, b = 1 and c = 5. What is the value of each of the following?  Evaluate each expression using the original values for a,b and c.

a.       a + b

b.      a > b

c.       3 + b * c

d.      ++b

e.       b++

f.        b <= c

g.       a > 5

h.       ++a == b

i.         b != c

j.        b == c

k.      b = c

l.         b / c

m.     b % c

n.       b + c * 4 / 3

 

  1. Write a program that allows the user to enter two values. Display the results of adding the two values, subtracting them from each other, multiplying them and dividing them. Save the file as MathExercise.cpp.

 

  1. Write a program for a bank that allows the user to enter an amount of money in cents. Display the number of whole dollars the bank will give the customer in exchange. Save the file as DollarConversion.cpp.

 

  1. Modify the program in 3. so that, in addition to the cents, the user can enters the denomination of the coin to which to convert the cents, such as 5 for 5 cent pieces or 25 for quarters. Save the file as ChangeConversion.cpp.

 

  1.  The Universal Product Code (UPC) on most grocery items, contains a 0 that is printed to the left of the barcode and 10 digits that are printed in two groups of 5,each below the code. The first five digits represent a manufacturer and the second five digits represent a specific product. To the right of the barcode, a check digit is printed. Write a program that allows the user to enter a 5-digit manufacturer code, a 5-digit product code, and the check digit.

 

Display the result of a Boolean comparison (1 or 0) that shows whether the check digit is correct according to the following algorithm. (Hint : Use a combination of modulus, multiplication and subtraction to separate the digits in the entered code.)

a. Sum the second and fourth digits in the manufacturer's code.

b. Sum the first, third and fifth digits in the product code.

c. Sum the results of step a and step b, and multiply the total by 3.

d. Sum the first, third and fifth digits of the manufacturer's code.

e. Sum the second and fourth digits in the product code.

f. Sum the results of steps d and e, and add the result from step c.

g. Take the remainder when the result of step f is divided by 10.

h. Subtract the result of step g from 10. Compare this result to the check digit entered by the user. If the result and the check digit are equal, then the UPC is a valid one.

 

 
 
« go back|print