sum_arr1.c 654 B

1234567891011121314151617181920212223242526272829
  1. // sum_arr1.c -- sums the elements of an array
  2. // use %u or %lu if %zd doesn't work
  3. #include <stdio.h>
  4. #define SIZE 10
  5. int sum(int ar[], int n);
  6. int main(void)
  7. {
  8. int marbles[SIZE] = {20,10,5,39,4,16,19,26,31,20};
  9. long answer;
  10. answer = sum(marbles, SIZE);
  11. printf("The total number of marbles is %ld.\n", answer);
  12. printf("The size of marbles is %zd bytes.\n",
  13. sizeof marbles);
  14. return 0;
  15. }
  16. int sum(int ar[], int n) // how big an array?
  17. {
  18. int i;
  19. int total = 0;
  20. for( i = 0; i < n; i++)
  21. total += ar[i];
  22. printf("The size of ar is %zd bytes.\n", sizeof ar);
  23. return total;
  24. }