Project 1: Function Worksheet

Functions are an important part of programming, allowing us to break up long sequences of code into shorter reusable parts. We then assemble the parts to create larger programs.

In this project we complete several functions. Each function is like a smaller program inside of a our larger program. Notice that we can focus on each function separately, allowing our full attention on each part of the problem.

Project 2: Counting Characters

A personal name, or full name, refers to the set of names by which an individual is known and that can be recited as a word-group, with the understanding that, taken together, they all relate to that one individual [1].

For this project, you will ask the user to enter a word or phrase and then count the numbers of letters in the input that match the unique letters of your name, excluding spaces. If you have two or more letters in you name that are the same, only test for one of the letters. For example, if your name is "Ed Parrish" and the user enters "Egg Roll" the program will report a count of two (2) letters.

When pair programming, you may still work together with your partner to complete this program. However, each student will turn in a program that only counts the characters of their name. Thus the programs will vary in the part that detects the letters of their name.

Project Specifications

Write a program that prints the number of characters that match your personal name (full name that matches grade feedback in Canvas) from a word or phrase entered by a user. If you are pair-programming then each partner must turn in files that match their name only.

You must name the source code file charcount.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.

Ask the user to enter a word or phrase, and no other input, as shown in the Example Run. Ask for all user input in the main() function and exit the program when the user enters a single letter "x" (without the quotes).

Write a function with the following signature exactly:

/**
Counts the number of letters in str that match the unique letters
(not spaces) of my name. However, the input string may have duplicate
letters that are counted.
@param str a string with a word or phrase
@return the number of letters.
*/
int countLetters(string str)

When called, the function counts the number of letters in str that match unique characters in your name, both upper and lower case, and returns that count. For example, John has four unique characters in his name while Jojo has two unique characters. Thus entering "JJ" would return 2 characters for John and 2 for Jojo. Do NOT change the function's name, parameter type, number of parameters, or return type. Instead, pay attention to the intent of these items to guide your code development. Do not use tolower(), toupper() or other techniques we have not covered.

After calling the function, display the returned count of characters in the main() function.

Add a while or do-while statement in main() that allows the user to repeat the program until the user enters a single "x" (without the quotes).

Do NOT forget to include a getline() statement inside the loop.

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. Your program must count the characters for the set of names by which you are known to the instructor (like in feedback on previous assignments) and include your first and last names at a minimum.

Enter a word or phrase (x to exit): Egg Roll
The input contains 2 letters of your name.
Enter a word or phrase (x to exit): eeee
The input contains 4 letters of your name.
Enter a word or phrase (x to exit): Ed Parrish
The input contains 9 letters of your name.
Enter a word or phrase (x to exit): The quick brown fox jumps over the lazy dog
The input contains 12 letters of your name.
Enter a word or phrase (x to exit): 12345
The input contains 0 letters of your name.
Enter a word or phrase (x to exit): x
You have a name we can count on!

In the above example run, the programmer's name is "Ed Parrish". Notice that the count is based on what the user entered and ignores duplicate characters in the programmer's name. The user entered the phrase shown in italics (for emphasis) to produce the output. Your program does NOT print the characters in italics, nor does the user input appear in italics.

Submit this project with the rest of the assignment as described in Deliverables.

Hints:

Iterate through each character of the input string with a for-loop like we discussed in lesson 6.2.3: Iterating Strings.

Inside the loop, use if-statements to test for each character of your name.

To convert a character between upper and lower case, remember how the ASCII table is organized. The character 'A' is 65 and 'a' is 97. The difference between the two is 32. As an example, if a character is less than 'a' you can convert it to lower case by adding 32 (optional).

References and More Information

Project 3: Buying the Most Toys

We know a young child who likes toys. We want to buy the child the most toys we can to make the biggest impression! However, we only have a certain amount of money we can spend. We are looking at a list of toy prices and we want to maximize the number of toys that we can buy without going over your budget.

To help us make the decision, we write a C++ program that tells us how many toys we can buy on the list with the amount of money we can spend.

Project Specifications

Write a program that tells us the most individual toys we can buy from a list of prices, without duplicating any toy and without going over budget.

Name the source code file toys.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.

Ask the user for the amount of money to spend, and no other input, as shown in the Example Run.

Define a constant array with the following prices in your program.

1.99, 2.49, 3.98, 4.99, 5.49, 6.98, 7.99, 10.75

Make sure to add the prices to the array in the same order as given above.

Define a function to count the number of toys we can buy for a given amount of money. Call the toy-counting function from the main() function.

Add a while or do-while loop to main() that asks for the amount of money to spend indefinitely until the users enters an amount of money less than or equal to zero.

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.

** Toy Maximizer! **
Enter the amount of money to spend: 20
Number of toys: 5
Money remaining: 1.06
Enter the amount of money to spend: 30
Number of toys: 6
Money remaining: 4.08
Enter the amount of money to spend: 0
You're the best!

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

Submit the source code file toys.cpp with the rest of the assignment as described in Deliverables.

Hints:

Print the number of toys inside the toy-counting function (spec 5). We may use cout in any function.

To avoid magic numbers with the array of prices, put const in front of the array.

Tutorial Lab

In preparation for next weeks lessons, complete the following:

Read the assigned reading in the textbook

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

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.