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.

  1.  cout << false;
    }
  2.  cout << true;
    }
  3. bool is_true (int number) {
  4. else {
  5. if (number % 2 == 0) {
  6. void is_true (int number) {
  7. }

Q3

Construct a function that prints the difference of a and b if the result would result in a positive number. Otherwise, prints -1.

  1.  cout << -1;
    }
  2.  cout << a - b;
    }
  3. else {
  4. if (a - b < 0) {
  5. if (a - b > 0) {
  6. int difference (int a, int b) {
  7. void difference (int a, int b) {
  8. }

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.

  1. cout << "automatic"; }
  2. cout << "hydromatic"; }
  3. cout << "systematic"; }
  4. else if (x > y) {
  5. else {
  6. if (x % 2 == 0) {
  7. if (x % 2 == 1) {
  8. if (x > y) {
  9. 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.

  1. cout << "Choose me!"; }
  2. cout << "Love me!"; }
  3. cout << "Pick me!"; }
  4. else (x + y % 2 == 0) {
  5. else if ((x + y) % 2 == 0) {
  6. else if (x + y % 2 == 0) {
  7. else if (y > x) {
  8. if (x < y) {
  9. if (x = y) {
  10. 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.

  1. cout << 'A'; }
  2. cout << 'B'; }
  3. cout << 'C'; }
  4. cout << 'F'; }
  5. else if (grade < 100) {
  6. else if (grade < 80) {
  7. else if (grade < 90) {
  8. else {
  9. if (grade < 100) {
  10. if (grade < 70) {
  11. if (grade < 80) {
  12. if (grade < 90) {
  13. void print_letter_grade (double grade) {
  14. }

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.

  1. else {
  2. else {
     cout << "Knave";
    } }
  3. else {
     cout << "Knive";
    } }
  4. if (told == true) {
     cout << "Knave";
    }
  5. if (told == true) {
     cout << "Knight";
    }
  6. if (truth == true) {
  7. void knight_knave (bool truth, bool told) {
  8. }

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.

  1. else {
  2. else {
     cout << "Meow!";
    }
  3. else {
     cout << "Purr!";
    }
  4. if (animal == "dog") {
     cout << "Bark!";
    }
  5. if (animal == "dog") {
     cout << "Woof!";
    }
  6. if (mood == "bad") {
  7. void make_vocals (string animal, string mood) {
  8. }

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.

  1. cout << "Input a positive number!";
  2. cout << num;
  3. if (num < 0) {
  4. int num;
    cin >> num;
  5. takesum ();
  6. void take_sum () {
  7. } // END "if"
  8. } // 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.

  1. capitalize (); }
  2. char let;
    cin >> let;
  3. cout << "Input a lowercase character!";
  4. cout << char(let);
  5. if (int(let) < 97 || int(let) > 122) {
  6. let = let - 32;
  7. void capitalize () {
  8. }