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. Compile and run the code when finished with each function to verify correctness.

    Uncomment the function calls from main() as you progress. Do NOT change any code in main() beyond removing comments to run the code.

  7. When all of the tests pass, upload your completed source code file with the rest of the assignment as described in Deliverables.
Hints:

Date and Time

Formats

Project 2: Date and Time Displays

Dates and times are displayed differently in various parts of the world. In America, we usually write dates in the format MDY (month-day-year). However, the most popular world-wide format is DMY (day-month-year) and the second most popular format is YMD (year-month-day). A variety of separators of the components are used including: "/", ".", "-" and " " (space) [1].

Times are also written differently. In America, we use the 12 hour clock, but in most non-English speaking countries, and where unambiguity and accurate timekeeping are important, the 24-hour clock is used. For example, in America, 8:30 at night is represented as 8:30pm, while in the U.S. military, it is represented as 20:30. [2]

In this project we write void functions to display dates and times in a variety of formats.

Project Specifications
  1. Write a program that calls void functions to display dates and times entered by a user in both American and other formats as specified by the functions below.
  2. Name the source code file datetime.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 seven inputs (and no other input) in this order and each on their own line, as shown in the Example Run below:
    1. year
    2. month
    3. day
    4. date separator
    5. hour
    6. minute
    7. "morning" or "afternoon" (without the quotes)

    Assume the user enters only valid data.

  4. Write following five functions with the specified names and parameters and add them to your program:
    1. formatDateMDY

      Takes as input in order: three integer parameters, one for the year, one for the month and one for the day, and a character (char) for the date separator. Prints a formatted version of the date to the console, using the format MDY with the date separator between each component. The function returns nothing.

    2. formatDateDMY

      Takes as input in order: three integer parameters, one for the year, one for the month and one for the day, and a character (char) for the date separator. Prints a formatted version of the date to the console, using the format DMY with the date separator between each component. The function returns nothing.

    3. formatDateYMD

      Takes as input in order: three integer parameters, one for the year, one for the month and one for the day, and a character (char) for the date separator. Prints a formatted version of the date to the console, using the format YMD with the date separator between each component. The function returns nothing.

    4. formatTime12

      Takes as input in order: two integer parameters, one for the hour, one for the minutes, and a Boolean parameter that indicates either morning or afternoon. Prints a formatted version of the date to the console, using the format HH:MMam or HH:MMpm, where the hours (HH) or minutes (MM) may be either one or two digits. The function returns nothing.

    5. formatTime24

      Takes as input in order: two integer parameters, one for the hour, one for the minutes, and a Boolean parameter that indicates either morning or afternoon. Prints a formatted version of the date to the console, using the 24-hour clock format of HH:MM, where the hours (HH) or minutes (MM) may be either one or two digits. Note that there is no am or pm in this format. The function returns nothing.

  5. Do not change the names of the functions or the required parameters.
  6. Write these functions below main(), and their prototypes above main().
  7. Example Run: The input prompts and outputs of the program must look like the following for full credit, including the double quotes ("), same order of input and wording of the output for the date and time displays. For the input shown you must get the same output. However, the output must change properly if the inputs are different.
    Welcome! This program will print dates and times
    in a variety of world-wide styles!
    
    Please enter the current year: 2017
    Please enter the current month: 10
    Please enter the current day: 7
    Please enter a single character separator: -
    
    Please enter the current hour: 6
    Please enter the current minutes: 35
    Please enter whether it is "morning" or "afternoon": afternoon
    
    Date in MDY style: 10-7-2017
    Date in DMY style: 7-10-2017
    Date in YMD style: 2017-10-7
    
    Time in the 12-hour clock style: 6:35pm
    Time in the 24-hour clock style: 18:35
    
    Bye! See you another day!
    

    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.

  8. After displaying the output, exit the program.
  9. Provide a block comment above each function prototype that describes the function using the format we discussed in class.
  10. Submit the source code file datetime.cpp with the rest of the assignment as described in Deliverables.
Hints:
  • Call the formatting functions from the main() function.
References and More Information
  1. Date format by country: Wikipedia article
  2. Date and time representation by country: Wikipedia article

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. Make the following changes to your datetime.cpp file: (2 points)
    1. Write a function with the following signature:
      /**
          Displays the prompt and reads an int from cin in the range of min to max.
          @param prompt The message to print that prompts the user.
          @param min The minimum acceptable integer value.
          @param max The maximum acceptable value.
          @return A valid int in the range of min to max.
      */
      int readIntInRange(string prompt, int min, int max)
      
    2. Write this function below main(), and the prototype above main().
    3. Get the input from the user inside the readIntInRange() function.
    4. The function must recover from invalid input like "one" or a number out of range, providing the user brief explanations of the problem like this:
      Please enter the year: one
      Please enter integers only!
      Please enter the year: 10000
      Please enter integers between 0 and 3000!
      Please enter the year: 2018
      (Input accepted, on to next input function)
      

      Hint: See Exercise 6.1. Use only techniques we have covered.

    5. Call this function five times to get valid values for year, month, day, hour and minute, with the same prompts as the Example Run of project 2.

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. datetime.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: November 06 2017 @01:03:58