Skip to content

Coding Sum

Now we have the data in the program, we can start to think about the statistics we want to calculate.

The sum is the simplest of the statistics. This involves adding together all the numbers in the array. The main issue here is that the computer cannot add all of these values together, and we must rethink our logic to express it in terms of processing for each element.

Think about the way you would sum a list of numbers, this is now the task you need to code for the computer. What is it that you do with each number? To think this through write a list of random numbers down. Keep it simple, make each number between 0 and 9. Now calculate the sum. Do it slowly, and think about the tasks that you are performing for each number. Allow yourself only to look at one number at a time.

You should have noticed that you achieve this by keeping a running total, and that you add the value of each number from the list to that total. When you have done this for each number in the list, you have the sum of all values. The pseudocode for this is shown below.

Function: sum
-------------------------------
Returns:
- double: The sum of the numbers from the data array
Parameters:
- data (const reference to number data) - the data to sum
Local Variables:
- i (int): index of the current element in the array
- total (double): running total
Steps
1. total is assigned 0
2. For i. starts at 0 and loops while i < data.size
3. total is assigned total + the value of the ith element of data.values
4. Return the result total.

Have a go at coding this yourself.

  • /* stats-calc.cpp - from the field guide. written by ... */
    #include <cstdio>
    #include "utilities.h"
    // The maximum number of values we can store
    const int MAX_NUMBERS = 20;
    /**
    * The data structure to store the numbers
    *
    * @field values the array of values
    * @field size the number of values in the array - up to MAX_NUMBERS
    */
    typedef struct
    {
    double values[MAX_NUMBERS];
    int size;
    } number_data;
    /**
    * Populate the array with values entered by the user
    *
    * @param data the array of values (passed by reference)
    */
    void populate_array(number_data &data)
    {
    int size = read_integer("How many values do you want to enter? ");
    if (size > MAX_NUMBERS)
    {
    printf("Sorry, you can only enter %d values.\n", MAX_NUMBERS);
    size = MAX_NUMBERS;
    }
    else if (size < 0)
    {
    size = 0;
    }
    data.size = size;
    // Populate each element - up to data.size
    for (int i = 0; i < data.size; i++)
    {
    data.values[i] = read_double("Enter value: ");
    }
    }
    /**
    * Output the values in the array
    *
    * @param data the array of values
    */
    void print(const number_data &data)
    {
    for (int i = 0; i < data.size; i++)
    {
    printf("%d: %lf\n", i, data.values[i]);
    }
    }
    /**
    * Calculate the sum of the values in the array
    *
    * @param data the array of values
    * @return the sum of the values
    */
    double sum(const number_data &data)
    {
    int i;
    double result = 0;
    for (i = 0; i < data.size; i++)
    {
    result += data.values[i];
    }
    return result;
    }
    //todo: add max here
    //todo: add mean here
    int main()
    {
    number_data data = {{},0};
    populate_array(data);
    printf("\nYou entered...\n\n");
    print(data);
    printf("\nCalculating statistics...\n\n");
    printf("Sum: %4.2lf\n", sum(data));
    return 0;
    }

Make sure to test your program is working. You should be able to enter different numbers of values each time, and see the sum of the values you entered.