4.18. Mixed Up Code Practice

Q1

Vacation time! But before you go, you need to convert your currency. Let's write the code for the dollar_to_yen function. dollar_to_yen takes dollar as a parameter and returns the equivalent amount of Japanese yen. The conversion rate is 1 USD equals 105.42 Japanese yen. Put the necessary blocks of code in the correct order.

  1. double dollar_to_yen () {  #distractor
  2. double dollar_to_yen (double dollar) {
  3. double yen;  #distractor
  4. int dollar_to_yen (double dollar) {  #distractor
  5. return 105.42 * dollar;
  6. return 105.42 * yen;  #distractor
  7. void dollar_to_yen (double dollar) {  #distractor
  8. }

Q2

When you buy something, you also need to pay sales tax. For example, a nice shirt could be labeled with a price of exactly $20, but when you pay, you actually need to pay $21.20 in a state with 6% sales tax. However, different states have different tax rates. Write the function price_with_tax, which takes price and percent_tax as parameters. price_with_tax calculates the price after tax and returns it. For example, price_with_tax (20, 6) returns 21.2. Put the necessary blocks of code in the correct order.

  1. double price_with_tax (double price, double percent_tax) {
  2. double price_with_tax (price, percent_tax) {  #distractor
  3. int price_with_tax (double price, int percent_tax) {  #distractor
  4. return (1 + percent_tax / 100) * price;
  5. return (1 + percent_tax) * price;  #distractor
  6. return percent_tax * price;  #distractor
  7. }

Q3

Most assignments and tests are graded as a percentage, but final grades are letters. Let's write the code for the percent_to_letter function. percent_to_letter takes a percentage and returns the corresponding letter grade. A 90 and above is an 'A', an 80 and above is a 'B', a 70 and above is a 'C', and anything under a 70 is an 'F'. Put the necessary blocks of code in the correct order.

  1. char percent_to_letter (double percentage) {
  2. char percent_to_letter (percentage) {  #distractor
  3. else if (percentage > 70) {
  4. else if (percentage >= 70) {
  5. else if (percentage >= 80) {
  6. else {
  7. if (percentage >= 90) {
  8. return 'A';
  9. return 'B'
  10. return 'B';
  11. return 'C';
  12. return 'F';
  13. return A;
  14. void percent_to_letter (double percentage) {  #distractor
  15. void percent_to_letter (int percentage) {  #distractor
  16. }
  17. }
  18. }
  19. }
  20. }

Q4

Let's write the code for the triangle_area function. triangle_area takes two parameters, base and height. It returns the area of the triangle using the formula 1/2 * base * height. Put the necessary blocks of code in the correct order.

  1. cout << 0.5 * base * height << '\n';  #distractor
  2. double area;  #distractor
  3. double triangle_area (base, height) {  #distractor
  4. double triangle_area (double base, double height) {
  5. int triangle_area (double base, double height) {  #distractor
  6. return 0.5 * base * height;
  7. void triangle_area (double base, double height) {  #distractor
  8. }

Q5

Let's write the code for the cylinder_volume function. cylinder_volume takes two parameters, radius and height. It returns the volume of the cylinder using the formula pi * radius * radius * height. Put the necessary blocks of code in the correct order.

  1. double cylinder_volume (double radius, double height) {
  2. double cylinder_volume (radius, height) {  #distractor
  3. double pi = 3.14;
  4. return pi * radius * radius * height;
  5. void cylinder_volume (double radius, double height) {  #distractor
  6. }

Q6

On a distant planet, depending on the characteristics of an egg, a kenchic, an ooseg, or a guinpen might hatch from it. Let's write the function bird_type which returns an int corresponding to each type of bird (1 for kenchic, 2 for ooseg, and 3 for guinpen). If the egg is round, then it is a guinpen. Otherwise, if the egg is round and it isn't gray, then it is a kenchic. If it isn't a guinpen and it isn't a kenchic, then it's an ooseg. Put the necessary blocks of code in the correct order.

  1. double bird_type (int is_round, char is_gray) {  #distractor
  2. else if (!(is_round || is_gray)) {
  3. else if (!is_round || is_gray) {
  4. else {
  5. if (!is_round && !is_gray) {
  6. if (is_round && !is_gray) {
  7. int bird_type (bool is_round, bool is_gray) {
  8. return 0;  #distractor
  9. return 1;
  10. return 2;
  11. return 3;
  12. void bird_type (bool is_round, bool is_gray) {  #distractor
  13. }
  14. }
  15. }
  16. }

Q7

Let's write the code for the is_double_digit function. is_double_digit takes num as a parameter. is_double_digit returns true if num is a double digit number and returns false otherwise. Put the necessary blocks of code in the correct order.

  1. bool is_double_digit (int num) {
  2. else {
  3. if (10 <= num <= 99) {  #distractor
  4. if (num > 10 && num < 100) {  #distractor
  5. if (num > 10 && num <= 100) {  #distractor
  6. if (num >= 10 && num < 100) {
  7. is_double_digit (int num) {
  8. return false;
  9. return true;
  10. }
  11. }
  12. }

Q8

Let's write the code for the compare function. compare takes two integers a, b. compare returns 1 if a is greater than b, -1 if a is less than b and 0 if they are equal. Put the necessary blocks of code in the correct order.

  1. bool compare (int a, int b) {
  2. else if (a &lt b) {
  3. else if (a > 0){ #distractor
  4. else if (a!=b) {  #distractor
  5. else {
  6. if (a > b && a &lt b) {  #distractor
  7. if (a > b) {
  8. int compare (int a, int b) {
  9. return -1;
  10. return 0;
  11. return 1;
  12. }
  13. }
  14. }
  15. }

Q9

Let's write the code for the is_factor function. is_factor takes two parameters, num and factor. is_factor returns true if factor is a factor of num and returns false otherwise. Put the necessary blocks of code in the correct order.

  1. bool is_factor (int num, int factor) {
  2. else {
  3. if (factor % num == 0) {  #distractor
  4. if (num % factor == 0) {
  5. if (num % factor) {  #distractor
  6. if (num / factor == 0) {  #distractor
  7. return false;
  8. return true;
  9. void is_factor (int num, int factor) {
  10. }
  11. }
  12. }

Q10

Let's write the code for the is_perfect_square function. is_perfect_square takes input as a parameter and returns true if input is a perfect square and returns false otherwise. Put the necessary blocks of code in the correct order.

  1. bool is_perfect_square (int input) #distractor
  2. bool is_perfect_square (int input) {
  3. double root = sqrt (input);  #distractor
  4. else {
  5. if (pow (root, 2) == input) {
  6. if (sqrt (input)) {  #distractor
  7. int is_perfect_square (int input) {  #distractor
  8. int root = sqrt (input);
  9. return false;
  10. return true;
  11. }
  12. }
  13. }

Q11

Most bacteria cultures grow exponentially. For this problem, assume the number of cells in a bacterial culture doubles every hour. Let's write the code for the count_bacteria function. count_bacteria takes hour as a parameter and returns the number of bacteria cells after hour hours. Assume when hour is 0, there is one cell. When hour is one, the number of cells doubles to two. When hour is two, the number of cells doubles to four. Use recursion. Put the necessary blocks of code in the correct order.

  1. bool count_bacteria (int hour) {  #distractor
  2. else {
  3. if (hour == 0) {
  4. if (hour == 1) {
  5. int count_bacteria (int hour) {
  6. return 1;
  7. return 2 * count_bacteria (hour - 1);
  8. return 2 * count_bacteria (hour);  #distractor
  9. return 2 * hour;  #distractor
  10. return 2 + count_bacteria (hour - 1);  #distractor
  11. void count_bacteria (int hour) {  #distractor
  12. }
  13. }
  14. }