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 work on the code together at the same computer.
  • You may not look at another student's code until you complete and submit this assignment, except for code you work on 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. Instead, ask them to show you other examples that you then apply to the project.
  • 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 comment blocks (See: Function Comment Block)
  3. Function naming conventions (See: Function Names)
  4. Indentation in functions and placement of curly braces (See: Indentation)
  5. No magic numbers. (Hint: make arrays of numbers const)
  6. Indentation in while statements and placement of curly braces
  7. 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).

  8. Meaningful variable names and consistent naming style (caps vs. underbars).
  9. 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, replacing the words Your Name with your first and last name and Date Here with the current date.
  3. No user input is required for this project and do not add any.
  4. Write the required functions, one per problem, as described by the function signature and comment block.

    Do NOT change any of the function headers and do NOT make any changes to the main() function besides uncommenting code.

  5. Write the function definitions below main(), and their prototypes above main().
  6. Do not turn in code with cout in any function besides printArray() and the cout statements already coded in main().

    You may add cout to debug code but please remove it before turning in your code.

  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.
    ***Testing toUpperCase***
    toUpperCase1 must change a to (A): A
    toUpperCase2 must change z to (Z): Z
    toUpperCase3 must change X to (X): X
    
    ***Testing addDash***
    addDash0 must return (-ab): -ab
    addDash1 must return (a-b): a-b
    addDash2 must return (ab-): ab-
    addDash3 must return (ab): ab
    
    ***Testing matchCounter***
    matchCounter1 must return (3): 3
    matchCounter2 must return (2): 2
    matchCounter3 must return (1): 1
    
    ***Testing printArray***
    printArray1 (1=a b c d)
    real output: 1=a b c d
    printArray2 (2=x b c d e)
    real output: 2=x b c d e
    printArray3 (3=x y w d 1 2)
    real output: 3=x y w d 1 2
    
    ***Testing evenAdd1***
    evenAdd1a (ea1=b b d d): ea1=b b d d
    evenAdd1b (ea2=y b d d f): ea2=y b d d f
    evenAdd1c (ea3=y y x d 2 2): ea3=y y x d 2 2
    
    *** End of Tests ***
    

    The actual output of calling printArray() appears on the line under the expected output. This placement allows easy verification of the correct output, including the correct spacing and equal sign. Also note that the output of evenAdd1() is produced in main() by calling printArray()--an example of function reuse!

  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: Sequence Alignment

Scientists measure the evolutionary closeness of species by looking at DNA sequences for key proteins and measuring their similarities and dissimilarities. Since DNA changes occur over time, if two sequences of DNA are essentially the same then scientists consider the two species evolutionarily closer. Scientists call this comparison process sequence alignment [1].

Consider the two strings of DNA below (completely made up) and notice the matches in black and mismatches in red:
Species 1: AATAACGAAA
Species 2: AAAACGAAAA

We can change the alignment by assuming that an insertion or deletion, called an indel for short, of one of the bases has occurred. As an example, we improve the alignment with the two changes shown below in red:

Species 1: AATAACGAAA-
Species 2: AA-AACGAAAA

The alignment greatly improved in the above with the addition of two indels, marked as two dashes (-). A researcher would assume that two changes happened, one change in each species.

While automated algorithms exist to do sequence alignment, sometimes a researcher wants or needs to do an alignment by hand. We provide the tools for manual sequence alignment in this project.

This assignment adapted from Bill Punch and Richard Enbody at Michigan State University.

Project Specifications
  1. Write a program that provides the tools for manual sequence alignment on two strings.
  2. Name the source code file dnaseq.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. Strings: First prompt the user for two strings. For example:
    Enter string 1: abc
    Enter string 2: ac
    

    In the above example, and those that follow, the user entered the values shown in aqua italics (for emphasis). Your program does NOT print the characters in aqua italics, nor does the user input appear in aqua italics.

    The strings may contain any letters, but to be "biological" they should consist of: 'A', 'T', 'C', 'G'. The strings may be of different lengths and you may assume they will not contain spaces.

  4. Menu: Next prompt the user with a menu to manage the sequence alignment that is displayed after each option completes and looks like the following:
    Select one of the following:
    a. add indel
    d. delete indel
    s. score current alignment
    q. quit
    Choice: _
    
    The user enters the letter of the operation on the line labeled "Choice" where the underbar "_" is shown. The underbar "_" is shown for information only and is not part of the program display. Entering a q exits the program and that is the only way to exit the program. The other options are described below. Do NOT change the letter of a menu operation.

    Display an error message if the user enters something other than one of the four menu letters. For example:

    Error: invalid menu choice!
    
  5. Add indel: selected by menu letter a. When adding an indel, prompt the user for two pieces of information in this order:
    1. which string to change (1 or 2)
    2. the index (starting at 0) to insert the indel. Placement is before the given index. Display an error if the index is out of range and make no changes to the string.
    For example:
    Work on which string (1 or 2): 3
    Error, must enter 1 or 2
    Work on which string (1 or 2): 2
    Add before which index: 1
    

    With the information, modify the string by inserting a dash (-) at the index location, which will make the string longer. Do not replace the character with a dash.

  6. Delete indel: selected by menu letter d. If you can add an indel, you should be able to delete it if it doesn't do what you want. Again, prompt for two pieces of information:
    1. which string to change (1 or 2)
    2. the index (starting at 0) to delete the indel.
    For example:
    Work on which string (1 or 2): 2
    Delete which index: 1
    

    Delete the indel at the index location with the information. If the character to delete is not an indel, display the word Error and a message; do not make changes to the string. For example on the string a-c:

    Delete which index: 0
    
    Error: index 0 is 'a' but must be an indel '-'.
    String: a-c
    
    Delete which index: 1
    
  7. Score current alignment: selected by menu letter s. Report the current number of matches and the number of mismatches using the following rules:
    1. If one string is shorter than the other, fill out the shorter string with indels (-).
    2. For each index of the two strings, if the letters are the same count a match; otherwise count a mismatch
    3. Any indel is automatically a mismatch.
    After showing the matches and mismatches, print both strings, each on its own line, as follows:
    1. First print a label, Str1 for the first string and Str2 for the second string, followed by the current value of the string.
    2. Print matching characters in lower case. If the user entered upper case letters, convert them to lower case on a match.
    3. Print all mismatches in upper case.
    4. Print indels as dashes.
    For example:
    Matches: 1, Mismatches: 2
    Str1: aBC
    Str2: aC-
    
    Hint: Printing a string is best done by creating a new local string and adding characters (upper, lower or dashes) to it and then printing the new string. Scoring does not change the input strings. Only add indel and delete indel operations change the input strings.
  8. When designing the program, define between three and eight functions in addition to main(). Call all the functions at least once to perform an action.
  9. Define the functions after main() and their declarations (prototypes) before main().

    Do not forget to add block comments before each function declaration (prototype).

  10. 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.
    DNA and Protein Sequence Alignment
    Enter string 1: abc
    Enter string 2: ac
    
    Select one of the following:
    a. add indel
    d. delete indel
    s. score alignment
    q. quit
    Choice: x
    
    Error: invalid menu choice!
    
    Select one of the following:
    a. add indel
    d. delete indel
    s. score alignment
    q. quit
    Choice: s
    
    Matches: 1, Mismatches: 2
    Str1: aBC
    Str2: aC-
    
    Select one of the following:
    a. add indel
    d. delete indel
    s. score alignment
    q. quit
    Choice: a
    Work on which string (1 or 2): 2
    Add before which index: 1
    
    Select one of the following:
    a. add indel
    d. delete indel
    s. score alignment
    q. quit
    Choice: s
    
    Matches: 2, Mismatches: 1
    Str1: aBc
    Str2: a-c
    
    Select one of the following:
    a. add indel
    d. delete indel
    s. score alignment
    q. quit
    Choice: d
    Work on which string (1 or 2): 3
    Error, must enter 1 or 2
    Work on which string (1 or 2): 2
    Delete which index: 1
    
    Select one of the following:
    a. add indel
    d. delete indel
    s. score alignment
    q. quit
    Choice: s
    
    Matches: 1, Mismatches: 2
    Str1: aBC
    Str2: aC-
    
    Select one of the following:
    a. add indel
    d. delete indel
    s. score alignment
    q. quit
    Choice: q
    Goodbye!
    

    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.

  11. Submit the source code file dnaseq.cpp with the rest of the assignment as described in Deliverables.
Hints:
  • First work out the top level algorithm and program structure.
  • Next, get the command loop working.
  • Then get individual commands to work (scoring is the most work, save it till last).
  • Add code to detect errors and allow the user to reenter correct information.
  • Realize that scoring does not change the input strings. Only add indel and delete indel operations change the input strings.
References and More Information
  1. Sequence alignment: Wikipedia article.
  2. DNA: background information on DNA

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 your own interesting function problem for the function worksheet that modifies arrays or strings. (1 point for modifying arrays or strings, 1 point for robust test cases, and 1 point for interest and creativity)
    1. Submit the extra function and its main() function in a file named xcfunwork2.cpp.
    2. Do not have any user input in the extra credit file.
    3. Extra function must have a string or array parameter to operate upon.
    4. Label the tests in main() with a cout statement with the words "Testing Extra Credit" followed by the function name, like:
      ***Testing Extra Credit myFabFun***
      
    5. Following the label, include at least 3 test cases calling the extra function in main(), like the test cases in the standard project, each case reported on seperate lines.
    6. Credit the source of the extra function, even if yourself, in the source code file. State the author and, if not original, the availability using the tag "@source". For example:
      @author: Ed Parrish adapted from cplusplus example
      @source: http://www.cplusplus.com/forum/beginner/84104/
      
  3. Add an error check if indexes entered are out of range for the string when adding or deleting indels and allow the user to reenter a correct index number before continuing. For example: (1 point)
    DNA and Protein Sequence Alignment
    Enter string 1: abc
    Enter string 2: ac
    
    Select one of the following:
    a. add indel
    d. delete indel
    s. score current alignment
    q. quit
    Choice: a
    Work on which string (1 or 2): 2
    Add before which index: -1
    Error: index must be 0 to 2.
    Add before which index: 3
    Error: index must be 0 to 2.
    Add before which index: 2
    
    Select one of the following:
    a. add indel
    d. delete indel
    s. score current alignment
    q. quit
    Choice: s
    
    Matches: 1, Mismatches: 2
    Str1: aBC
    Str2: aC-
    
    The outputs of the program must look like the above for full credit, including the same wording of the error messages.

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

Function Worksheet

  • 5: All functions completed and program generates the correct output without error
  • 4: All functions completed but has a minor error
  • 3: Most functions completed or has some small errors
  • 2: At least half the functions completed or has some errors
  • 1: Few functions completed or has many errors
  • 0: Does not compile or wrong file turned in

DNA Sequence Alignment Project

  • 10: Demonstrates mastery of the assignment
    • Applies concepts from the lessons appropriately
    • Meets all specifications (see above) with particularly elegant solutions
    • Runs to completion with no abnormal error conditions
    • Generates correct output given correct input
    • Behaves in a reasonable way in response to incorrect data
  • 8: Has all the functionality expected of the assignment
    • Demonstrates many techniques from the lesson
    • Attempts to meet all specifications (see above)
    • Implementation seems more complicated than necessary.
    • May have one minor error
  • 6: Has most of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Attempts to meet all but one of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have 2-3 minor errors
  • 4: Has some of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Attempts to meet at least 1/2 of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have more than 3 minor errors
    • AI not correctly trained
  • 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: Does not execute, not turned in or uses techniques not covered in course

Program Style

  • 3: 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
  • 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 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. dnaseq.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: October 23 2019 @21:11:35