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. Complete each of the following string manipulation problems and code your solutions into the worksheet where indicated by the comments. See the Example Run to verify the correctness of each computation.
    1. Measure the length of both words entered by the user and save the length in the related variable. Then use an if-statement to compare the two word lengths and print the word that is the shortest.
    2. Concatenate first, second, and first words in that order with no spaces between them. Assign the result of the concatenation operations to variable msg.
    3. Concatenate first two letters of both words in the order they were entered twice. Assign the result of the concatenation operations to variable extract. Assume that both words are at least 2 characters long. See the Example Run to verify correctness.
    4. Concatenate the first 2 letters of the first word with the second word, followed by the remainder of the first word. Assume that the first word is at least 2 characters long. See the Example Run to verify correctness.
    5. Compare the first letter of both words to see which one is the smallest (earliest in the alphabet). Print the print smallest letter followed by largest letter. See the Example Run to verify correctness.
  5. 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.
    *** Word Worksheet ***
    Input two words separated by a space, and press the Enter key
    For example: Hello World!<Enter>: Hello World!
    
    You input: first = "Hello" and second = "World!"
    Parenthesis below show correct results for example words.
    
    *** Measuring word length ***
    First word length (5): 5
    Second word length (6): 6
    Shortest word (Hello): Hello
    
    *** Concatenating strings ***
    Concatenated (HelloWorld!Hello): HelloWorld!Hello
    
    *** Extracting substrings ***
    Extracted (HeWoHeWo): HeWoHeWo
    
    *** Combining parts of words ***
    Combinations (HeWorld!llo): HeWorld!llo
    
    *** Comparing first letter of both words ***
    Smallest then largest letter (HW): HW
    

    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.

  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.

J😀kes

Project 2: Computer Jokes

In this project we program our computer to tell jokes. A typical joke is a story with a funny punchline.

Project Specifications
  1. Write a program that tells various jokes depending on the user input.
  2. Name the source code file jokes.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 (and no other input) in this order and each on their own line, as shown in the Example Run below:
    1. Want to hear a bad joke (yes/no)?
    2. Want to hear another joke (yes/no)?

    Assume the user inputs only "yes" or "no" (without the double quotes) in answer to the two questions.

  4. For the first question, Want to hear a bad joke (yes/no)?
    1. If the user inputs yes, the program must print out the computer joke:
      "Q: Why did the computer get cold?
      A: Because it forgot to close Windows."
      
    2. If the user inputs no, the program must print the classic Mae West quote:
      "Between two evils, I always pick the one I never tried before."
      -Mae West

    Note that the output must display the "" around the jokes for full credit.

  5. For the second question, Want to hear another joke (yes/no)?
    1. If the user inputs yes, have the program print a joke that you like. Please keep the jokes appropriate for family-oriented programming.
    2. If the user inputs no, the program must print the following message.
      Okay, you tell me a joke then.
  6. Note that you are required to use exactly two if statements and two else statements to receive full credit on this assignment. (No else if needed!)
  7. Example Run: Your program must work identically to the example output below for full credit, including the same order of input and wording of the output. For the input shown you must get the same output with the exception that you print your own joke where the output shows, Put you own joke here....
    This Computer Jokes
    =-=-=-=-=-=-=-=-=-=
    Want to hear a bad joke (yes/no)? no
    Okay. I will tell you a good one:
    
    "Between two evils, I always pick the one I never tried before."
    - Mae West
    
    Want to hear another joke (yes/no)? yes
    
    Put you own joke here...
    
    This Computer Jokes
    =-=-=-=-=-=-=-=-=-=
    Want to hear a bad joke (yes/no)? yes
    
    "Q: Why did the computer get cold?
    A: Because it forgot to close Windows."
    
    Want to hear another joke (yes/no)? no
    
    "Okay, you tell me a joke then."
    

    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.

  8. After displaying the output, exit the program.
  9. Submit the source code file jokes.cpp with the rest of the assignment as described in Deliverables.

Image
Image source (modified)

Project 3: Automating Decisions

In this project we study how to make simple decisions using a computing model known as the perceptron [1]. A perceptron is a type of artificial neuron, which seeks to simulate neurons in the human brain [2]. Perceptrons were invented in the 1950s and 1960s by the scientist Frank Rosenblatt [3] and are used in machine learning, which is a branch of artificial intelligence that allows computers systems to automatically "learn" and improve from experience [4].

As shown in the image, perceptrons work by taking several binary inputs and producing a single binary output. Each of the inputs is multiplied by a weight and all the weighted-inputs are added to gether to get a single sum. The output is determined by whether or not the sum of the weighted inputs meets a threshold value [5]. If the weighted sum is ≤ the threshold then the output is false. If the weighted sum is > the threshold then the output is true.

Perceptron Example

As an example of a three-input perceptron, we list three criteria that could be used for making a decision on whether to attend class on a given day:

  1. You are unfamiliar with the lecture information
  2. You do not need the time to study on your own
  3. You have slept enough

We store the state of these criteria with binary variables `x_1, x_2`, and `x_3`. As an example, we have `x_1 = 1` if you are unfamiliar with the lecture content. Similarly, `x_2 = 1` if you have enough time to study for other classes and `x_3 = 1` if you are not too tired.

For each of these criteria we assign weights `w_1, w_2`, and `w_3`. For example we may assign a weight `w_1 = 5` for not knowing the class material and a weights of `w_2 = 2`, and `w_3 = 2` for the other criteria. The larger value of `w_1 = 5` indicates learning the material matters a lot to you, much more than the other criteria. Finally, we choose a threshold of `6` for the perceptron. With these choices you go to class if the class material is unknown and you have your study time under control or are not too tired.

By varying the weights and the threshold, we can get different models of decision-making. For example, by lowering the threshold to 4, we will go to class whenever we do not know the material or when we have the study time and had enough sleep. Dropping the threshold means you are more willing to go to class.

Project Specifications
  1. Write a program that makes a decision based on user input for three seperate criteria. Choose any decision and criteria that you like except for attending class.
  2. Name the source code file perceptron.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 (and no other input) in this order and each on their own line, as shown in the Example Run below:
    1. Weight of criteria one
    2. Weight of criteria two
    3. Weight of criteria three
    4. Threshold value
    5. State of criteria one
    6. State of criteria two
    7. State of criteria three

    Assume the user enters only valid data.

  4. After collecting the input, multiply the weight times the state of each criteria and sum the multiplied values.
  5. Next, compare the decision value against the threshold and display true or yes in the decision is above the threshold and false or no if the decision is below the threshold. In math terms:

    Sum of the multipled values

  6. 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 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.
    On a scale of 1-10, rate the following criteria:
    How important is the lecture content? 5
    How important is using the time to study for another class? 2
    How important is enough sleep? 2
    Enter the decision threshold (lower is more likely): 6
    
    For your decision:
    If lecture content is unlearned input 1 else 0: 1
    If the time is NOT needed for study input 1 else 0: 1
    If you have slept enough input 1 else 0: 0
    
    The decision to attend class is: Yes
    

    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. The values in (parentheses) are expected values when entering the example input.

  7. The output must be on a new line and contain a separate word the says either "yes" or "true" for affirmative decisions and "no" or "false" for negative decisions.
  8. After displaying the output, exit the program.
  9. Add a while statement that allows the user to repeat the decision part of the program by entering a "y" (without the quotes).
  10. Submit the source code file perceptron.cpp with the rest of the assignment as described in Deliverables.
References and More Information
  1. Perceptron: Wikipedia article.
  2. Artificial neuron: Wikipedia article.
  3. Frank Rosenblatt: Wikipedia article
  4. What is Machine Learning? A definition
  5. Perceptrons: from the book Neural Networks and Deep Learning
  6. Neural Network Definition

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. Add an additional interesting and useful string manipulation like those done in wordwork.cpp making use of both input variables and a string function that we have covered. (2 points: 1 point for completing with a string function and 1 point for interest or usefulness.)
    1. Submit the source code with the extra string manipulation in a file named xcwordwork.cpp.
    2. Describe the purpose of the string manipulation in your README.txt file.
    3. Include the data input section of the original wordwork.cpp file.
    4. Follow the output format of the previous problems by:
      1. Printing a title for the problem surrounded by asterisks (*) like, *** Comparing last letter of both words ***.
      2. On the next line print a label for the problem like, Largest-smallest last letter,
      3. followed by the expected value inside parenthesis when entering "Hello World!" like, (o!),
      4. followed by a colon and the output of the string manipulation like, : o!.

      Example Extra Credit Output
      *** Extra Credit Word Worksheet ***
      Enter two words separated by a space, and press the Enter key
      For example: Hello World!<Enter>: Hello World!
      
      You entered: first = "Hello" and second = "World!"
      Parenthesis below show correct results for example words.
      
      *** Comparing last letter of both words ***
      Largest-smallest last letter (o!): o!
      
  3. Attend an SI session for this assignment and sign the roll sheet. (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. longword.cpp
  4. pirate.cpp
  5. phonebot.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, you should 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 can 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 08 2019 @17:50:02