## A Sum of Numbers

This chapter will demonstrate how to create a program for calculating the sum of several numbers, or many more. It is one of the most basic problems a programmer has to solve, appearing quite frequently in programs.

When given a list of several numbers (for example 12, 6, 3, 31, 11), one way of adding them all up is to proceed one item (i.e. term) at a time. We start with the value 0 and add the first term 12 to get the number 12; then we add 6 to that and get the number 18; then we add the term 3 to get 21; then we add 31 to get 52; and finally, we add 11 to get 63. Therefore, the sum of all numbers in the given list equals 63.

Let us write a program that implements this method to add up 5 numbers. The numbers will be input by a user one by one. To be more informative, the program will also print out the sum-so-far on each step of the process.

You can run the programs given in this chapter by using an online service called C++ shell

#include <iostream> using namespace std; int main() { double sum = 0; double inputValue; cout << "Type in five numbers:" << endl; for (double i=1; i<=5; i++) { cin >> inputValue; sum = sum + inputValue; cout << "The current sum is: " << sum << endl; } }

Here is one possible result of running this program:

Type in five numbers: 2 The current sum is: 2 5 The current sum is: 7 3 The current sum is: 10 5 The current sum is: 15 1 The current sum is: 16

How does this program work? The key
parts are the following two statements, which get repeated by the `for`

loop:

cin >> inputValue; sum = sum + inputValue;

This part of the code is repeated five
times. In each iteration the first statement reads a value from standard input.
The second statement then adds that value to the variable `sum`

, that
is to the currently accumulated sum. That statement can be interpreted in the following
manner: add a just input value to the current total sum. And, that would be the
entire idea of calculating the sum of many numbers.

There is one crucial step to this method though, which novice programmers often tend to forget. Surprisingly, it is the first and the simplest step:

double sum = 0;

When accumulating a sum, do not forget to set the sum to zero as the first step! |

Summing up is a task often encountered in programming, so much so that the authors of C++ have decided to allow programmers to write it more concisely. The statement

sum = sum + inputValue;

can be replaced by:

sum += inputValue;

We read this statement as: "Add up `inputValue`

to `sum`

.", or "Increase `sum`

by `inputValue`

."

For example, after executing the statements

double x=3; x+=8;

the variable `x`

will
contain the number 11.

Similarly, we can use the operator '`-=`

' to
decrease the value of a variable. For example

double y=9; double z=7; y-=z-1;

Since `z-1`

equals 6, then `y`

gets
decreased by 6, which in turn means that `y`

will assume the number 3. So, you
can see that the expression `y-=z-1`

is actually the same as `y=y-(z-1)`

. Do pay attention to the location
of parentheses there.

C++ allows us to use a number of similar shorthand operators for various arithmetic operations as well. We present some of them in the following table:

Operator | Use | Longer Form |
---|---|---|

`+=` | `a+=b` | `a=a+b` |

`-=` | `a-=b` | `a=a-b` |

`*=` | `a*=b` | `a=a*b` |

`/=` | `a/=b` | `a=a/b` |