søndag den 14. juli 2019

C++ exercise 1.9 - C++ Primer 4th edition 2005

C++ Primer Fourth Edition 2005
ISBN-13: 978-0-201-72148-5
ISBN:10: 0-201-72148-1
Page 17.
Exercise Section 1.4.2
Exercise 1.9: What does the following for loop do? What is the final value of sum?

int sum = 0;
for(int i = -100; i <= 100; ++i)
    sum += i;
This is an answer and an extended solution
  1. The for loop initialize i with the value -100 (init-statement)
  2. The for loop evaluate i <= 100 (is i less than or equal to 100?) (condition)
  3. Body statement gets executed. The body statement adds i to sum and assign the result to sum, then i is increment by 1 and again evaluate the condition in the for loop header until i equal to 101. The final value of the whole loop is 0 because the above the statement calculates sum+i in the range from -100 to 100.
Every time the for loop takes around it is called an iteration.

For each iteration i get a new value of "i incremented by 1" and sum gets a new value of "sum + i"

The loop keeps repeating (iteration) until the condition ( i <=100; ) reach 101 because then the condition is False and the loop stops running.

To run and test the code we have to make small changes to it. written in red color

#include <iostream>
int main()
{
    int sum = 0;
    
    for(int i = -100; i <= 100; ++i)
    {
        sum += i;
        std::cout << sum << std::endl;
    }
}

Just copy/paste the text into to your text editor and save it as a .cpp or .cc then compile and run it and you will get the result of 0 like in the picture.

This is and image of your console program
The result is 0
If you scroll up on the right side (scroll bar) of the terminal/prompt you will see that -5050 is respeated twice.

why is -5050 repeated twice?

Why is that? Why is -5050 repeated twice?.. lets find out :)

When you have the code:

int sum = 0;
    
for(int i = -100; i <= 100; ++i)
{
    sum += i;
    std::cout << sum << std::endl;
}

Iteration 1: step 1
// i is initilized with -100
for(int i = -100; i <= 100; ++i)
{
    sum += i;
    std::cout << sum << std::endl;
}
Iteration 1: step 2
// i <= 100 (is -100 less than or equal to 100?) if it's true then keep going.  
for(int i = -100; i <= 100; ++i)
{
    sum += i;
    std::cout << sum << std::endl;
}
Iteration 1: step 3
for(int i = -100; i <= 100; ++i)
{
    // i gets added to sum (-100 = 0 + -100)(sum's new value is -100)
    sum += i;
    std::cout << sum << std::endl;
}
Iteration 1: step 4
for(int i = -100; i <= 100; ++i)
{ 
    sum += i;
    // the new value sum(-100) is printes to the screen
    std::cout << sum << std::endl;
}
Iteration 1: step 5 (the result is only being used in the next loop (loop 2)
// i gets a new value ++i (i = i + 1) (-99 = - 100 + 1) 
for(int i = -100; i <= 100; ++i)
{ 
    sum += i; 
    std::cout << sum << std::endl; 
} 
 
Iteration 1:
Step 1. for header: i is initialized with -100
Step 2. for header: i <= 100 (is -100 less than or equal to 100?) if it's true then keep ongoing.
Step 3. for body: i gets added to sum (-100 = 0 + -100)( sum's new value is -100)
Step 4. for body: the new value sum(-100) is printed to the screen
Step 5. for header: i gets a new value ++i (i = i + 1) (-99 = - 100 + 1)


Iteration 2: :

Step 1. for header: i is initialized with the new value of -99
Step 2. for header: i <= 100 (is i less than or equal to 100?) if it is true then keep ongoing.
Step 3. for body: i gets added to sum (-199 = -100 + -99)( sum's new value is -199)
Step 4. for body: the new value sum(-199) is printed to the screen
Step 5. for header: i gets a new value ++i (i = i + 1) (-98 = - 99 + 1)

Now you should be able to figure out how loop 3, 4, 5, 6, etc. works

Why is -5050 repeated twice?: Answer

When you get to Iteration 99 the sum is = -5049 that sum will be used in interation 100

Iteration 100

Iteration 100. i = -1. sum = -5050
because (-5049) + (-1) = (-5050)

for(int i = -1; i <= 100; ++i)
{

    sum  += i;
// -5049 + -1 = -5050 
    std::cout << sum << std::endl;
}
Now remember in the next iteration (iteration number 101) the (i = -1) value will be incremented by 1 because of ++i (i = i + 1) in the for header and therefor the result of i will be 0

Iteration 101

Iteration 100. i = 0. sum = -5050
because (-5050) + 0 = (-5050)

for(int i = 0; i <= 100; ++i)
{

    sum  += i;
// -5050 + 0 = -5050 
    std::cout << sum << std::endl;
}

Why is -5050 repeated twice? Conclusion

As you can se in iteration 100 and iteration 101 the sum is -5050
-5049 + -1 = -5050
-5050 + 0 = -5050

-5050 ? shown in X and Y graph

Here is a graph that shows what happens to the variable sum when the loop runs 200 times. At iteration 100(bottom line) you can see that the blue line can't get further downwards. That the point where "i" (red line) is equal to 0
A graph of the For loop. The blue line is "sum + i" and red line is i's new value
A graph of the For loop. The blue line is "sum + i" and red line is i's new value
If you just know a little arithmetics and X, Y graph/coordinates then you should be able to read and understand the graph.

Graph illustration of the for loop
At the crossing of the green and red line is the spot for i = 0.
At the crossing of the green and blue line is the spot for sum = -5050

I hope this gives you an in depth understanding of the "Exercise 1.9: What does the following for loop do? What is the final value of sum?" from the book C++ Primer Fourth Edition 2005.

Ingen kommentarer:

Send en kommentar