3.16. Mixed-Up Code Exercises

Answer the following Mixed-Up Code questions to assess what you have learned in this chapter.

Q1

Construct a function that correctly prints the integer conversion of the passed double.

  1. cout << d;
  2. cout d;
  3. d = int(d);
  4. d = integer(d);
  5. print_integer (double d);
  6. void print_integer (double d) {
  7. }

Q2

Construct a function called new_line that takes no arguments and prints a blank line. Then construct another function called divider that prints two blank lines separated by a line of ". . . . . . . . . . . ."

  1. cout << ". . . . . . . . . . . . " << '\n';
  2. cout << '\n';
  3. cout << new_line ();  //first call
  4. cout << new_line ();  //second call
  5. new_line ();  //first call
  6. new_line ();  //second call
  7. void divider () {
  8. void divider (new_line) {
  9. void new_line () {
  10. }  //divider
  11. }  //new_line

Q1

Construct a function that correctly calculates the volume of a cone with as much precision as possible and prints the value to the terminal.

  1. constexpr double pi = 3.1415926;
  2. cout << vol;
  3. double vol = 1/3 * pi * r * r * h;
  4. double vol = 1/3.0 * pi * r * r * h;
  5. double volume_cone (double r, double h) {
  6. int vol = 1/3 * pi * r * r * h;
  7. int vol = 1/3.0 * pi * r * r * h;
  8. void volume_cone (double r, double h) {
  9. }

Q4

Construct a function that prints the sin of an angle given in degrees.

  1. #include &#60;cmath&#62;
  2. #include &#60;iostream&#62;
    using namespace std;
  3. #include &#60;math&#62; #distractor
  4. constexpr double pi = 3.1415926;
  5. cout <<  sin(d);
  6. cout <<  sin(r);
  7. double r = d * (2 * pi) / 360.0;
  8. double r = d * 360.0 / (2 * pi);
  9. void sine_degrees () {
  10. void sine_degrees (double d) {
  11. }

Q5

Construct a function that prints the price (with 8% sales tax) of an item with after using a 30% off coupon.

  1. cout << final;
  2. double discount = item * 0.30;
  3. double final = (item - discount) * 0.08;
  4. double final = (item - discount) * 1.08;
  5. double final = item - discount * 0.08;
  6. void final_price (double item) {
  7. void final_price (string item) {
  8. }

Q6

Suppose you have already defined a function called sum_of_squares which returns the sum of the squares of two numbers and sqrt which returns the square root of a number. Construct a function that calculates the hypotenuse of the right triangle and prints the three sidelengths.

  1. cout << "The sides of the triangle are: " << s1 << ", " << s2 << ", " << hypot;
  2. cout << "The sides of the triangle are: " << s1 << ", " << s2 << ", " << s3;
  3. double hypot = sqrt(s1, s2);
  4. double hypot = sqrt(sum);
  5. double s1 = 4.8;
    double s2 = 3.8;
  6. double sum = sum_of_squares(s2, s1);
  7. int main () {
  8. int s1 = 4.8;
    int s2 = 3.6;
  9. sum = sum_of_squares(s1, s2);
  10. }

Q7

The chickens from the previous chapter are infuriated. Construct a function that prints "Eat" on the first line, "More" on the second line, and the name of the passed animal on the fourth line, followed by an exclamation point.

  1. cout << "Eat" << '\n';
  2. cout << "Eat";
  3. cout << '\n';
  4. cout << '\n'; cout << "More" << '\n';
  5. cout << animal << ! << '\n';
  6. cout << animal << '!' << '\n';
  7. void eat_more () {
  8. void eat_more (string animal) {
  9. }

Q8

Construct a function that takes a dollar amount and cent amount and prints the total amount of money that you have. Hint: the mod operator (%) returns the remainder of a division.

  1. cout << '$' << dollar_total << '.' << cent_total;
  2. cout << '$' << dollar_total << cent_total;
  3. double cent_total = cents % 100;
  4. double cent_total = cents / 100;
  5. double dollar_total = dollars + cents / 100.0;
  6. int dollar_total = dollars + cents / 100;
  7. void print (int dollars, int cents) {
  8. }

Q9

In Michigan, the probability that it snows on any given day in the winter is about 14%. The probability of having a snow day on any given day in the winter is about 4%. The probability that is snows and you have a snow day is 8%. Construct and call a function that calculates the probability of having a snow day, given the fact that it will snow tonight. For reference, the formula for conditional probability is: P(A|B) = P(B and A) / P(B).

  1. conditional_probability(p_snow, p_both);
  2. conditional_probability(p_snowday, p_both);
  3. conditional_probability(p_snowday, p_snow);
  4. cout << prob;
  5. double p_snow = 0.14;
    double p_snowday = 0.04;
    double p_both = 0.08;
  6. double prob = B / both;
  7. double prob = both / B;
  8. int main () {
  9. void conditional_probability (double B, both) {
  10. void conditional_probability (double B, double both) {
  11. } //conditional_probability
  12. } //main

Q10

Your final grade is determined by a midterm component (each midterm is worth 20% of the grade) and a final component. In order to avoid any discrepancies with students who's grades are on the fence, your teacher follows this strict grading scale: [0%,60%) = F, [60%, 70%) = D, [70%, 80%) = C, [80%, 90%) = B and [90%, 100%] = A. They do not truncate until the very end. Construct a function that determines your final grade percentage according to this grading scheme and prints the result.

  1. cout << grade;
  2. cout << int(grade) + 1;
  3. cout << int(grade);
  4. double grade = m_comp + f_comp;
  5. double m_comp = m1 * 0.2 + m2 * 0.2;
    double f_comp = f * 0.06;
  6. int m_comp = m1 * 0.2 + m2 * 0.2;
    int f_comp = f * 0.06;
  7. void final_grade (double m1, double m2, double f) {
  8. void final_grade (double m1, m2, f) {
  9. }