13.16. Activecode ExercisesΒΆ

Answer the following Activecode questions to assess what you have learned in this chapter.

Question

Write 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.

Example c192_mucp_13_1_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to use a switch statement to classify a day of the week.

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

Question

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.

Example c192_mucp_13_2_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to use a switch statement to check and print out whether a number is divisible by two.

Example c192_mucp_13_2_ac_a
 1#include <iostream>
 2using std::cout;
 3
 4int main () {
 5    int input;
 6    cout << "Please enter an integer: ";
 7    std::cin >> input;
 8    switch (input % 2) {
 9        case 0:
10            cout << input << " is even!" << std::endl;
11            break;
12        case 1:
13            cout << input << " is odd!" << std::endl;
14            break;
15        default:
16            cout << "Invalid input." << std::endl;
17            break;
18    }
19}

Question

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.

Example c192_mucp_13_3_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to use a switch statement to check and print out the maximum between two numbers.

Example c192_mucp_13_3_ac_a
 1#include <iostream>
 2using std::cout;
 3
 4int main () {
 5    int input1;
 6    int input2;
 7    cout << "Please enter first integer: ";
 8    std::cin >> input1;
 9    cout << "Please enter second integer: ";
10    std::cin >> input2;
11    switch (input1 > input2) {
12        case 0:
13            cout << "The maximum is " << input2 << std::endl;
14            break;
15        case 1:
16            cout << "The maximum is " << input1 << std::endl;
17        default:
18            cout << "Invalid input." << std::endl;
19            break;
20    }
21}

Question

Write the pseudocode for the implementation of merge_sort.

Example c192_mucp_13_4_ac_q
1// YOUR PSEUDOCODE HERE

Answer

Below is one way to write the pseudocode of merge_sort.

Example c192_mucp_13_4_ac_a
1// card_deck card_deck::merge_sort () const {
2//     find the midpoint of the deck
3//     divide the deck into two subdecks
4//     sort the subdecks using sort
5//     merge the two halves and return the result
6//     divide each subdeck into two more subdecks
7// }

Question

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.

Example c192_mucp_13_5_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is one way to write the struct definition for entry and for dictionary.

Example c192_mucp_13_5_ac_a
 1#include <string>
 2#include <iostream>
 3#include <vector>
 4
 5struct entry {
 6    std::string word;
 7    int page;
 8};
 9
10struct dictionary {
11    std::vector<entry> entries;
12};

Question

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.

Example c192_mucp_13_6_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is one way to write the dictionary member function.

Example c192_mucp_13_6_ac_a
 1#include <cstddef>
 2#include <string>
 3#include <iostream>
 4#include <vector>
 5
 6struct entry {
 7    std::string word;
 8    int page;
 9};
10
11struct dictionary {
12    std::vector<entry> entries;
13public:
14    std::ptrdiff_t find(std::string word);
15};
16
17std::ptrdiff_t dictionary::find (std::string word) {
18    for (std::size_t i = 0; i < entries.size(); ++i) {
19        if (entries[i].word == word) {
20            return static_cast<std::ptrdiff_t>(i);
21        }
22    }
23    return -1;
24}

Question

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).

Example c192_mucp_13_7_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is one way to write the find_first_word member function.

Example c192_mucp_13_7_ac_a
 1#include <cstddef>
 2#include <string>
 3#include <iostream>
 4#include <vector>
 5
 6struct entry {
 7    std::string word;
 8    int page;
 9};
10
11struct dictionary {
12    std::vector<entry> entries;
13public:
14    std::size_t find_first_word(std::size_t start);
15};
16
17std::size_t dictionary::find_first_word (std::size_t start) {
18    std::size_t min = start;
19    for (std::size_t i = start; i < entries.size(); ++i) {
20        if (entries[i].word < entries[min].word) {
21            min = i;
22        }
23    }
24    return min;
25}

Question

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.

Example c192_mucp_13_8_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is one way to write the swap member function

Example c192_mucp_13_8_ac_a
 1#include <cstddef>
 2#include <string>
 3#include <iostream>
 4#include <vector>
 5
 6struct entry {
 7    std::string word;
 8    int page;
 9};
10
11struct dictionary {
12    std::vector<entry> entries;
13public:
14    void swap(std::size_t a, std::size_t b);
15};
16
17void dictionary::swap (std::size_t a, std::size_t b) {
18    entry temp = entries[a];
19    entries[a] = entries[b];
20    entries[b] = temp;
21}

Question

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!

Example c192_mucp_13_9_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is one way to write the dictionary member function alphabetize.

Example c192_mucp_13_9_ac_a
 1#include <algorithm>
 2#include <cstddef>
 3#include <string>
 4#include <vector>
 5struct entry { std::string word; std::size_t page; };
 6struct dictionary {
 7    std::vector<entry> entries;
 8    void alphabetize();
 9    std::size_t find_first_word(std::size_t start) const {
10        std::size_t lowest = start;
11        for (std::size_t i = start; i < entries.size(); ++i) {
12            if (entries[i].word < entries[lowest].word) lowest = i;
13        }
14        return lowest;
15    }
16};
17void dictionary::alphabetize() {
18    for (std::size_t i = 0; i < entries.size(); ++i) {
19        const std::size_t lowest = find_first_word(i);
20        std::swap(entries[i], entries[lowest]);
21    }
22}

Question

Let's check to see if our sorting worked! Write the dictionary member function print_dictionary, which prints out the word in each entry.

Example c192_mucp_13_10_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is one way to write the dictionary member function print_dictionary.

Example c192_mucp_13_10_ac_a
 1#include <cstddef>
 2#include <string>
 3#include <iostream>
 4#include <vector>
 5
 6struct entry {
 7    std::string word;
 8    int page;
 9};
10
11struct dictionary {
12    std::vector<entry> entries;
13public:
14    void print_dictionary();
15};
16
17void dictionary::print_dictionary () {
18    for (std::size_t i = 0; i < entries.size(); ++i) {
19        std::cout << entries[i].word << std::endl;
20    }
21}