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.
-
cout << d; -
cout d; -
d = int(d); -
d = integer(d); -
print_integer (double d); -
void print_integer (double d) { -
}
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 ". . . . . . . . . . . ."
-
cout << ". . . . . . . . . . . . " << '\n'; -
cout << '\n'; -
cout << new_line (); //first call -
cout << new_line (); //second call -
new_line (); //first call -
new_line (); //second call -
void divider () { -
void divider (new_line) { -
void new_line () { -
} //divider -
} //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.
-
constexpr double pi = 3.1415926; -
cout << vol; -
double vol = 1/3 * pi * r * r * h; -
double vol = 1/3.0 * pi * r * r * h; -
double volume_cone (double r, double h) { -
int vol = 1/3 * pi * r * r * h; -
int vol = 1/3.0 * pi * r * r * h; -
void volume_cone (double r, double h) { -
}
Q4
Construct a function that prints the sin of an angle given in degrees.
-
#include <cmath> -
#include <iostream> using namespace std; -
#include <math> #distractor -
constexpr double pi = 3.1415926; -
cout << sin(d); -
cout << sin(r); -
double r = d * (2 * pi) / 360.0; -
double r = d * 360.0 / (2 * pi); -
void sine_degrees () { -
void sine_degrees (double d) { -
}
Q5
Construct a function that prints the price (with 8% sales tax) of an item with after using a 30% off coupon.
-
cout << final; -
double discount = item * 0.30; -
double final = (item - discount) * 0.08; -
double final = (item - discount) * 1.08; -
double final = item - discount * 0.08; -
void final_price (double item) { -
void final_price (string item) { -
}
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.
-
cout << "The sides of the triangle are: " << s1 << ", " << s2 << ", " << hypot; -
cout << "The sides of the triangle are: " << s1 << ", " << s2 << ", " << s3; -
double hypot = sqrt(s1, s2); -
double hypot = sqrt(sum); -
double s1 = 4.8; double s2 = 3.8; -
double sum = sum_of_squares(s2, s1); -
int main () { -
int s1 = 4.8; int s2 = 3.6; -
sum = sum_of_squares(s1, s2); -
}
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.
-
cout << "Eat" << '\n'; -
cout << "Eat"; -
cout << '\n'; -
cout << '\n'; cout << "More" << '\n'; -
cout << animal << ! << '\n'; -
cout << animal << '!' << '\n'; -
void eat_more () { -
void eat_more (string animal) { -
}
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.
-
cout << '$' << dollar_total << '.' << cent_total; -
cout << '$' << dollar_total << cent_total; -
double cent_total = cents % 100; -
double cent_total = cents / 100; -
double dollar_total = dollars + cents / 100.0; -
int dollar_total = dollars + cents / 100; -
void print (int dollars, int cents) { -
}
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).
-
conditional_probability(p_snow, p_both); -
conditional_probability(p_snowday, p_both); -
conditional_probability(p_snowday, p_snow); -
cout << prob; -
double p_snow = 0.14; double p_snowday = 0.04; double p_both = 0.08; -
double prob = B / both; -
double prob = both / B; -
int main () { -
void conditional_probability (double B, both) { -
void conditional_probability (double B, double both) { -
} //conditional_probability -
} //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.
-
cout << grade; -
cout << int(grade) + 1; -
cout << int(grade); -
double grade = m_comp + f_comp; -
double m_comp = m1 * 0.2 + m2 * 0.2; double f_comp = f * 0.06; -
int m_comp = m1 * 0.2 + m2 * 0.2; int f_comp = f * 0.06; -
void final_grade (double m1, double m2, double f) { -
void final_grade (double m1, m2, f) { -
}