Topical Information

This lab will help you practice with parallel vectors.

Program Idea

You've been thinking about some cascading if/switch structures (aside from those bad dreams, I mean). You wonder if maybe you could make them more re-usable by changing them into loops which look through a LS|vector}. Let's give it a shot:

Given the if structure:

    if (score >= 90)
    {
        grade = 'A';
    }
    else if (score >= 80)
    {
        grade = 'B';
    }
    else if (score >= 70)
    {
        grade = 'C';
    }
    else if (score >= 60)
    {
        grade = 'D';
    }
    else
    {
        grade = 'F';
    }

You see that there are numbers being translated into letters. You envision a set of parallel vectors (short/double for the grade cutoffs and char for the grades):

            cutoffs        grades
            +----+         +---+
            | 90 |    0    | A |
            +----+         +---+
            | 80 |    1    | B |
            +----+         +---+
            | 70 |    2    | C |
            +----+         +---+
            | 60 |    3    | D |
            +----+         +---+
            |  0 |    4    | F |
            +----+         +---+

    MAX_cutoff = 5  // logical size of parallel vectors (identically
                    // cutoffs.size() and grades.size())

Now you write a simple loop:

    i = 0;
    while ((i != cutoffs.size()) && (score < cutoffs[i]))
    {
        i++;
    }
    grade = grades[i];

A little hand/desk testing shows that this seems to work for normal cases, but fails when the data is over 100% or below 0% (whether by accident or on purpose). You decide to write the input functions to avoid such non-sense as negative scores, but think maybe the over 100% wouldn't be bad if it were your score. So you alter your vectors a bit (to have the + be A+, just make the grades vector of base type string):

            cutoffs         grades
            +-----+         +---+
            | 100 |    0    | + |
            +-----+         +---+
            |  90 |    1    | A |
            +-----+         +---+
            |  80 |    2    | B |
            +-----+         +---+
            |  70 |    3    | C |
            +-----+         +---+
            |  60 |    4    | D |
            +-----+         +---+
            |   0 |    5    | F |
            +-----+         +---+

    MAX_cutoff = 6  // logical size of parallel vectors (identically
                    // cutoffs.size() and grades.size())

Checking again, everything is working except the negatives. Just in case someone by-passes your input routines (which would probably be doing domain validation to avoid negatives ...although some teachers like to use a negative score to denote a missed assignment as opposed to an actual earned zero), you decide to make it safe and change the final assignment of the grade:

            // assign 'U' for "unknown" when we went off the end
    grade = i >= grades.size() ? 'U' : grades[i];

Now everything is working fine!

Note how no changes are needed to the loop! Just the vector (and the afterwards assignment) needed to be changed to make our adjustments!

Could this possibly be made into a nice generic function that could be applied to many problems? Could such a function work by simply receiving the parallel vectors and an overrun error value as arguments? Wow! What a thought!

Thought Provoking Questions (I)

  1. Why don't we need to check both ends of the bounds for a grade? (i.e. both 80 and 90 for a B)
  2. What inputs would your generic function need? What value would it return?
  3. How generic is it, really? Would you need to overload it if you changed the above program to translate from insurance codes to deduction values (like in the payroll program options)? How many overloaded version can you see the need for? (Hint: one for each TYPE of translation.)
  4. Are the 0 and F in the last position needed? Why/Why not?
  5. Does this loop processing take any extra time over a branching structure (time to process, that is)? What operations need to be performed to find that a 62 is a D in each case? (That is, run through both the branch and the loop to check a score of 62 and count how many actions are done and what kind of action they are. Hint: an increment is considered insignificant when placed with a comparison.)

Intermission

Watch out below!!!!!

That was just the idea for the program and some examples/questions to help clarify it in your mind. It wouldn't hurt for you to type up the above code in a program for practice and to get a better handle on vector processing, but it is not what is necessary for this lab assignment...

Here is the actual assignment (although you still have to answer the TPQ(I) above for full credit):

Program Information

Write a program using the above idea but to solve the following situation:

In a certain application (details withheld to protect the company's 'privacy'), legitimate values are:

     Range of Values          Category
        0 -  5                    B
        8 - 14                    N
       20 - 21                    Q
       25 - 43                    D
       50 - 59                    N
       60 - 99                    A

The values are decimal ones -- not integers (the cutoff values just happen to be whole numbers). Any value not inside a specified range is considered invalid and should receive a category value of '-'. (Note that category 'N' has two ranges associated with it...this is NOT a mistake!)

Write a function which can receive a value, the vectors for legal value limits and associated resulting categories, and a value for use with an invalid category and return the resulting mapped category.

Test this function with a simple driver loop.

Thought Provoking Questions (II)

  1. Can you get away with just checking one end of a range as we did in the grades program above? Why/Why not?
  2. How many vectors will it take to store the value ranges?
  3. Do you need to change the values in the function's arguments? How can you ensure this won't happen by accident?
  4. How many tests will be needed to validate your function? How did you determine that without the branches there to help you count?
  5. Can you think of any other places such 'conversion' loops might come in handy? (Hint: Don't forget the link above to the payroll example...)

This assignment is (Level 4).

Options