/* A Program to sum the elements of an array */ #include #include #define SIZE 10 long sump(int *ar, int n); // function prototype main(void) { int marbles[SIZE] = {21, 10, 5, 39, 4, 16, 19, 26, 31, 20}; long int answer; answer = sump(marbles, SIZE); /* C does not allow arrays to be passed as function arguments. However we pass the name of the array, which is actually the address of where the array begins. That is, marbles[0]. */ printf("The total number of marbles is %ld.\n", answer); return(0); } long int sump(int *ar, int n) /* Use pointer arithemetic. Recall that ar[] is equivalent to *ar */ { int i = 0; long int total = 0; while(i < n) { total += *ar; // add array element value to total ar++; // advance pointer to next array element i++; // increment loop counter } return(total); }