/** * Program to sum integers using array (example of while and for loops). * Improved version that stops and prints an error message if the array fills * up. * (We don't need an array to solve this problem; it's used only to * illustrate using an array.) */ #include #define ARRAY_SIZE 10 int main(void) { int input; int sum = 0; int num[ARRAY_SIZE]; int count = 0; printf("enter some integers (at most %d values), anything else to stop\n", ARRAY_SIZE); while (scanf("%d", &input) == 1) { if (count >= ARRAY_SIZE) { printf("only %d values allowed\n", ARRAY_SIZE); return 1; } num[count++] = input; } for (int i = 0; i < count; ++i) { sum += num[i]; } printf("sum is %d\n", sum); return 0; }