5.14. Mixed Up Code Practice

Q1

The program below should print out the even numbers between 20 and 40, inclusive, but the code is mixed up and contains extra blocks. Put the necessary blocks in the correct order.

  1. cout << n << '\n';
  2. int main() {
  3. int n = 0; #distractor
  4. int n = 20;
  5. main(int) {
  6. n = n * 2;                 #distractor
  7. n = n + 2;
  8. n++;                 #distractor
  9. while (n < 40) {
  10. while (n <= 40) {
  11. }
  12. }

Q2

The program below should count down from 100 to 0 in decrements of 10 but the code is mixed up and contains extra blocks. Put the necessary blocks in the correct order.

  1. cout << n << '\n';
  2. int main() {
  3. int n = 100;
  4. int n = 10; #distractor
  5. n += 10;                 #distractor
  6. n -= 10;
  7. while (n < 0) { #distractor
  8. while (n > 0) { #distractor
  9. while (n >= 0) {
  10. }
  11. }

Q3

The program below should find the sum of the first 10 natural numbers, but the code is mixed up and contains extra blocks. Put the necessary blocks in the correct order.

  1. cout << n << '\n';  #distractor
  2. int main() {
  3. int n = 10; #distractor
  4. int n = 1;
  5. int sum = 0;
  6. int sum = n;  #distractor
  7. n++;
  8. sum = sum + n;
  9. while (n < 100) {  #distractor
  10. while (n <= 10) {
  11. while (n <= 9) {  #distractor
  12. }
  13. }

Q4

Let's write the code for the repeat_hello function. repeat_hello should be a void function that takes no arguments and uses a while loop to print out "hello" three times.

  1. cout << "hello" << '\n';
  2. int n = 0
  3. int n = 0;
  4. n++;
  5. repeat_hello () {
  6. void repeat_hello () {
  7. while (n < 3) {
  8. while (n > 3) {
  9. }
  10. }

Q5

Now let's generalize the repeat_hello function so that it repeats a given string three times. Let's write the code for the repeat_string function, which takes input as a parameter and uses a while loop to print out the string three times.

  1. cout << input << '\n';
  2. cout << string << '\n';
  3. int n = 0;
  4. n++;
  5. void repeat_string () {
  6. void repeat_string (string input) {
  7. while (3 > n) {
  8. while (n < 3) {
  9. }
  10. }

Q6

We can further generalize repeat_string so that it repeats a given string a given number of times. Let's write the code for the new repeat_string function, which takes input and x as parameters and uses a while loop to print out the string x number of times.

  1. cout << input << '\n';
  2. int n = 0;
  3. int n = x;
  4. n++;
  5. void repeat_string (string input, int x) {
  6. void repeat_string (string input, string x) {
  7. while (n < x) {
  8. while (x < n) {
  9. x++;
  10. }
  11. }

Q7

On the last day of every year, we count down the seconds before the new year arrives. Write the function new_year_countdown, which prints out a countdown from 10 and then prints out "Happy New Year!".

  1.    cout << "Happy New Year!" << '\n';
    }
  2. cout << n << ' ';
  3. int n = 0;
  4. int n = 10;
  5. n++;  #
  6. n--;
  7. void new_year_countdown () {
  8. void new_year_countdown (string input) {
  9. while (n != 10) {
  10. while (n > 0) {
  11. }

Q8

Help Goku reach power levels of over 9000! Write the function power_up which takes power_level as a parameter. power_up checks to see if power_level is over 9000. If it isn't, it repeatedly prints "More power!" and increments power_level by 1000 until power_level is over 9000. Then power_up prints "It's over 9000!". Put the necessary blocks in the correct order.

  1.    cout << "It's over 9000!" << '\n';
    }
  2. cout << "More power!" << '\n';
  3. if (power_level < 9000) {  #distractor
  4. int n = 0;  #distractor
  5. n++;  #distractor
  6. power_level = power_level + 1000;
  7. power_level++;
  8. void power_up () {
  9. void power_up (int power_level) {
  10. while (power_level < 9000) {
  11. while (power_level > 9000) {
  12. }

Q9

Write the function summation which takes two parameters, start and end. summation adds all the integers from start to end, inclusive, together and returns the sum. Put the necessary blocks in the correct order.

  1. int n = start;
  2. int sum = 0;
  3. int sum = start;  #distractor
  4. int summation () {  #distractor
  5. int summation (int start, int end) {
  6. n++;
  7. return n;  #distractor
  8. return sum;
  9. sum = sum + n;
  10. void summation (int start, int end) {  #distractor
  11. while (n < end) {
  12. while (n <= end) {
  13. }
  14. }

Q10

Write the function reverse_number which takes num as a parameter and returns num but with its digits reversed. For example, reverse_number (1324) returns 4231. Put the necessary blocks in the correct order, with reverse declared first, then temp, and lastly remainder.

  1. int remainder = 0;
  2. int remainder;  #distractor
  3. int reverse = 0  #distractor
  4. int reverse = 0;
  5. int reverse_number (int num) {
  6. int temp = num;
  7. remainder = temp % 10;
  8. return reverse;
  9. return temp;  #distractor
  10. reverse = reverse * 10 + remainder;
  11. temp = temp / 10;
  12. void reverse_number (int num) {  #distractor
  13. while (temp > 0) {
  14. }
  15. }