Activecode Exercises

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

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.

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

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.

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

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.

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

Write the pseudocode for the implementation of mergeSort.

Below is one way to write the pseudocode of mergeSort.

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.

Below is one way to write the struct definition for Entry and for Dictionary.

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.

Below is one way to write the Dictionary member function.

Of course, all dictionaries are in some sort of order. In order to do this, we must first write the Dictionary member function findFirstWord, 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).

Below is one way to write the findFirstWord member function.

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.

Below is one way to write the swap member function

Now let’s write the Dictionary member function alphabetize, which sorts the Entries in the Dictionary in alphabetical order. Use the findFirstWord and swap functions we defined earlier!

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

Let’s check to see if our sorting worked! Write the Dictionary member function printDictionary, which prints out the word in each Entry.

Below is one way to write the Dictionary member function printDictionary.

You have attempted of activities on this page