/* hints2.c -- Read a sequence of positive integers and print them * out together with their sum. Use a Sentinel value * (say 0) to determine when the sequence has terminated. * You can compile the program with the command * % cc hints2.c adder.c * and run it with the command * % a.out */ #include #include "adder.h" #define SENTINEL 0 int main(void) { int current; /* The number just read */ startup(); /* Function defined in adder.c */ do { printf("\nEnter an integer > "); scanf("%d", ¤t); if (current > SENTINEL) insert(current); /* Function defined in adder.c */ } while (current > SENTINEL); endup(); /* Function defined in adder.c */ } /*-----------------------------------------------------------------*/ /* adder.h -- Interface to adder module */ #ifndef ADDER_H_ #define ADDER_H_ void startup(void); void insert(int current); void endup(void); #endif /*-----------------------------------------------------------------*/ /* adder.c -- The adder module */ #include static int count; /* The number of numbers read. */ static int sum; /* The sum of the numbers read */ void startup(void) { count = sum = 0; } void insert(int current) { count++; sum = sum + current; } void endup(void) { if (count) printf("The sum is %d and the average is %f\n", sum, (0.0+sum)/count); else printf("The sequence was empty\n"); }