Thursday, June 07, 2007

Too Clever By 'Alf, My Old Son

Working thru the book "Accelerated C++" by Koenig and Moo, kindly lent to me by a friend, I came across this seemingly intractable problem:

Write a program that will keep track of grades for several students at once. The program could keep two vectors in sync: The first should hold the student's names, and the second the final grades that can be computed as input is read. For now, you should assume a fixed number of homework grades. We'll see in $4.1.3/56 how to handle a variable number of grades intermixed with student names.

Now, since the "grade" is computed by taking the sum of a ratio of homework to finals to midterm exam grades, I had been thinking I should store each in a vector, which (contrary to the probspec) would necessitate not two but five vectors.

Turns out the sentence you should assume a fixed number of homework grades is maximally important, so don't go storing all the grades in a vector until you've computed the total grade.

Call it an object lesson in not being too clever by 'alf.

No comments: