Wyo C++ Ch. 5 Worksheet #2 Name -

1. Explain the difference between the following statements where the initial value of the variable age is 22? Continue answer on back if necessary.

a/ cout << "Since it is your birthday, you are now " << ++age << " years old." << endl;
b/ cout << "Since it is your birthday, you are now " << (age + 1) << " years old." << endl;

 

 

2. What are the values of the following expressions after the following statements have executed. If a value is a floating-point value, show a decimal point (e.g. instead of 4 write the answer 4.)

int apples = 10;
int bananas;
double cost = 8.0;
double priceApples = 0.5;

  a/ (apples + 6) / 2
  b/ (apples + 6) / 2.0
  c/ cost - apples * priceApples
  d/ cost - apples * int (priceApples)
  e/ apples % 3 * 2
  f/ 10.0 + apples * priceApples
  g/ (apples + bananas) * priceApples

3. Which C++ concept is being illustrated: promotion (P) or typecasting (T)?

  a/ wholeNumPart = int (totalCost);
  b/ double cost = 0.0;
int num = 3;

cost = num;
  c/ cout << "Just give me " << int (amountOwed) << " dollars." << endl;
  d/ int numApples = 0;
double cost = 0.0;

cost = numApples * 0.5;
  e/

double cost = 0.0;
int apples = 4;
double costPerApple = 0.0;

costPerApple = cost / double (apples);

4. On the back of this worksheet paper, write a full, working C++ program that declares integer (int) variables named wins, losses, ties, and totalPoints. Initialize each one to the value of 0 using initialization statements. Assign the value of wins plus ties to the variable totalPoints within the program. Make sure that the program prints an additional output message along with the value of totalPoints.