Lab 09: Arrays

Feb 6, 2024  β”‚  m. Feb 6, 2024 by Charlotte Curtis

Objectives

Setup

Before starting on this lab, double check your git configuration. Run the following:

$ git status

You should see something like:

On branch main
Your branch is ahead of 'teacher/main' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

where the specific number of commits may vary.

If this is the case, you’re good to do the usual git pull to fetch the new lab starter code.

If instead you see something like:

On branch main
Your branch and 'teacher/main' have diverged,
and have 1 and 2 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

you may need to run git pull --no-rebase instead of the usual git pull to make sure you don’t end up in another dangling commit situation.

Finally, cd into the arrays directory to begin the lab.

Main exercise

For this exercise, you’ll be reading a text file (using input redirection) and counting the number of occurrences of each letter in some input text. When complete, running your main program should look something like this:

$ ./main < text.txt
The most frequent letter is E.
It occurs 54 times!

Before you dive in, here’s a bit more info on the char data type:

Steps to complete

  1. In your main function, declare an array of 26 integers called counts and initialize it to all zeros. Note that a constant N_ALPHA has been defined in arrays.h - feel free to use it instead of hard-coding 26. This array will be used to count the number of occurrences of each letter in a text file.

    Yes, there are only 26 letters in the alphabet, but a constant is still a good idea - what if you wanted to extend it to different alphabets?

  2. Implement the function count_letters to read from cin one character at a time until the end of file is reached. Inside the loop, convert the character to uppercase and, if the character is a letter, increment the corresponding element of the counts array.

    You can convert to uppercase and check for characters using the toupper and isalpha functions from the cctype module . These functions work as follows:

    #include <cctype>
    ...
    ch = static_cast<char>(toupper(ch)); // ch is now upper case
    if (isalpha(ch)) {
        // ch is a letter
    }
    
  1. Implement the function highest_freq_index. This function should find and return the index of the first character with the highest frequency. Once more you’ll need to use the counts array and some kind of loop.

    Make sure to call this function from main and print the result to verify that it works before building and running the tests.

    Note: this function has a const qualifier on the counts parameter, while the count_letters function does not. What happens if you modify the declaration and definition of count_letters to include the const qualifier? Why?

  1. Declare and implement a function to print all characters with a given frequency. Test this function by calling it twice: Once for the highest frequency, and once for a frequency of 0.

Extra challenges

  1. Extend your function from step 4 to accept an additional parameter defining the number of instances to print. For example, to display only the first 3 characters that do not appear, you would pass the function a 0 for the frequency and a 3 for the number of instances.

  2. Write a function that takes two arrays of ten integers each as parameters, and prints out all the elements of the first which also occur in the second. You may assume neither array contains duplicate elements.

  3. Modify your function to the previous question to “return” the elements that match, rather than printing them. Remember that an array cannot be used as a return type in C++!

Assessment

If you haven’t run the steps to fix your labs configuration, do it now before you push anything. Follow the instructions here .

To build and run the test for this lab, cd to your top-level labs directory and run the command:

$ make lab=arrays

When you are satisfied with your highest_freq_index function, add and commit your changes to your labs repo, then git push to submit. Your instructor will be building and running the exact same tests to determine whether you passed the lab.

There’s been some confusion over which files need to be committed, and the short answer is anything that you’ve changed should be committed. An easy way to do this is to add all your modified files to the staging area with:

$ git add .

where the . is a shortcut for “current directory”.

Then commit and push as usual.



Previous: Lab 08: loops
Next: Lab 10: C-Strings