13.13. Mixed-Up Code Exercises

Arrange the following Mixed-Up Code exercises to assess what you have learned in this chapter.

13.13.1. Parsons exercises

How long is a year on other planets? Let's write a program that prints out the number of days in a year on each planet using a switch statement. These values are, in planetary order, 88 days, 225 days, 365 days, 687 days, 4333 days, 10759 days, 30687 days, and 60190 days. Print out this information in the following format: planet planet has num_days number of days in a year! Use the lines to construct the code, then complete the code in the correct order.

  1. case 1:
        std::cout << "planet Mercury has 88 number of days in a year!" << '\n';
        break;
  2. case 2:
        std::cout << "planet Venus has 225 number of days in a year!" << '\n';
        break;
  3. case 3:
        std::cout << "planet Earth has 365 number of days in a year!" << '\n';
        break;
  4. case 4:
        std::cout << "planet Mars has 687 number of days in a year!" << '\n';
        break;
  5. case 5:
        std::cout << "planet Jupiter has 4333 number of days in a year!" << '\n';
        break;
  6. case 6:
        std::cout << "planet Saturn has 10759 number of days in a year!" << '\n';
        break;
  7. case 7:
        std::cout << "planet Uranus has 30687 number of days in a year!" << '\n';
        break;
  8. case 8:
        std::cout << "planet Neptune has 60190 number of days in a year!" << '\n';
        break;
  9. enum planet { mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune };
  10. int main() {
  11. planet p = venus;
  12. switch (p) {
  13. }
  14. }

Now let's generate a bingo_board! We want to fill the 25 spaces on the bingo_board with random values from 1 to 75 without repititon. To do this, we'll make a vector of numbers from 1 to 75 and shuffle it using the same method as shown in this chapter. Then we will select the first 25 values for the 25 spaces on the bingo_board. We will do this entire process in multiple steps. First, write the function random_int, which generates a random value between low and high, inclusive. Be sure to include the relevant libraries! Use the lines to construct the code, then complete the code in the correct order.

  1. return std::uniform_int_distribution<std::size_t>{low, high}(engine);
  2. static std::mt19937 engine(std::random_device{}());
  3. std::size_t random_int(std::size_t low, std::size_t high) {
  4. }

Now that we have the functions random_int and swap_values, we can write the function generate_rand_vec. generate_rand_vec creates a vector with values from 1 to 75, shuffles it using random_int and swap_values, and returns the shuffled vector. Use the lines to construct the code, then complete the code in the correct order.

  1. for (std::size_t i = 0; i < vec.size(); ++i) {
  2. iota(vec.begin(), vec.end(), 1);
  3. return vec;
  4. std::size_t x = random_int(i, vec.size() - 1);
  5. std::vector<int> generate_rand_vec() {
  6. std::vector<int> vec(75);
  7. swap_values(vec, i, x);
  8. }
  9. }

Let's print out our bingo_board! Write the bingo_board member function print_board. Insert tabs between each value in each row to make the board print out neater. Use the lines to construct the code, then complete the code in the correct order.

  1. for (std::size_t i = 0; i < board[j].size(); i++) {
  2. for (std::size_t j = 0; j < board.size(); j++) {
  3. std::cout << '\n';
  4. std::cout << board[j][i].value << '\t';
  5. void bingo_board::print_board () {
  6. }
  7. }
  8. }

You may have noticed that in some cases, our version of bubble_sort does an unnecessary amount of work. For example, if our vector was {1, 2, 3, 5, 4}, bubble_sort would swap 4 and 5, but then keep going even though our vector is already in order! We can save some work by including a bool called is_changed. If we swap values during a pass, we set is_changed to true. If nothing has been swapped, then is_changed stays false, and we know to break out of the loop since our vector is already sorted. Write the function fast_bubble_sort, which is bubble_sort with this modification. Use the lines to construct the code, then complete the code in the correct order.

  1. bool is_changed = false;
  2. break;
  3. for (std::size_t i = 0; i + 1 < vec.size(); ++i) {
  4. for (std::size_t j = 0; j + 1 < vec.size() - i; ++j) {
  5. if (is_changed == false) {
  6. if (vec[j] > vec[j + 1]) {
  7. is_changed = true;
  8. swap_values(vec, j, j + 1);
  9. void fast_bubble_sort(std::vector<int> &vec) {
  10. }
  11. }
  12. }
  13. }
  14. }

Below is the enumerated type Days which maps days of the week to integers starting at 1. Use a switch statement to determine whether or not day is a weekend or not. Check for cases in numerical order.

  1. break;
  2. break;
  3. break;
  4. case 0:
  5. case 1:
  6. day day = sun;
  7. default:
  8. enum day { mon = 1, tue, wed, thu, fri, sat, sun };
  9. int main () {
  10. std::cout << "Invalid input." << '\n';
  11. std::cout << "It is not the weekend :(" << '\n';
  12. std::cout << "It is the weekend :)" << '\n';
  13. switch (day > 5) {
  14. }
  15. }

Use a switch statement to check and print out whether a number is divisible by two. Prompt and get input from the user. If input isn't valid, print out the default statement "Invalid input." Check for cases in numerical order.

  1. break;
  2. break;
  3. break;
  4. case 0:
  5. case 1:
  6. default:
  7. int input;
  8. int main () {
  9. std::cin >> input;
  10. std::cout << "Invalid input." << '\n';
  11. std::cout << "Please enter an integer: ";
  12. std::cout << input << " is even!" << '\n';
  13. std::cout << input << " is odd!" << '\n';
  14. switch (input % 2) {
  15. }
  16. }

Use a switch statement to check and print out the maximum between two numbers. Prompt and get input from the user for two integers. If input isn't valid, print out the default statement "Invalid input." Check for cases in numerical order.

  1. break;
  2. break;
  3. case 0:
  4. case 1:
  5. default:
  6. int input1;
  7. int input2;
  8. int main () {
  9. std::cin >> input1;
  10. std::cin >> input2;
  11. std::cout << "Invalid input." << '\n';
  12. std::cout << "Please enter first integer: ";
  13. std::cout << "Please enter second integer: ";
  14. std::cout << "The maximum is " << input1 << '\n';
  15. std::cout << "The maximum is " << input2 << '\n';
  16. switch (input1 > input2) {
  17. }
  18. }

Below is the pseudocode for the implementation of merge_sort. Put the blocks in the correct order!

  1. card_deck card_deck::merge_sort () const {
  2. card_deck::merge_sort () const {  #distractor
  3. divide each subdeck into two more subdecks
  4. divide the deck into two subdecks
  5. find the midpoint of the deck
  6. merge the two halves and return the result
  7. sort the subdecks using sort
  8. use a for loop to traverse half the deck  #distractor
  9. }

Let's revisit the dictionary data structure defined in the previous section. Write the struct definitions for entry, which has member variables word and page, and for dictionary, which has a vector of Entries. Put the necessary blocks of code in the correct order.

  1. entry entries;  #distractor
  2. entry word;  #distractor
  3. int page;
  4. std::string word;
  5. std::vector<entry> entries;
  6. std::vector<Word> entries;  #distractor
  7. struct dictionary {
  8. struct entry {
  9. };
  10. };

Assume our dictionary is currently unsorted. Let's write a dictionary member function find that takes a string word as a parameter and returns the index of its corresponding entry. If the word isn't in the dictionary, return -1. Put the necessary blocks of code in the correct order.

  1. for (std::size_t i = 0; i < entries.size(); ++i) {
  2. for (std::size_t i = 1; i < dictionary.entries.size(); ++i) {  #distractor
  3. for (std::size_t i = 1; i < entries.size(); ++i) {  #distractor
  4. if (entries[i].word == word) {
  5. if (i.word == word) {  #distractor
  6. return -1;
  7. return static_cast<std::ptrdiff_t>(i);
  8. std::ptrdiff_t dictionary::find (entry word) {
  9. std::ptrdiff_t dictionary::find (std::string word) {
  10. }
  11. }
  12. }

Of course, all dictionaries are in some sort of order. In order to do this, we must first write the dictionary member function find_first_word, which takes a starting index as a parameter returns the index of the entry with the highest priority alphabetically (i.e. the entry with a word that would come first in the alphabet). Put the necessary blocks of code in the correct order.

  1. for (std::size_t i = 0; i < entries.size(); ++i) {  #distractor
  2. for (std::size_t i = start; i < entries.size(); ++i) {
  3. if (entries[i].word < entries[min].word) {
  4. if (entries[i].word > entries[min].word) {  #distractor
  5. min = i;
  6. return min;
  7. std::size_t dictionary::find_first_word (std::size_t start) {
  8. std::size_t dictionary::find_first_word (std::string word) {
  9. std::size_t min = start;
  10. }
  11. }
  12. }

We also need a swap function. Write the dictionary member function swap which takes two indices as parameters and swaps the Entries at those indices. Put the necessary blocks of code in the correct order.

  1. entries[a] = entries[b];
  2. entries[b] = temp;
  3. entry temp = entries[a];
  4. void dictionary::swap () {
  5. void dictionary::swap (std::size_t a, std::size_t b) {
  6. }

Now let's write the dictionary member function alphabetize, which sorts the Entries in the dictionary in alphabetical order. Use the find_first_word and swap functions we defined earlier! Put the necessary blocks of code in the correct order.

  1. for (std::size_t i = 0; i < entries.size() - 1; ++i) {  #distractor
  2. for (std::size_t i = 0; i < entries.size(); ++i) {
  3. int dictionary::alphabetize () {
  4. std::size_t min = find_first_word (0);  #distractor
  5. std::size_t min = find_first_word (i);
  6. std::swap (0, min);  #distractor
  7. std::swap (entries[i], entries[min]);
  8. void dictionary::alphabetize () {
  9. }
  10. }

Let's check to see if our sorting worked! Write the dictionary member function print_dictionary, which prints out the word in each entry. Put the necessary blocks of code in the correct order.

  1. for (std::size_t i = 0; i < entries.size(); ++i) {
  2. std::cout << entries[i].entry << '\n';  #distractor
  3. std::cout << entries[i].word << '\n';
  4. std::cout << entry.word << '\n';  #distractor
  5. void dictionary::print_dictionary () {
  6. }
  7. }