Module 4.2 - More on Arrays
Notice in average1.c that we have 3 separate loops; one does the reading, one does the printing, and the last does the averaging.
- Can you contract these calculations into just 2 loops, one that does the data extraction, and the next that does both printing and averaging?
- One aspect of for loops that may help is that the variable initialization portion of the for clause is not limited to initializing just one variable.
- What about distilling this program into just 1 loop, which does all the reading, printing an averaging?
Think about how you might go about doing the above and discuss it with your classmates.
Next, take another look at the 2 loops in dot.c; again,
- one does the reading, and
- the next performs the computation of the dot product.
Can these also be incorporated into one loop?
Now, think about this; when does it make sense to separate computational elements into distinct iterative structures? Certainly, one scenario where it makes sense is when you're attempting to instruct students on certain computational techniques and strategies.
Are there other scenarios that have the same characteristics? What are those characteristics? A couple are:
- Where optimum performance is not a high priority
- Where understanding the strategies or tactics of the computation is a high priority.
Finally, think about how this applies to the sorting algorithms we've been discussing in the Connect and Reflect sections:
- Do the same guidelines apply? Specifically, when does it make sense to employ a non-optimal sorting algorithm?
- For sorting algorithms with approximately equal complexity, when does one make sense over another?
Continue to the Algebraic Methods Module ->