arrfun1.cpp 620 B

1234567891011121314151617181920212223242526
  1. // arrfun1.cpp -- functions with an array argument
  2. #include <iostream>
  3. const int ArSize = 8;
  4. int sum_arr(int arr[], int n); // prototype
  5. int main()
  6. {
  7. using namespace std;
  8. int cookies[ArSize] = {1,2,4,8,16,32,64,128};
  9. // some systems require preceding int with static to
  10. // enable array initialization
  11. int sum = sum_arr(cookies, ArSize);
  12. cout << "Total cookies eaten: " << sum << "\n";
  13. // cin.get();
  14. return 0;
  15. }
  16. // return the sum of an integer array
  17. int sum_arr(int arr[], int n)
  18. {
  19. int total = 0;
  20. for (int i = 0; i < n; i++)
  21. total = total + arr[i];
  22. return total;
  23. }