/*--- CS 111 - Week 14 Lecture 1 - 2025-12-02 compile using: g++ 111lect14-1.cpp -o 111lect14-1 run using: ./111lect14-1 by: Sharon Tuttle last modified: 2025-12-02 ---*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> using namespace std; //=== // say I want a function to sum all of the // elements in an array of numbers /*=== signature: sum_array: double[] int -> double purpose: expects an array of numbers and its size, and returns the sum of all of those numbers. tests: double sales[3] = {120.10, 30.20, 1.01}; double my_list[5] = {10, 20, 3, 4, 100}; sum_array(sales, 3) == (120.10 + 30.20 + 1.01) sum_array(my_list, 5) == 137.0 ===*/ double sum_array(double num_set[], int set_size) { int index = 0; double sum_so_far = 0.0; // "walk" through the array num_set, adding up // its contents while (index < set_size) { sum_so_far = sum_so_far + num_set[index]; index = index + 1; } return sum_so_far; } /*--- test the function above ---*/ int main() { cout << boolalpha; cout << "*** Testing: sum_array ***" << endl; double sales[3] = {120.10, 30.20, 1.01}; double my_list[5] = {10, 20, 3, 4, 100}; cout << (sum_array(sales, 3) == (120.10 + 30.20 + 1.01)) << endl; cout << (sum_array(my_list, 5) == 137.0) << endl; cout << "just for fun: " << sum_array(sales, 3) << endl; return EXIT_SUCCESS; }