Awk quiz, arrays and loops, Foundations of Computing

Write the "percent" program (described below) in Awk. A solution appears at the end of this page.

You may refer to books or notes, and you may use the computer, but you must work alone.

You can turn in a handwritten program, or print out a computer file.

Be sure to turn in something -- if you get stuck, turn in your best attempt. If you get stuck on the computer, turn in something handwritten.

Write a program named percent - total and percentage calculation in Awk.

In the input, the first field in each line is an identifier (any string). The second field in each line is a non-negative number.

The program calculates the total of all the numbers and the percentage contributed by each line. The percent contribution of each line is the number on that line, divided by the total, times one hundred.

The program generates this output: It prints a blank line, then prints out a sequence of lines in the same order as the input lines, each line showing the identifier, number, and percent contribution. The beginning of each output line shows the identifier and number exactly as they appear in the input, then a comma after the number, then the percent contribution, then a percent sign. After all the data lines the program prints out another blank line, then prints out the total on the last line.

Here is a sample program run:

$ awk -f percent
Washington 3
Oregon 2
California 5

Washington 3, 30%
Oregon 2, 20%
California 5, 50%

Total: 10        

Solution

Here is a solution:

 # Calculate total as we read file, storing data in arrays
 { id[NR] = $1; num[NR] = $2; total = total + $2 }


END {  
       print ""
       for (i=1; i<=NR; i=i+1) {
         print id[i] " " num[i] ", " 100.0*num[i]/total "%"
       }
       print ""
       print "Total: " total
    }