A Simple Example
Task:
Write a program to average a set of integers.
Implement the program by reading the data into an array (at most 100
items), echoing them 3 items per line, and printing the average.
Algorithm for main()
get the data into an array,
(keeping the count of data entered)
find the average of the data
print the data, 3 items per line
print the average
We will implement these steps as function calls.
(Note, we define a macro for the maximum size of the array).
#define SIZE 100
main()
{ int data[SIZE]; /* the data array */
int count; /* the number of data items in the array */
float average; /* the average value */
/* get the data into an array,
keeping the count of data entered */
count = get_data(data,SIZE);
/* find the average of the data */
average = avg_data(data, count);
/* print the data, 3 items per line */
print_data(data, count);
/* print the average */
printf("\nthe average is %f\n",average);
}
How does this program
run?
We used a similar technique in the
Shell Game.
to Overview.