5.3: Case Study- Find The Sum

Introduction to Programming with C++ · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    5.3: Case Study- Find The Sum

    Programming Task:

    Write a program with a looping statement that allow a user to enter a set of integers and then produce (display) the sum of those numbers.

    As always, we begin with an empty project with a blank source file opened and type in the code for the initial settings:

    Initial Setting.png

    Step 1: Write a program that allows a user to enter 3 integers and display the sum of those numbers user entered.

    We first declares two integer variables sum and a. We use the variable sum for storing the summation and use the variable a for storing the new number user entered.

    A2-6-1.png

    Next, we set the initial value of the variable sum to 0. Then use a set of cout and cin statements to allow a user to enter the first number into the program and save this number in the variable a as shown in the screenshot below:

    A2-6-2.png

    In the next line, we will put in a so called augmented statement: sum = sum + a; or sum+=a; this statement will take the value of sum and the value of a, add them up and put the result back to the variable sum. (Please review section 2.3 for more information about augmented statement and augmented operators)

    A2-6-3.png

    At this point, the variable sum in the program is holding the first number user entered. Now, we copy the code in line 8, 9, and 10 and paste them into the program after line 10 as shown in the red box below:

    LA2-6-4-1.png

    Now, line 11 asks the user to enter the second number. Line 12 is using cin statement to put the second number user entered in the variable a. The line 13 takes the value of the variable sum (it is holding the first number user entered) and the value of the variable a which is holding the second number user entered, adds them up and puts the result back to the variable sum (augmented statement).

    At this point, the variable sum is holding the sum of the first and the second number user entered so far.

    Now, we copy and paste those code in the red box above (line 11, 12 and 13) into the program right after the line 13:

    A2-6-5.png

    In the program shown above, the line 14 is asking user to enter the third number, and the line 15 saves it in the variable a. Again, line 16 adds the value of sum (at this point the variable sum is holding the sum of the first two numbers user entered) and value of the variable a, puts the result back into the variable sum.

    Thus, after the line 16 in this program, the variable sum is holding the sum of all three numbers user entered. What we need to do now is to put in a cout statement to display the sum of the three numbers user entered:

    A2-6-6.png

    Now, we are done with this program. this is only the step 1. If we take a close look at this program, we can see this program runs the same group of statements three time:

    A2-6-7.png

    As we know, if a program needs to repeatedly execute a statement or a group of statements, we can use a looping statement. Thus, we can use a looping statement in place of those three groups of statements in the program. If you are not quite sure how to do it or if you are not quite sure what we are talking about. You can try to do following:

    1. Copy the code in one of the red boxes to the clipboard
    2. Delete the code in all three red boxes
    3. Type in the code we used for displaying "Good Morning!" messages as shown below:
      A2-6-8.png
    4. Delete line10, and paste the code you copied to the clipboard in 1 at line 10:
      A2-6-9.png

    This program is almost the one we would like to have for this assignment. However, it only calculates and displays the sum of three integers a user entered. It won't accommodate different needs of a user. For example, one user may need to find the sum of 5 integers and the other may need to calculate the sum of 20 integers and so on.

    I will leave the final version of this program to you to complete. If you don't know how to complete the final version of this program, please study section 5.2 where we were in a very similar situation and we solved this problem with an additional variable and a set of cout and sin statements.


    This page titled 5.3: Case Study- Find The Sum was last modified on Wed, 24 Dec 2025 20:37:20 GMT and is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Jin He.