/************************************************** * sum-array.c * * Michael Siff - Computer Architecture - Fall 2007 * * compute the sum of an array of integers * **************************************************/ #include #define MAX_ARRAY_SIZE 10 int sumArray(int a[], int n) { int i = 0; int sum = 0; for (i = 0; i < n; i++) { sum += a[i]; } return sum; } int main() { int x = 0; int myArray[MAX_ARRAY_SIZE]; int nElements = 0; while (x >= 0 && nElements < MAX_ARRAY_SIZE) { printf("Enter a number or negative to stop: "); scanf("%d", &x); /* read in an integer and store it in x */ if (x >= 0) { myArray[nElements] = x; nElements++; } } printf("There are %d elements and their sum is: %d\n", nElements, sumArray(myArray, nElements)); return 0; }