Consider the following C program. It is supposed to read a list of numbers
from the user until the user types -1; then it should print the average
of the numbers. It won't work for several reasons.
Describe four of the errors in the program.
Don't give any more than four; four right answers and one wrong answer
still means points off.
#include <stdio.h>
#include <stdlib.h>
int main () {
double sum, avg, num
int n;
n = 0;
printf ("Enter some numbers, -1 when done.\n");
scanf ("%f", &num);
while (num != -1) {
sum += num;
n++;
scanf ("%lf", &num);
avg = sum / n;
printf ("The average is %f\n", avg);
exit 0;
}
Some errors:
1. Semicolon is missing from the end of the first 'double' declaration.
2. First scanf should use the "%lf" format to read a double.
3. The while statement has an open brace without a close brace.
4. The value of 'n' could be zero causing a division by zero error.
5. The exit function should have parentheses around its argument.