This lab will help you practice with parallel vectors.
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!
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):
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.
This assignment is (Level 4).