Lab 9: Structs I
- Due No Due Date
- Points 1
Note: You are encouraged to work on the following with a partner.
Overview
In this lab, you'll get more practice with functions and structs.
Learning outcomes
By the end of this lab, you should be more comfortable...
- defining and invoking functions
- defining and using structs
- passing structs to functions
Exercises
zyLab
Complete the following zyLabs in the zyBook. While you do them, be sure to implement incrementally—write a line or two of code, then compile/run it. Test frequently by clicking "Submit mode" and then "Submit for grading". You have unlimited submits, so don't worry about using some quota up.
- zyLab 6.33: Even/odd values in a vector
- zyLab 6.40: Warm up: Text analyzer & modifier
- zyLab 16.30: Account struct
- zyLab 16.32: Account struct: print function
Submitting
For the zyLabs, Click "Submit mode" then "Submit for grading" when you are finished so I can see that you have done the zyLab.
PA options
Please see the syllabus and course schedule (both on the homepage) for more information about how many programming assignments you are required to pass, due dates, etc. Of note: you do not need to attempt every or even most PAs.
PA 9.1: Complete any single previously listed PA
Pick one of the previously listed PA options. You can only do one, and you can't pick an earlier version of a PA that you've already received credit for. Here they are as a reminder:
- PA 2.1: Area under the curve
- PA 3.1: Adaptive quiz
- PA 4.1: Password generator+ (can't do if you've already submitted PA5.1 or PA7.2)
- PA 5.1: Password generator++ (can't do if you've already submitted PA7.2)
- PA 6.1 (Advanced): Word stats (can't do if you've already submitted PA7.1)
- PA 7.1 (Advanced): Word stats with functions
- PA 7.2: Password generator++ with functions
- PA 8.1: Grid world with points
- PA 8.2 (advanced): Grid word with random layouts
When you submit, add the following above the specs that you paste at the top of your source code file:
// In fulfillment of PA9.1
PA 9.2: Palindrome detector
In zyLab 4.29, you wrote code to detect if a phrase is a palindrome (appears the same forwards as backwards). In this PA, you will build on that detector as follows. The program should read in phrases from the user (a phrase may include spaces), one line each, until the user enters END by itself on a line. The program should read these into a vector of strings (let's call this `phrases`). In a parallel vector of bools (let's call this `palindromeStatus`), you should keep track of whether the corresponding phrase in the phrases vector is a palindrome using the first function listed below. Once all the phrases have been read in and processed, the program should display all of the phrases that are palindromes followed by all the phrases that aren't; diaplaying the palindromes/non-palindromes should be done by invoking the second two functions listed below.
Your program must define the following functions and include a JavaDoc above each:
- bool IsPalindrome(string phrase)—returns true if the given phrase is a palindrome and false otherwise
- void PrintPalindromes(vector<string> phrases, vector<bool> palindromeStatus)—prints a header "Palindromes:" followed by all of the phrases (indented slightly) that are palindromes to the console
- void PrintNonPalindromes(vector<string> phrases, vector<bool> palindromeStatus)—prints a header "Non-palindromes:" followed by all of the phrases (indented slightly) that are not palindromes to the console
Here are the specs (make sure to copy and paste these at the top of your source code; fill out your name, date, etc. and place an 'x' in each of the boxes; if any of the boxes aren't checked, then it's not ready to submit!).
// Name:
// Date:
// Partner:
//
// Specifications checklist for PA Option 9.2
// [ ] the header includes your name and anyone you worked with
// [ ] the header includes this specifications checklist
// [ ] the code in indented properly (use the autoformatter if you're not sure)
// [ ] useful comments are present above chunks of code that "hang together" (work toward a higher level goal)
// [ ] the identifiers are well named
// [ ] the program compiles
// [ ] the program runs without crashing or hanging
// [ ] all output and prompts look clean (correct spelling, capitalization, nothing squished)
// [ ] all prompts specify the required format of the input and work as expected
// [ ] the program allows the user to enter as many phrases as they want, one per line
// [ ] END is used as a sentinel and is not included in any of the processing or later output
// [ ] palindromes are accurately classified
// [ ] all palindrome phrases are listed in the output of the program above the non-palindrome phrases
// [ ] there's a "Palindromes:" header above the list of palindrome phrases
// [ ] there's a "Non-palindromes:" header above the list of no-palindrome phrases
// [ ] all phrases in both lists are indented slightly
// [ ] each of the required functions are defined and invoked properly
// [ ] bool IsPalindrome(string phrase)
// [ ] void PrintPalindromes(vector<string> phrases, vector<bool> palindromeStatus)
// [ ] void PrintNonPalindromes(vector<string> phrases, vector<bool> palindromeStatus)
// [ ] all functions have a properly formatted, completed, and accurate JavaDoc above them
PA 9.3 (advanced): Word stats with structs
Complete PA6.1 (or PA7.1), but instead of using parallel vectors to store downcased words and their frequencies, use a single vector of struct instances. Define the struct WordFreq that tracks two data members: word and frequency.
Here are the specs:
// Name:
// Date:
// Partner:
//
// Specifications checklist for PA Option 9.3 (Advanced)
// [ ] the header includes your name and anyone you worked with
// [ ] the header includes this specifications checklist
// [ ] the code in indented properly (use the autoformatter if you're not sure)
// [ ] useful comments are present above chunks of code that "hang together" (work toward a higher level goal)
// [ ] the identifiers are well named
// [ ] the program compiles
// [ ] the program runs without crashing or hanging
// [ ] all output and prompts look clean (correct spelling, capitalization, nothing squished)
// [ ] all prompts specify the required format of the input and work as expected
// [ ] the program prompts the user for their text
// [ ] all words entered by the user are read in until the sentinel END is read in
// [ ] the END sentinel is not used in any of the stats
// [ ] the program uses a vector of type WordFreq to store downcased words and their counts
// [ ] the WordFreq struct is defined correctly
// [ ] words are stored and reported at the end in all lower case
// [ ] these stats are displayed at the end and are accurate:
// [ ] the number of total words entered
// [ ] the number of distinct words (case insensitive)
// [ ] the number of characters (the length of all input words summed together)
// [ ] the distribution of distinct words and their corresponding counts represented as a bar graph
// [ ] the left side of the bar graphs are aligned across words
Submit your PA to First programming assignment, Second programming assignment, Third programming assignment, Fourth programming assignment, or Fifth programming assignment based on whether this is your first, second, etc. PA. If you are working with a partner, both of you must submit individually.
Rubric
Criteria | Ratings | ||
---|---|---|---|
Worked diligently on the lab problems
|
|
||
Submitted all materials according to the instructions
|
|
||
|