DAY17
Wednesday, 20 June 2012
On Monday, we discusses an "impossible problem", something we cannot
program using the C language as far as we know. A person has asked us
to write a program and mail it to them. The program will allow the
person to type in any number of grades. Then the program will
compute the average of the grades, a histogram (the number of grades
in the ranges 90+, 80-90, 70-80, 60-70, below 60), and
a sorted list of the grades.
Today we will continue our discussion of how the "impossible problem"
can be solved using arrays:
-
how to read the entries of an array from a user when you don't
know how many values there are.
-
how to get the sum, average, maximum and minimum of the data in an array.
-
how to sort an array into order;
-
how to create a histogram of data in an array.
-
how to put this all together to make an "impossible program".
-
how to prepare the grades in a file beforehand, and then
"feed" the data to the program when you're ready.
-
how to make a plot of the histogram that actually looks like a histogram.
Programs and functions we might talk about:
-
array_bin.c counts the number of array
entries that fall between 0 and 10, 10 and 20, ..., 90 to 100.
-
array_max.c sets the entries of
an array to random numbers (using iran.c), then finds the maximum.
-
array_read.c shows how a program
can read elements of an array, one by one, from a user. The program
counts how many items have been entered. (The program has a maximum
number of values it can accept.)
-
array_sort.c shows how to sort the
elements of an (integer) array of 10 elements. One element is sorted. Then we
try to consider one more element, and sort them. Then we add
one more, and so on.
-
array_sort_output.txt,
the output from array_sort as it sorts the array.
-
array_sort2.c another sorting example.
This time, the array elements are random.
-
array_sum.c shows how to compute the
sum (and average) of the elements of an (integer) array.
-
gnuplot_input.txt, commands to GNUPLOT
to make the GRADES_COUNT plot.
-
grades.c uses an array to store up to 100 grades.
It computes the average, and prints a summary of the grades by letter.
-
grades.txt a file containing 53 grades, followed
by a "-1" to signal the end. The grading program can read this data, if you
use a command like ./a.out < grades.txt.
-
grades_count.txt, the output file of the
grade counts, created by the command ./a.out < grades.txt > grades_count.txt.
-
grades_count.png, a plot of the grade counts,
created by GNUPLOT.
-
iran.c returns random integers in some interval.
-
p7.3.c computes an array whose entries are Fibonacci numbers:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ... (This is an example program from the book.)
-
vector.c considers three vectors in 3D space,
and finds their lengths, dot products, pairwise cosines and angles.
Last revised on 20 June 2012.