13.17. Mixed Up Code Practice¶
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." << std::endl; -
std::cout << "It is not the weekend :(" << std::endl; -
std::cout << "It is the weekend :)" << std::endl; -
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." << std::endl; -
std::cout << "Please enter an integer: "; -
std::cout << input << " is even!" << std::endl; -
std::cout << input << " is odd!" << std::endl; -
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." << std::endl; -
std::cout << "Please enter first integer: "; -
std::cout << "Please enter second integer: "; -
std::cout << "The maximum is " << input1 << std::endl; -
std::cout << "The maximum is " << input2 << std::endl; -
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 << std::endl; #distractor -
std::cout << entries[i].word << std::endl; -
std::cout << entry.word << std::endl; #distractor -
void dictionary::print_dictionary () { -
} -
}