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
planethasnum_daysnumber of days in a year! Use the lines to construct the code, then complete the code in the correct order.
-
case 1: std::cout << "planet Mercury has 88 number of days in a year!" << '\n'; break; -
case 2: std::cout << "planet Venus has 225 number of days in a year!" << '\n'; break; -
case 3: std::cout << "planet Earth has 365 number of days in a year!" << '\n'; break; -
case 4: std::cout << "planet Mars has 687 number of days in a year!" << '\n'; break; -
case 5: std::cout << "planet Jupiter has 4333 number of days in a year!" << '\n'; break; -
case 6: std::cout << "planet Saturn has 10759 number of days in a year!" << '\n'; break; -
case 7: std::cout << "planet Uranus has 30687 number of days in a year!" << '\n'; break; -
case 8: std::cout << "planet Neptune has 60190 number of days in a year!" << '\n'; break; -
enum planet { mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune }; -
int main() { -
planet p = venus; -
switch (p) { -
} -
}
Now let's generate a
bingo_board! We want to fill the 25spaces on thebingo_boardwith random values from 1 to 75 without repititon. To do this, we'll make avectorof 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 thebingo_board. We will do this entire process in multiple steps. First, write the functionrandom_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.
-
return std::uniform_int_distribution<std::size_t>{low, high}(engine); -
static std::mt19937 engine(std::random_device{}()); -
std::size_t random_int(std::size_t low, std::size_t high) { -
}
Now that we have the functions
random_intandswap_values, we can write the functiongenerate_rand_vec.generate_rand_veccreates avectorwith values from 1 to 75, shuffles it usingrandom_intandswap_values, and returns the shuffledvector. Use the lines to construct the code, then complete the code in the correct order.
-
for (std::size_t i = 0; i < vec.size(); ++i) { -
iota(vec.begin(), vec.end(), 1); -
return vec; -
std::size_t x = random_int(i, vec.size() - 1); -
std::vector<int> generate_rand_vec() { -
std::vector<int> vec(75); -
swap_values(vec, i, x); -
} -
}
Let's print out our
bingo_board! Write thebingo_boardmember functionprint_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.
-
for (std::size_t i = 0; i < board[j].size(); i++) { -
for (std::size_t j = 0; j < board.size(); j++) { -
std::cout << '\n'; -
std::cout << board[j][i].value << '\t'; -
void bingo_board::print_board () { -
} -
} -
}
You may have noticed that in some cases, our version of
bubble_sortdoes an unnecessary amount of work. For example, if ourvectorwas {1, 2, 3, 5, 4},bubble_sortwould swap 4 and 5, but then keep going even though ourvectoris already in order! We can save some work by including aboolcalledis_changed. If we swap values during a pass, we setis_changedto true. If nothing has been swapped, thenis_changedstays false, and we know to break out of the loop since ourvectoris already sorted. Write the functionfast_bubble_sort, which isbubble_sortwith this modification. Use the lines to construct the code, then complete the code in the correct order.
-
bool is_changed = false; -
break; -
for (std::size_t i = 0; i + 1 < vec.size(); ++i) { -
for (std::size_t j = 0; j + 1 < vec.size() - i; ++j) { -
if (is_changed == false) { -
if (vec[j] > vec[j + 1]) { -
is_changed = true; -
swap_values(vec, j, j + 1); -
void fast_bubble_sort(std::vector<int> &vec) { -
} -
} -
} -
} -
}
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.
-
break; -
break; -
break; -
case 0: -
case 1: -
day day = sun; -
default: -
enum day { mon = 1, tue, wed, thu, fri, sat, sun }; -
int main () { -
std::cout << "Invalid input." << '\n'; -
std::cout << "It is not the weekend :(" << '\n'; -
std::cout << "It is the weekend :)" << '\n'; -
switch (day > 5) { -
} -
}
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.
-
break; -
break; -
break; -
case 0: -
case 1: -
default: -
int input; -
int main () { -
std::cin >> input; -
std::cout << "Invalid input." << '\n'; -
std::cout << "Please enter an integer: "; -
std::cout << input << " is even!" << '\n'; -
std::cout << input << " is odd!" << '\n'; -
switch (input % 2) { -
} -
}
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.
-
break; -
break; -
case 0: -
case 1: -
default: -
int input1; -
int input2; -
int main () { -
std::cin >> input1; -
std::cin >> input2; -
std::cout << "Invalid input." << '\n'; -
std::cout << "Please enter first integer: "; -
std::cout << "Please enter second integer: "; -
std::cout << "The maximum is " << input1 << '\n'; -
std::cout << "The maximum is " << input2 << '\n'; -
switch (input1 > input2) { -
} -
}
Below is the pseudocode for the implementation of merge_sort. Put the blocks in the correct order!
-
card_deck card_deck::merge_sort () const { -
card_deck::merge_sort () const { #distractor -
divide each subdeck into two more subdecks -
divide the deck into two subdecks -
find the midpoint of the deck -
merge the two halves and return the result -
sort the subdecks using sort -
use a for loop to traverse half the deck #distractor -
}
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.
-
entry entries; #distractor -
entry word; #distractor -
int page; -
std::string word; -
std::vector<entry> entries; -
std::vector<Word> entries; #distractor -
struct dictionary { -
struct entry { -
}; -
};
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.
-
for (std::size_t i = 0; i < entries.size(); ++i) { -
for (std::size_t i = 1; i < dictionary.entries.size(); ++i) { #distractor -
for (std::size_t i = 1; i < entries.size(); ++i) { #distractor -
if (entries[i].word == word) { -
if (i.word == word) { #distractor -
return -1; -
return static_cast<std::ptrdiff_t>(i); -
std::ptrdiff_t dictionary::find (entry word) { -
std::ptrdiff_t dictionary::find (std::string word) { -
} -
} -
}
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.
-
for (std::size_t i = 0; i < entries.size(); ++i) { #distractor -
for (std::size_t i = start; i < entries.size(); ++i) { -
if (entries[i].word < entries[min].word) { -
if (entries[i].word > entries[min].word) { #distractor -
min = i; -
return min; -
std::size_t dictionary::find_first_word (std::size_t start) { -
std::size_t dictionary::find_first_word (std::string word) { -
std::size_t min = start; -
} -
} -
}
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.
-
entries[a] = entries[b]; -
entries[b] = temp; -
entry temp = entries[a]; -
void dictionary::swap () { -
void dictionary::swap (std::size_t a, std::size_t b) { -
}
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.
-
for (std::size_t i = 0; i < entries.size() - 1; ++i) { #distractor -
for (std::size_t i = 0; i < entries.size(); ++i) { -
int dictionary::alphabetize () { -
std::size_t min = find_first_word (0); #distractor -
std::size_t min = find_first_word (i); -
std::swap (0, min); #distractor -
std::swap (entries[i], entries[min]); -
void dictionary::alphabetize () { -
} -
}
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.
-
for (std::size_t i = 0; i < entries.size(); ++i) { -
std::cout << entries[i].entry << '\n'; #distractor -
std::cout << entries[i].word << '\n'; -
std::cout << entry.word << '\n'; #distractor -
void dictionary::print_dictionary () { -
} -
}