Objectives
- Get familiar with C-style arrays
- Learn about errors with arrays
- Learn about how arrays interact with functions
- Experiment with integer representations of characters
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:
As an integral data type,
char
is a special flavour of integer, so it is possible to perform relational operations on characters. For example, the following Boolean expression tests whetherch
contains an uppercase letter:ch >= 'A' && ch <= 'Z'
It is also possible to perform arithmetic with characters. To map an uppercase character (
'A'
through'Z'
) to its corresponding array index (0
through25
), simply subtract'A'
from the character:int index; index = static_cast<int>(ch) - static_cast<int>('A');
Note: You can technically do this without the
static_cast
, but it’s good practice to be explicit about what you’re doing.
Steps to complete
In your
main
function, declare an array of 26 integers calledcounts
and initialize it to all zeros. Note that a constantN_ALPHA
has been defined inarrays.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?
Implement the function
count_letters
to read fromcin
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 thecounts
array.You can convert to uppercase and check for characters using the
toupper
andisalpha
functions from thecctype
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 }
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 thecounts
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 thecounts
parameter, while thecount_letters
function does not. What happens if you modify the declaration and definition ofcount_letters
to include theconst
qualifier? Why?
- 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
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 a3
for the number of instances.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.
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.