A8-Multi-Function Programs

Table of Contents


Objectives

  • Use function declarations (prototypes).
  • Code void functions.
  • Design a program using multiple functions as modules.
  • Develop functions as black boxes.
  • Add block comments to function declarations.

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 look at another student's code until you complete and submit this assignment, except for code you develop code 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 8.
  2. Complete the Review Exercises in CodeLab 8. These exercises will help prepare you for the problem-solving programs and should be completed first.
  3. Read lesson 8.1.4: Arrays as Function Parameters.

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. Avoid duplicating code (see textbook page 208)
  2. Function naming conventions (See: Function Names)
  3. Indentation in functions and placement of curly braces (See: Indentation)
  4. No magic numbers. (Hint: make arrays of numbers const)
  5. Indentation in while statements and placement of curly braces
  6. 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).

  7. Meaningful variable names and consistent naming style (caps vs. underbars).
  8. Create the README.txt file following the instructions.

Image

Project 1: Function Worksheet Sequel

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 Specifications
  1. Start by downloading the worksheet: funwork2.cpp.

    Keep the same filename and add to the existing code to complete the project. Leave the existing code unchanged, except for comments as instructed.

  2. Add your name and the date to the file comment block at the top of the file where shown in the comments.
  3. No user input is required for this project and do not add any.
  4. Write the required functions as described by the function signature and comment block.

    Do NOT change any of the function signatures.

  5. Write these functions below main(), and their prototypes above main().
  6. Do not code cout in any function besides printArray() and the cout statements already coded in main().
  7. Compile and run the code when finished with each function to verify correctness. Check the test results and make any alterations to your function definitions as necessary. Do NOT change any code in main() beyond removing comments to call the functions and do not change the function headers.

    When you first start you will see warnings when compiling. Remember that code with warnings compiles but the warning is giving you hints about problems with the code.

  8. Example Run: The outputs of the program must look exactly like the following for full credit, including the same order and format of the output. There is no user input.
    ***Testing icyHot***
    icyHot1 should return true: true
    icyHot2 should return true: true
    icyHot3 should return false: false
    
    ***Testing isNumber***
    isNumber1 should return false: false
    isNumber2 should return true: true
    
    ***Testing capitalizeString***
    capitalize1 should return I LOVE COOKIES!: I LOVE COOKIES!
    capitalize2 should return 124 EAST 9TH ST: 124 EAST 9TH ST
    
    ***Testing plusOut***
    plusOut1 should return +++ East +th Ave: +++ East +th Ave
    plusOut2 should return Agent +++: Agent +++
    plusOut3 should return Phone (+++) +++-++++: Phone (+++) +++-++++
    
    ***Testing printArray***
    printArray1 should cout 1 2 3: 1 2 3
    printArray2 should cout 10 30 50 79 10: 10 30 50 79 10
    printArray3 should cout 5: 3
    
    ***Testing equalFirstLast***
    equalFirstLast1 should return false: false
    equalFirstLast2 should return true: true
    equalFirstLast3 should return true: true
    
    ***Testing squareArray***
    squareArray1 should return 1 4 9: 1 4 9
    squareArray2 should return 9 25 36 64 81: 9 25 36 64 81
    squareArray3 should return 9: 9
    
    *** End of Tests ***
    
  9. When all of the tests pass, upload your completed source code file with the rest of the assignment as described in Deliverables.
Hints:
  • Remember that a warning is not a failure to compile but must be corrected before submitting the project.
  • For an example of printing the elements of an array, see lesson 8.1.4: Arrays as Function Parameters.

Image
Image source

Project 2: Restaurant Math

Meals in a restaurant include the cost of purchasing the food, sales tax and tip (gratuity). Before the 20th century, Americans viewed tipping as inconsistent with the values of an egalitarian, democratic society. After prohibition, restaurants encouraged tipping to make up for losses due to the lack of alcohol sales and the opposition to tipping gradually faded [1]. Tipping is now an American institution such that a 15% gratuity is now customary when a waiter provides adequate service at a restaurant with table service [2].

Project Specifications
  1. Write a program to help you calculate your bill at a restaurant.
  2. Name the source code file mealfun.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. Price of the meal
    2. Percent tip
  4. Assume the user may enter invalid data such as letters when numbers are wanted. Thus your program must detect and recover from invalid data entry, allowing the user to continue with the program after correcting the entry error.
  5. For the calculations, use a tax rate of 9.25%.
  6. Display the dollars and cents using two decimal places for the cents as shown in the Example Run. Remember the "magic formula" for decimal formatting.
  7. Define between three and five functions besides the main() function. At least one function must be called from another function besides main(). Use Stepwise Refinement to organize your code into functions.
  8. Define these functions below main(), and their declarations (prototypes) above main().
  9. 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 format of the output. Numbers must be on the same line as the tag explaining the number. For the input shown you must get the same numerical output. However, the output must change properly if the inputs are different.
    Welcome to the Restaurant Meal Calculator
    Enter the price of your meal: twenty-five dollars and 99 cents
    Error: enter numbers only!
    Enter the price of your meal: 25.99
    Enter the percent tip (10 min): fifteen
    Error: enter numbers only!
    Enter the percent tip (10 min): 5
    Must enter at least 10!
    Enter the percent tip (10 min): 15
    
    Food cost:   25.99
    Sales Tax:    2.40
    Gratuity:     3.90
    Total cost:  32.29
    Goodbye.
    
    Welcome to the Restaurant Meal Calculator
    Enter the price of your meal: 42.24
    Enter the percent tip (10 min): twelve
    Error: enter numbers only!
    Enter the percent tip (10 min): 2
    Must enter at least 10!
    Enter the percent tip (10 min): 12
    
    Food cost:   42.24
    Sales Tax:    3.91
    Gratuity:     5.07
    Total cost:  51.22
    Goodbye.
    

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

  10. After displaying the output, exit the program.
  11. Provide a block comment above each function prototype that describes the function using the format we discussed in class.
  12. Submit the source code file mealfun.cpp with the rest of the assignment as described in Deliverables.
Hints:
References and More Information
  1. Segrave, Kerry (1998). Tipping: An American social history of gratuities. Jefferson, North Carolina: McFarland & Company. ISBN 0786403470 (Google preview).
  2. Gratuity - United States - History: Wikipedia, retrieved 2017-01-03.
  3. Publication 115, Tips, Gratuities, and Service Charges: California State Board of Equalization (tax administration)

Extra Credit and Extensions

The following are optional and worth extra credit points if the main program works well:

  1. Complete the programming project using pair programming. (2 points)
  2. Create the correct spacing for the output of mealfun.cpp between the tag and the money using the setw() formatting manipulator described on pages 49-51 (1/e: 53-55) of the textbook or here. For example: (1 point)
    cout << "Meal cost:" << setw(7) << price << endl;
    

    To use this manipulator, include the iomanip library. For credit, the mealfun.cpp program must compile and function well enough to produce correct output.

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

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 8 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

Project Functionality (x2)

  • 7: Demonstrates mastery of the assignment.
    • Applies concepts from the lessons appropriately
    • Meets all specifications with particularly elegant solutions
    • Runs to completion with no abnormal error conditions
    • Generates correctly formatted output given correct input
    • Behaves in a reasonable way in response to incorrect data
  • 6: Has all the functionality expected of the assignment with mostly correct formatting.
    • Demonstrates many techniques from the lesson
    • Attempts to meet all specifications
    • Generates correctly formatted output given correct input
    • May have one minor formatting error
  • 5: Has most of the functionality and format expected of the assignment
    • Demonstrates many techniques from the lesson
    • Attempts to meet all specifications
    • Implementation seems more complicated than necessary.
    • May have one or more formatting errors in output
    • May have one minor functional error
  • 4: Has most of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Attempts to meet all but one of the specifications
    • Implementation seems excessively complicated.
    • May have many formatting errors in output
    • May have 2-3 minor functional errors
  • 3: Has some of the functionality expected of the assignment
    • Attempts to meet at least 1/2 of the specifications
    • Demonstrates many techniques from the lessons
    • Implementation seems excessively complicated.
    • May have more than 3 minor functional errors
    • Source code compiles with no errors or warnings
  • 2: Serious functional problems but shows significant effort and understanding
    • Attempts to meet less than 1/2 of the of the specifications
    • Has a major error or many minor errors
    • Implementation seems very convoluted
    • Demonstrates few techniques from the lessons
    • Source code compiles but may have multiple warnings
  • 1: Serious functional problems but shows some effort and understanding.
    • Does not compile but shows an attempt to meet most specifications
    • Implementation seems somewhat complete but has errors
    • Demonstrates some techniques from the lessons
  • 0: Minimal to no work apparent or made in an incorrect way.
    • Not turned in
    • Does not compile with little work apparent
    • Attempts to meet less than 1/2 of the of the specifications
    • Uses techniques not covered in the course so far

Program Style

  • 4: Code is well-documented including:
    • Correct file name used
    • Name, date, and program description in file comment block
    • Follows specified format for file comment block
    • Has a function comment block for all function declarations following the specified format
    • Proper use of spaces around operators
    • No tab characters are present in the source code
    • As described in How To Document and Organize C++ Code
    • Correct file name used
  • 3: Code has a minor documentation error
  • 2: Code has some documentation errors
  • 1: Code has many documentation errors
  • 0: No apparent attempt to follow documentation standards or write documentation comments

CodeLab Exercises

Number 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 A8-Multi-Function Programs, following the instructions for submitting homework. Include the following items for grading:

  1. README.txt file
  2. All the exercise files from Lesson 8
  3. funwork2.cpp
  4. mealfun.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 13 2018 @01:09:00