6.8. Mixed-Up Code Exercises¶
Answer the following Mixed-Up Code questions to assess what you have learned in this chapter.
Q2
Construct a function that prints whether a number is true.
-
cout << false; } -
cout << true; } -
bool is_true (int number) { -
else { -
if (number % 2 == 0) { -
void is_true (int number) { -
}
Q3
Construct a function that prints the difference of a and b if the result would result in a positive number. Otherwise, prints -1.
-
cout << -1; } -
cout << a - b; } -
else { -
if (a - b < 0) { -
if (a - b > 0) { -
int difference (int a, int b) { -
void difference (int a, int b) { -
}
Q4
Construct a block of code that prints "automatic" if x is an odd number, "systematic" if x is greater than y, AND "hydromatic" if y is not equal to x. Check all 3 conditions.
-
cout << "automatic"; } -
cout << "hydromatic"; } -
cout << "systematic"; } -
else if (x > y) { -
else { -
if (x % 2 == 0) { -
if (x % 2 == 1) { -
if (x > y) { -
if (y != x) {
Q5
Construct a block of code that prints "Pick me!" if x is equal to y, "Choose me!" if x is less than y, OR "Love me!" if x + y is even.
-
cout << "Choose me!"; } -
cout << "Love me!"; } -
cout << "Pick me!"; } -
else (x + y % 2 == 0) { -
else if ((x + y) % 2 == 0) { -
else if (x + y % 2 == 0) { -
else if (y > x) { -
if (x < y) { -
if (x = y) { -
if (x == y) {
Q6
Construct a function that prints your letter grade according to this scheme. [0, 70) = F, [70, 80) = C, [80, 90) = B, and [90, 100] = A.
-
cout << 'A'; } -
cout << 'B'; } -
cout << 'C'; } -
cout << 'F'; } -
else if (grade < 100) { -
else if (grade < 80) { -
else if (grade < 90) { -
else { -
if (grade < 100) { -
if (grade < 70) { -
if (grade < 80) { -
if (grade < 90) { -
void print_letter_grade (double grade) { -
}
Q7
According to a logic game, a knight is someone who cannot tell a lie, and a knave is someone who cannot tell the truth. Construct a function that takes two booleans: the truth value of the story, and the truth value told by the person. The function should print whether the person was a knight or a knave.
-
else { -
else { cout << "Knave"; } } -
else { cout << "Knive"; } } -
if (told == true) { cout << "Knave"; } -
if (told == true) { cout << "Knight"; } -
if (truth == true) { -
void knight_knave (bool truth, bool told) { -
}
Q8
If a cat is in a good mood, it purrs; when it's in a bad mood, it meows. If a doog is in a good mood, it barks; when it's in a bad mood it woofs. Construct a function that accomplishes this.
-
else { -
else { cout << "Meow!"; } -
else { cout << "Purr!"; } -
if (animal == "dog") { cout << "Bark!"; } -
if (animal == "dog") { cout << "Woof!"; } -
if (mood == "bad") { -
void make_vocals (string animal, string mood) { -
}
Q9
Construct a recursive function that tells the user to enter a positive number. It should then output that number to the terminal. If the user enters a negative number or zero, prompt the user again.
-
cout << "Input a positive number!"; -
cout << num; -
if (num < 0) { -
int num; cin >> num; -
takesum (); -
void take_sum () { -
} // END "if" -
} // END function
Q10
In the table of ASCII characters, the lowercase alphabet consists of characters 97-122. The uppercase alphabet consists of characters 65-90, which is a 32 character shift back from the lowercase. Construct a recursive function that asks the user to input a LOWERCASE character, converts that character to UPPERCASE character and prints it. If the user enters a character outside of the range of the LOWERCASE alphabet, prompt the user again. Hint: "||" means "or" when used between two conditional statements.
-
capitalize (); } -
char let; cin >> let; -
cout << "Input a lowercase character!"; -
cout << char(let); -
if (int(let) < 97 || int(let) > 122) { -
let = let - 32; -
void capitalize () { -
}