CS285 -- Lab 3: Generic Algorithms



->Courses
->CS285
-->Objectives
-->Homework
-->Quiz 1
-->Lab 1
-->Lab 2
->Lab 3
-->Lab 4
-->Lab 5
-->Lab 6
->Electronic Submission
->Old Exams
->C++ Examples
->MSVC Info
->STL Help
->Book Errata
->Software
->Tentative Schedule
->Course Policies

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

Winter 2001-2002

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() on the string class. 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 boolean functions:
    • isVowel() -- returns true if the character is a vowel (assume y is a vowel).
    • isConsonant() -- returns true if the character is a consonant.
    • isSpace() -- returns true if the character is whitespace (space, tab, carriage return, newline, etc.).
    • isMath() -- returns true if the character is one of the following: + - / * ^ = < > ( ).
    • isPunctuation() -- returns true if the character is one of the following: . , ' " ! ?.
  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 in this research paper.

In the above, you are only required to implement your generic algorithm for the string class. Although, this is not required, you may wish to make your cnt_if() truly generic by using templates in your definition of cnt_if().

You may use the cctype library, but you may not use the algorithm library.

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

Your report should include:

  • A discussion of how you approached the problem. This should contain a sufficient level of detail to convince your instructor that you were thinking as you worked on this assignment.
  • The Documented source code for your program.
  • Sample program output.
  • A brief description of any problems you encountered or questions you have regarding the lab.
  • A discussion of how you worked together as a team.
  • 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
    Note: You should be working together the entire time on this project so you need only submit one activity log. If you both spent one hour debugging your code together, your activity log should indicate one hour (not two hours) was spent debugging.
  • 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-2002 Dr. Christopher C. Taylor Office: CC-27C Phone: 277-7339 Last Updated: January 9, 2002
I am responsible for all content posted on these pages; MSOE is welcome to share these opinions but may not want to.