Awk Quiz Comments, Foundations of Computing

Revised March 29, use your browsers Reload or Refresh button to get the latest version.


Here is the problem statement and a solution

Scoring

I awarded up to six points for each solution, one point each for:

Errors

A correct solution must compute the grand total of all the elements before it can compute the percent contribution of any element. In my solution, I compute the total as I read in the data, before the END section. Another correct solution uses two for loops in the END section: the first computes the total, and the second computes and prints out the percent contributions.

What does not work is to compute the total and the percent contribution in the same loop in the END section. Quite a few students submitted incorrect solutions something like this:


       ...

       # WRONG - computes total in same loop as percent

       for (i=1; i<=NR; i=i+1) {

         total = total + num[i]

         print id[i] " " num[i] ", " 100.0*num[i]/total "%"

       }  

       ...                  

This is incorrect because it computes the percent from the running total, not the grand total. If you test it you will see it is clearly wrong -- most obviously, it always says that the first element contributes one hundred percent.