Posted by hussein on August 05, 2000 at 11:23:44:
In Reply to: Self-test exercise posted by ste on August 04, 2000 at 16:16:52:
: how does the book come up with the anwers for self-test exercise #1 and 2?? These problems are similar to hw18, i think. Is "how many comparisons" equal to finding the time?
no, this is a little different from finding the time (it is similar to the self-test exercises). all you need to count are the comparisons (<, <=, >, and >=) - ignore everything else for this question
for example:
for ( int i=0; i<10; i++ )
if ((a cout << "***\n";
the loop executes 10 times and there are two comparisons within the loops as well as one that is done within the loop termination condition check, so you get a total of 30 comparisons. formally, we can say that
total = sum(from 0 to 9) (2+1) = 10.3 = 30
in actual fact, when the loop terminates, there is one comparison that must fail and that gives you 31 comparisons ...
the self-test is different because in that case you only count array element comparisons - here you count all comparisons.