CS285 -- Lab 3: Generic Algorithms



->Courses
->CS285
-->Homework
-->Quiz 1
-->Lab 1
-->Lab 2
->Lab 3
-->Lab 4
-->Lab 5
->Electronic Submission
->MSVC Info
->STL Help
->Book Errata
->Tentative Schedule
->Course Policies

[Courses]
[Rich][Home][Rich]
[Author]

Winter Quarter 2000-2001

Overview

In this lab, you will be exposed to iterators and generic algorithms. This is accomplished by designing and implementing a generic algorithm the same functionality as the STL's generic algorithm count_if().

Procedures

The heavy emphasis on iterators in STL makes it possible to implement generic algorithms that can be used on multiple container types (provided that the correct type of iterator is available). These generic algorithms alleviate the need to write unique member functions for each container type. Not only does it reduce the amount of code that needs to written, debugged, and maintained, it also provides a common interface for all container classes that have the correct type of iterator required by the generic algorithm.

In this lab, you are to do the following:

  1. Design and implement a generic algorithm called cnt_if() that has the same functionality as the STL generic algorithm count_if(). You are not allowed to use count_if() in your implementation of cnt_if() (hopefully this is obvious).

    The generic algorithm count_if() counts the number of values that cause a function (that is passed as a parameter) to return true. (See the example on pp. 135-136 of the course textbook.)

    Be sure to note that the count_if() algorithm increments rather than sets the counter value. Therefore, if you wish to obtain the actual count, you should be sure that your counter has a value of zero when count_if() is called.

  2. Design and implement the following predicate functions:
    • isVowel(char) -- returns true if the character is a vowel (assume y is a vowel).
    • isConsonant(char) -- returns true if the character is a consonant.
    • isSpace(char) -- returns true if the character is whitespace (space, tab, carriage return, newline, etc.).
    • isMath(char) -- returns true if the character is one of the following: + - / * ^ = < > ( ).
    • isPunctuation(char) -- returns true if the character is one of the following: . , ' " ! ?.
    • isCapitalized(string) -- returns true if the string is capitalized.
    • isLong(string) -- returns true if the string is a word (consists of letters) and contains more than eight characters.
    • isShort(string) -- returns true if the string is a word (consists of letters) and contains fewer than four characters.
  3. Write a program that will use your implementation of the above functions to determine the number of vowels, consonants, spaces, math symbols, and punctuation and the number of capitalized words, words with more than eight characters, and words with fewer than four charaters in this research paper.
  4. Write another program that will read in the following file and use cnt_if() to count the number of positive and negative integers in this file.

Lab report (due 11:00pm, the day prior to week 6 lab)

Your report should include:

  • Your design documentation.
  • The Documented source code for your program.
    You may find it useful to use the code.bat and dump.bat batch files to produce one file containing all of your source code with the markup commands required by my electronic submission process.
  • Program results.
  • A brief description of any problems you encountered or questions you have regarding the lab.
  • A detailed discussion of how your team operated and what each team member contributed to the project.
  • A summary of your activity log indicating how much time you spent on the assignment. In addition to the total time spent on the project, please report the time in the following categories:
    • Design
    • Coding
    • Debug (before you think it's working)
    • Test (after you think it's working)
    • Documentation
    • Other
  • Any suggestions you have for how the lab could be improved.

As with any report you submit, correct spelling and grammar are required. In addition, your report should be submitted electronically following the Electronic submission guidelines. (You may wish to consult the sample report before submitting your report.) Be sure to keep copies of all your files, in case something gets lost. It may be wise to keep a diskette backup as well.

Your grade will depend on quality of design, clarity of code and documentation, as well as whether your program produces the correct results. If you have any questions, consult your instructor.

© 2001 Dr. Christopher C. Taylor Office: CC-27C Phone: 277-7339 Last Updated: January 3, 2001
I am responsible for all content posted on these pages; MSOE is welcome to share these opinions but may not want to.