A3-Strings and Conditions

Table of Contents


Objectives

  • Work with sequences of variables
  • Work with different types of data
  • Start declaring and using string variables
  • Start calling (invoking) string member functions
  • Start using if-statements.
  • Explore pair programming.

Academic Honesty

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

  • You are encouraged to work with one other student of this class following the rules of Pair Programming for Homework Assignments. If you choose to pair program, there is a bonus applied.
  • You may not give a copy of your code to your designated pair-programming partner if you did not develop the code together.
  • You may not show your completed code to another person or look at another person's code until you complete and submit this assignment, except for code you develop together with your pair-programming partner.
  • You may get help from people other than your pair-programming partner if you get stuck, but only if they do not show or tell you the code to type.
  • Remember that the instructor performs similarity tests on programming project submissions, and copied or plagiarized code is usually very easy to detect.

Preparation

  1. Make sure you have completed the exercises from lesson 3.
  2. Complete the Review Exercises in CodeLab 3. These exercises will help prepare you for the problem-solving programs and should be completed first.

Project Specifications

Your solutions to these project must only use techniques we have covered so far.

Programming Style

For all programs, remember to follow all the style rules we covered including:

  1. File block comments.
  2. Placement of curly braces and indentation within curly braces, including if-else-if-else Formatting.
  3. Limiting line length to about 80 characters.

    TextPad, and most other text editors, tell you both the line and column location, which lets you check the line length.

  4. No tab characters in your code.

    You can remove tab characters by either setting up TextPad correctly (see here) or by running a program named astyle (see here).

  5. Spaces before and after operators.
  6. Meaningful variable names and consistent naming style (caps vs. underbars).

Image
Image source: Ed Parrish

Project 1: Word Worksheet

Use this worksheet to improve your understanding of how to work with strings and if-statements, including:

  • String variables (lesson 3.2.3)
  • String input and output (lesson 3.2.3)
  • Concatenation (lesson 3.2.5)
  • String functions (lesson 3.2.6)
  • Comparing strings (lesson 3.3.3)

Refer to lesson 3 for more information on C++ strings and if-statements. Do not use string functions we have not covered in the lecture notes.

For this worksheet, the user enters two words. You write code to display the computations as specified below.

Project Specifications
  1. Start by downloading the worksheet: wordwork.cpp.

    Keep the same filename and add to the existing code to complete the project. Leave the existing code unchanged, except as explained with comments and these instructions. Include all your code in this single file.

  2. Add your name and the date to the file comment block at the top of the file.
  3. User input is already coded into the worksheet.

    Do not add any other input commands or change the input order.

  4. Read the comments in the worksheet file and write the code where indicated by the comments. See the Example Runs to verify the correctness of each computation.
  5. Example Runs: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and exact wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.
    *** Word Worksheet ***
    Input two words separated by a space, and press the Enter key
    For example: A word!: A word!
    
    You input: one = "A" and two = "word!"
    Parenthesis below show correct results for example words.
    
    *** Measuring word length ***
    First word length (1): 1
    Second word length (5): 5
    Words longer than 1 character (word!): word!
    
    *** Concatenating strings ***
    Concatenated (A-word!): A-word!
    
    *** Extracting substrings ***
    Extracted (A!): A!
    
    *** Combining parts of words ***
    Combinations (A!A): A!A
    
    *** Comparing first two letters of both words ***
    Smallest two then largest two letters (Awo): Awo
    
    *** Word Worksheet ***
    Input two words separated by a space, and press the Enter key
    For example: A word!: Low down
    
    You input: one = "Low" and two = "down"
    Parenthesis below show correct results for example words.
    
    *** Measuring word length ***
    First word length (1): 3
    Second word length (5): 4
    Words longer than 1 character (word!): Lowdown
    
    *** Concatenating strings ***
    Concatenated (A-word!): Low-down
    
    *** Extracting substrings ***
    Extracted (A!): Ln
    
    *** Combining parts of words ***
    Combinations (A!A): LnL
    
    *** Comparing first two letters of both words ***
    Smallest two then largest two letters (Awo): Lodo
    

    In the above example runs, the user entered the values shown in aqua italics (for emphasis) to produce the output. Your program does NOT print the characters in aqua italics, nor does the user input appear in aqua italics. Notice that words with a single character do not print a second letter in the last problem.

  6. After displaying the output, exit the program.
  7. Submit the source code file wordwork.cpp with the rest of the assignment as described in Deliverables.
Hints:
  • For problem 5, our program must compare the first two letters of both words. However, what if one of the words has only one letter? Then we compare the single letter word against the first two letters of the multiple-letter word.

Image
Image source

Project 2: Haiku Mad Libs

Haiku is a Japanese poetic form that consists of 17 syllables arranged in three lines containing five, seven, and five syllables, respectively. A good haiku expresses much and suggests more in the fewest possible words. [1]

Mad Libs is a word game where one player prompts others for a list of words to substitute for blanks in a story, before reading the story [2]. When prompted, the user inserts a specific type of word. Once all words are added, the text is read.

In this project, we will write a computer program to create a Haiku poem in Mad-Libs style. The computer will prompt for the words and display two poems in the terminal window, with unique titles for both Haiku's. After reading the poems the user votes for their favorite. Following the vote is a brief analysis of the words entered.

Project Specifications
  1. Write a program that asks the user for three words and uses the words to complete two haiku poems.
  2. Name the source code file haiku.cpp and include all your code in this single file.

    Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

  3. Ask the user for the following inputs in this order, as shown in the Example Run below:
    1. A one syllable noun
    2. A two syllable adjective
    3. a two syllable gerund (a noun ending in "ing")
  4. After getting the input words, output the following word analysis:
    1. The first word and the number of letters in the first word
    2. The second word and the number of letters in the second word
    3. The third word and the number of letters in the third word
    For example the input words: car purple driving would produce the word analysis:
    Word "car" has 3 characters
    Word "purple" has 6 characters
    Word "driving" has 7 characters
    

    Notice the double quotes (") and be sure to include them in the word analysis output.

  5. From the input words, construct two titles (one for each Haiku) using the following algorithms:
    1. Title 1:
      1. Select the first letter of word one
      2. Select the last letter of word two
      3. Select the first letter of word three
      4. Concatenate these three letters between the strings "My " and " Haiku"
      For example the input words: car purple driving would produce the title:
      My ced Haiku
      
    2. Title 2:
      1. Select the last letter of word one
      2. Select the first letter of word two
      3. Select the last letter of word three
      4. Concatenate these three letters between the strings "My " and " Haiku"
      For example the input words: car purple driving would produce the title:
      My rpg Haiku
      

    See lesson 3.2.6 for more information.

  6. Substitute the titles and input strings into the following two poems and output the poems to the terminal window as shown in the Example Run below:
    (Title 1)
    ---------
    When begins the (input 3a)
    And the (input 3b) Crocus blooms
    (input 3c) is watching
    
    (Title 2)
    ---------
    Flowers sit in (input 3a)
    Shades of the (input 3b) tree near
    (input 3c) in sunset
    
  7. Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and exact wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.
    Haiku Mad Libs Generator
    
    Enter a one syllable noun: car
    Enter a two syllable adjective: purple
    Enter a two syllable gerund (noun ending in "ing"): driving
    
    Word "car" has 3 characters
    Word "purple" has 6 characters
    Word "driving" has 7 characters
    
    My ced Haiku
    ------------
    When begins the car
    and the purple Crocus blooms
    driving is watching
    
    My rpg Haiku
    ------------
    Flowers sit in car
    shades of the purple tree near
    driving in sunset
    

    In the above example run, the user entered the values shown in aqua italics (for emphasis) to produce the output. Your program does NOT print the characters in aqua italics, nor does the user input appear in aqua italics.

  8. After displaying the output, exit the program.
  9. Submit this project with the rest of the assignment as described in Deliverables.
References and More Information
  1. Haiku: Merriam-Webster dictionary
  2. Mad Libs: Wikipedia article

Image
Complete this project!

Project 3: Grade Values

Academic grades in the US are traditionally given as letter grades: A, B, C, D, and F. Many colleges, though not Cabrillo College, add a plus or minus to the grade to increase the accuracy of student evaluations. In this project we translate letter grades into their numerical value to calculate a grade point average (GPA).

Project Specifications
  1. Write a program that converts a letter grade into it's numerical value using the following conversion table.
    Letter Grade GPA
    A 4.00
    A- 3.67
    B+ 3.33
    B 3.00
    B- 2.67
    C+ 2.33
    C 2.00
    C- 1.67
    D+ 1.33
    D 1.00
    D- 0.67
    F 0.00
  2. Name the source code file gradevalue.cpp and include all your code in this single file.

    Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

  3. Ask the user for a single input (string) of a letter grade of A, B, C, D, or F, possibly followed by a + or -, and no other input as shown in the Example Runs below.

    Assume the user only enters correct data.

  4. Convert the letter grade into the numerical equivalent shown in the table in specification 1.

    Notice that the highest number in the table is 4.0 and that there are no F+ or F- grades. Make sure that the highest grade number is 4 and that F+, F and F- are assigned values of 0.

  5. Display the output using the default formatting and precision for the numbers -- do NOT add any numerical formatting statements to the code.
  6. Example Runs: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.
    Enter a letter grade: B-
    The numeric value is 2.67
    
    Enter a letter grade: A+
    The numeric value is 4
    
    Enter a letter grade: D-
    The numeric value is 0.67
    
    Enter a letter grade: F+
    The numeric value is 0
    

    In the above example runs, the user entered the values shown in aqua italics (for emphasis) to produce the output. Your program does NOT print the characters in aqua italics, nor does the user input appear in aqua italics.

  7. After displaying the output, exit the program.
  8. Submit this project with the rest of the assignment as described in Deliverables.

Extra Credit

The following are worth extra credit points:

  1. Complete the assignment using pair programming with the same person for all three projects. (2 points)
  2. Complete the gradevalue.cpp program with 8 or fewer relational expressions and without using techniques we have not covered. (2 points)

    The program must work correctly to get this extra credit.

  3. Attend an SI session for this assignment, sign the roll sheet and type in your README.txt, "attended SI" followed by the date(s) attended. (2 points)

Make certain that your README.txt file describes any extra credit completed.

Tutorial Lab

In preparation for next weeks lessons, complete the following:

  1. Read the assigned reading in the textbook
  2. Complete the Tutorial Exercises in CodeLab 3 before the specified due date.

    Refer to the assigned reading for the next lesson to help you understand the problems. Also, you can use the online lecture notes for more information as the notes become available. You can look at solutions if you miss your first few attempts and are stuck by clicking the "Solution" tab.

Grading Criteria

The instructor will evaluate your assignment using the following criteria. Thus you should check your assignment against these criteria to maximize your score.

Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive. Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the syllabus.

Lesson Exercises

  • 2: All lesson exercises attempted and turned in
  • 1: Some lesson exercises completed and turned in
  • 0: No lesson exercises completed or turned in

Projects (x3)

  • 5: Demonstrates mastery of the program
    • Applies concepts from the lessons appropriately
    • Meets all specifications (see above)
    • Runs to completion with no abnormal error conditions
    • Generates correct output given correct input
    • Correct file name
  • 4: Has most of the functionality expected of the program
    • Demonstrates some techniques from the lesson
    • Attempts to meet all but one of the specifications (see above)
    • Implementation seems more complicated than necessary.
    • May have one minor error
  • 3: Has some of the functionality expected of the program
    • Demonstrates some techniques from the lesson
    • Attempts to meet at least 1/2 of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have 2-3 minor errors
  • 2: Serious functional problems but shows some effort and understanding
    • Attempts to meet less than 1/2 of the of the specifications (see above)
    • Has a major error or many minor errors
    • Implementation seems very convoluted
    • Demonstrates few techniques from the lesson
  • 1: Does not compile or wrong file turned in
  • 0: Not turned in or uses techniques not covered

Project's Style

  • 3: Code is well-documented including:
  • 2: Code has a minor documentation error
  • 1: Code has some documentation errors
  • 0: No apparent attempt to follow documentation standards or write documentation comments

CodeLab Exercises

  • Number CodeLab completed correctly / number exercises * 8 and rounded up to the nearest integer.

README.txt File

  • 2: README.txt file submitted following the instructions
  • 1: README.txt file submitted but some information was missing
  • 0: No README.txt file submitted

Total possible: 30, plus extra credit

Deliverables

Students submit some homework as they work on it like CodeLab. However, students must submit other homework in Canvas following the link to A3-Strings and Conditions. Include the following items when submitting to Canvas:

  1. README.txt file prepared by following the instructions for submitting homework.
  2. All the exercise files from Lesson 3 including:
    • nameapp.cpp
    • selection.cpp
    • students.txt
  3. wordwork.cpp
  4. haiku.cpp
  5. gradevalue.cpp

Note: Make certain your programs compile before you turn them in. When a program does not compile then it does not function either. For all programming projects, expect little or no credit if your program does not compile and run. For more information see the Grading Criteria.

You must submit all the files needed to complete your assignment. Your assignment must work as submitted. Remember to test and double check your files before submitting them. If you make a mistake, you may resubmit up to the deadline. If you resubmit, you must include all your assignment files in the last submission as Canvas hides prior submissions.

Last Updated: February 18 2020 @20:07:50