Please send questions to st10@humboldt.edu .

...continuing with function sum_array

// signature: sum_array: double[] int -> double

// purpose: expects an array of numbers and how many numbers are
    in the array, and produces the sum of those numbers

header:
double sum_array(double values[], int num_values)

examples:
const int NUM_WTS = 5;
double my_list[NUM_WTS] = {10, 20, 3, 4, 100};

sum_array(my_list, NUM_WTS) == 137

*   as our logic within a single function gets more complex,
    we may need another step after our specific examples to
    finalize the logic;

    (in C++, I don't have as many handy templates as I did
    in Racket...)

    I might want to informally write-out the logic,
    THEN translate it into C++;

    there are MANY ways to do this --
    *   you can draw a flowchart
    *   you can write pseudocode -- English-like depiction of the logic
    *   etc... (programming patterns, etc.)

    *   for THIS problem...

        ...if you are walking through the array,
	   seeing each element as you go,
	   you COULD add each to a "running" total as you go;

	   a local variable could work for that;

           I'll call this sum_so_far
pseudocode:
set a sum_so_far to 0
set an index to 0

while the index is less than num_values
    add values[index] to sum_so_far
    add 1 to index 

return sum_so_far

*   now convert the pseudocode to C++

double sum_array(double values[], int num_values)
{
    double sum_so_far = 0;
    int index = 0;

    while (index < num_values)
    {
        sum_so_far = sum_so_far + values[index];
        index++;
    }

    return sum_so_far;
}

*   just like C++ has more than one branching statement --
    if, which is the most general, and
    switch, which is more special-purpose,

    it also has more than one looping statement;
    while is (arguably) the most general,
    another is special-purpose, especially convenient for
       count-controlled loops: the for statement/for loop

*   do you see that there THREE major steps to controlling a count-controlled
    loop?
    
    1. initialize your counter to a reasonable starting, initial value
    2. to control the repetition, you see if the counter is less than
       the desired limit (to see if you are done)
    3. update the counter WITHIN the loop, so that eventually step 2's
       condition will be false

    the for loop puts these three steps into the first line of
    the loop, so you are LESS LIKELY to forget them (hopefully)

    for (<initialize>; <bool_expr>; <update>)
        statement;

    or, use a block if you are repeating MORE than one statement:

    for (<initialize>; <bool_expr>; <update>)
    {
        statement; 
        ...
        statement;
    }
    
    semantics:
    (see the flowchart you drew in your notes from the board...)
 
    1. the <initialize> action(s) are done;
    2. then the <bool_expr> is evaluated; if true,
       do the body
       {   
           statement;
	   ...
	   statement;
       }
       THEN do the <update>, 
       then return to 2
       if false, go to 3
    3. continue after the loop

    I could re-write sum_array's body using a for-loop with this:

    double sum_so_far = 0;

    for (int index=0; index < num_values; index++)
    {
        sum_so_far = sum_so_far + values[index];
    }

    return sum_so_far

    *   this behaves VERY similarly to the while version,
        although note that index for the for loop has a scope
        LIMITED to the for-loop (it only has meaning in the
	for loop, if declared in the beginning of the for-loop)

LAB EXERCISE:
(submit as HW number 91)
*   work in pairs (put BOTH your names in the opening comment in sum_even.cpp)
*   write a function sum_evens, (you can use funct_play2)
    which expects an array of int and its size,
    and it produces the sum of all the even numbers in that array
    (it returns 0 if none are even)

(note: % is the modulo operator;
       play with it in expr_play to see how it works)