# A6-Loopy Programs

## Objectives

• Apply counting loops to display data
• Use indefinite loops to gather data
• Develop the ability to find minimum and maximum values
• Code nested loops

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 plagiarized code is usually very easy to detect.

## Preparation

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

## Project Specifications

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

#### Programming Style

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

1. No magic numbers
2. Indentation in `while` statements and placement of curly braces
3. 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).

4. Meaningful variable names and consistent naming style (caps vs. underbars).

#### Project 1: Loop Worksheet

Counting loops are very commonly used in programming to ... count things. Character data types are used to store and display individual letters and other symbols. The string type stores sequences of characters. In this project we explore the relationships between counting loops, characters and strings in C++.

##### Project Specifications

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 steps and code your solutions into the worksheet where indicated by the comments. See the Example Run to verify the correctness of each output.
1. Use a `for`-loop to print the number that the user entered (`n`) followed by a space for `n` times, all on the same line.
See lesson 5.1.2 for information on `for`-loops.
2. Use a `for`-loop to print a row of "`x`" on a single line that is `n` characters long.
See lesson 5.2.1 for an example.
3. Use a `for`-loop to print the message (`msg`) followed by a newline `n` times.
See lesson 5.1.2 for information on `for`-loops.
4. Use a `for`-loop to sum the numbers from `1` to `n`.
See lesson 5.1.3 for an example.
5. Use a `for`-loop, an `if`-statement and a variable to count the number of times that the character `ch` occurs in the message (`msg`).
See lesson 6.1.3 for information on iterating strings and lesson 3.3.3 for an example of using `if`-statements to compare characters.
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.
```*** Loop Worksheet! ***
Enter a string message: Hello World!
Enter a single character: o
Enter an integer between 1 and 20: 3

*** Printing 3 followed by a space 3 times ***
#1: 3 3 3
That is 3, 3's

*** Printing a row of 3 x's ***
#2: xxx
That's a high bar!

*** #3: Printing the string message 3 times ***
Hello World!
Hello World!
Hello World!
Yay! We finished!

*** Summing the numbers from 1 to 3 ***
#4: Summing it up (6): 6

*** Counting the number of times 'o' occurs in the message ***
#5: Occurences: 2
```

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.

6. After displaying the output, exit the program.
7. Submit the source code file `loopwork.cpp` with the rest of the assignment as described in Deliverables.
Hints:
• To count characters, use a second variable to store the count of characters.

Vote!

Elections are a formal group-decision making process by which a population chooses a person to hold office, such as the mayor of a city. Another use of elections is to accept or reject a political proposition. [1]

Most election results are tallied, or counted, using electronic voting. Most electronic voting machines use a computer to take care of the chore of casting and counting votes [2]. In this assignment we will develop a program to count votes.

##### Project Specifications
1. Write a C++ program that tallies, or counts, votes. The user enters votes based on a simple menu that presents the candidates. The user then inputs a single character to choose their candidate from the menu. Entering an 'X' (without the quotes) will exit the tallying operation and present a summary of the votes.
2. Name the source code file `vote.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. Present a list of candidates to the user and allow them to enter a single character as input, and no other input, as shown in the Example Run. Feel free to chose any candidate names for the voters to select.
4. Use an indefinite loop to allow multiple users to vote on your voting machine.
5. After all the votes are entered and the user enters an 'X', exit the loop and report the vote count by candidate names and neatly aligned as shown in the Example Run.
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 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.
```*** Voting Machine App ***

Candidate A: Dali Lama
Candidate B: Bernie Sanders
Candidate C: Ed Parrish
You chose candidate: Dali Lama

Candidate A: Dali Lama
Candidate B: Bernie Sanders
Candidate C: Ed Parrish
You chose candidate: Bernie Sanders

Candidate A: Dali Lama
Candidate B: Bernie Sanders
Candidate C: Ed Parrish
You chose candidate: Ed Parrish

Candidate A: Dali Lama
Candidate B: Bernie Sanders
Candidate C: Ed Parrish
You chose candidate: Bernie Sanders

Candidate A: Dali Lama
Candidate B: Bernie Sanders
Candidate C: Ed Parrish

*** Election Results ***
Dali Lama       1
Bernie Sanders  2
Ed Parrish      1
```

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.

7. After displaying the Election Results, as shown in the Example Run, exit the program. Display the title of Election Results exactly.
8. Submit the source code file `vote.cpp` with the rest of the assignment as described in Deliverables.
Hints:
• Define three (3) candidate names like:
```string nameA = "Dali Lama", nameB = "Bernie Sanders", nameC = "Ed Parrish";
```

Use any candidate names you want for the election. There are no write-in candidates.

• Review lesson 5.3.3 on how to process a sequence of inputs until a sentinel value is encountered. The sentinel value in this program is 'X' (without the quotes).
1. Election (political science): Encyclopedia Britanica Online. Retrieved March 3, 2018
2. Electronic voting: Wikipedia. Retrieved March 3, 2018

#### Project 3: Real or Fake News Detector

Fake, or hoax, news websites deliberately publish made-up stories as news, using social media to drive web traffic and amplify their effect [1]. This is a form of dishonesty known as a scam or swindle, and has been a boon for people trying to make a quick profit. Profit for fake news sites is primarily driven by online advertising revenue made mostly through Facebook and Google [2]. Both Google and Facebook have announced measures to ban web sites that show illegal or misleading content [3][4][5]. Do NOT confuse fake news with opinionated analysis from sites made on the basis of their ideologies.

For this project you are to write a program to help identify fake (made up) news. The instructor is providing you with five (5) "news" samples from the internet for testing your program. Two (2) of the samples are fake news articles and three (3) are real news articles. You must decide which are fake and which are real and write your program to identify them correctly.

To prepare for the decision, try playing the game Factitious.

##### Project Specifications
1. Write a program that scans a "news" article and decides if it is real or fake. The instructor will use the following articles for testing:
2. The name of the source code file for this program must be `realfake.cpp` and all your code must be in this file.

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

3. Your program must run from the command line using redirection of input as follows:
```./realfake < input.txt
```

Where input.txt is the news article file to test. See lesson 6.1.5: Processing Text Input and Exercise 6.1: Finding Words for examples.

4. Your program must print out a single word "Real" or "Fake" (without the quote marks) when run, depending on the article scanned, like this:
```./realfake < input.txt
Fake
```
5. Your program must correctly output the word "Fake" on two (2) articles and "Real" on three (3) articles based on the contents of the articles.
6. After printing the appropriate word, exit the program. Do NOT loop and run again.
7. Submit this project with the rest of the assignment as described in Deliverables.
Hint:
• Review lesson 6.1.5: Processing Text Input and choose a way to process text for your approach. Most students find comparing "words" easier like we did in Exercise 6.1: Finding Words.
• Remember that "words" are any set of characters separated by spaces. Thus a word might include parenthesis like "(hello)".
• Your final program should have a single `while`-loop and several `if`-statements inside the loop.
##### Resources

Here are some resources to help you get started solving the problem:

These sources may or may not correctly list fake news sites. You must research and decide for yourself.

## 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. After presenting the election results, display the name of the winner. The winner is the candidate with the most votes. Use this wording, substituting the winner's name for <winners name>, and assume no candidate is tied. (2 points)
```The winner is <winners name>!
```
3. Display `fake` and `real` for the actual fake and real news sites. (1 point)
4. Play the extra credit game: Lightbot 1. (2 points)
1. Requires flash installed and may not work on many browsers.
2. Complete the game through level 9
3. Record the message shown at the beginning of level 10 in your `README.txt` under Extra Credit.
4. Please do not cheat. If you get stuck, just explain how far you got in the README.txt and record the message for the level you completed.
5. If you go beyond level 9, make certain to still record the message for the beginning of level 10.

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

## Tutorial Lab

In preparation for next weeks lessons, complete the following:

2. Complete the Tutorial Exercises in CodeLab 6 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.

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

#### Programming 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

#### Programming Projects 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 and Other Tutorial 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

Submit your assignment to Canvas, in the assignment folder A6-Loopy Programs, following the instructions for submitting homework. Include the following items for grading:

1. `README.txt` file with option extra credit information
2. All the exercise files from Lesson 5 and Lesson 6 including:
• `forloop.cpp`
• `countdown.cpp`
• `scores.cpp`
• `findword.cpp`
3. `loopwork.cpp`
4. `vote.cpp`
5. `realfake.cpp`

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: April 09 2018 @02:30:33