søndag den 26. maj 2019

C++ using for loop to sum the numbers from 50 to 100

This exercise is from the C++ primer 4th edition book from 2005. The program works and runs fine. This is a solution to the exersice section 1.4.2 on page 17.

Exercise 1.10: Write a program that uses for loop to sum the numbers from 50 to 100. Now rewrite the program using a while loop.
💥 Beware: This is not just a simple program that adds two numbers together. It is a little bit more complicated than that.

For loop program

Firs we are going to write the for loop program. Sum the number from 50 to 100 using a for loop. you can copy/paste the code in to your text editor, notepad, Atom, SublimeText etc. and save it as a .cc file for linux and .cpp file for windows
#include <iostream>
int main()
{
  int sum = 0;
  int loopcount = 0;

  for (int i = 50; i < 100; i++) 
  {
    loopcount++;
    sum += i;
    std::cout << "Sum of loop number " 
    << loopcount << " is " 
    << sum << std::endl;
  }
  return 0;
}

A C++ program that uses a for loop to sum the numbers from 50 to 100
Not a simple C++ program that add numbers together
 
On line 11 i added a line to help you understand how the loop function. Se image further down.

std::cout << "Sum of loop number " 
<< loopcount << " is " << sum << std::endl; 

The variable "loopcount" gets incremented each time the loop runs.

For loop - part 1: the for header

On line seven you see the start of the for loop. The for loop consists of two parts. The first parts is called the "for header" and the secound parts is called the "for body"

"for header" (part one) consists of three parts. an init-statement, a condition, and an expression.

for (Init-statement, Condition, Expression) 
{
    "For body" this the code or 
     text inside of the breckets {}
}

The "for header" is marked in red text color

#include <iostream>
int main()
{
  int sum = 0;
  int loopcount = 0;

  for (int i = 50; i < 100; i++)
  {
    loopcount++;
    sum += i;
    std::cout << "Sum of loop number " 
    << loopcount << " is " 
    << sum << std::endl;
  }
  return 0;
}  

Init-statement

The intit-statement is set to "int i = 50;" it is a variable which have the value of an integer meaning numbers (not floating point number)

Condition

The condition in the for loop is "i < 100;". Is the variable "i" less than 100? if this condition is True then run the code in "for body".

Expression

The expression "i++" in the "for header" is only executed after the "for body" have been executed. The two plus signs in i++ is short for (i++) = (i = i + 1) and adds one to the variable "i" every time the loop takes a round. Meaning the variable "i" gets incrementet by 1 for every round the Condition is True.

For loop - part 2: the for body

The "for body" in the for loop is the code, that gets executed when the condition in the for header is True.

The "for body" is the part in red text color

#include <iostream>
int main()
{
  int sum = 0;
  int loopcount = 0;

  for (int i = 50; i < 100; i++) 
  {
    loopcount++;
    sum += i;
    std::cout << "Sum of loop number " 
    << loopcount << " is " 
    << sum << std::endl;
  }
  return 0;
} 
When you compile an execute the program in the terminal it will look like this:
C++ program compiled and executed in the linuix terminal shows us the result of the program
Here we see the c++ program being compiled and executed in the Linux Terminal
As you can see i made the program using Atom Text editor and Terminal in the Linux inviroment but it is the same process in Windows the only difference is that you save your C++ file in .cc file format in linux and in Windows you save it as a .cpp file format.
  • .cc file format in Linux
  • .cpp file format for windows
you still use the same compiler command in the terminal or command prompt
 
g++ forsum.cc -o forsum
g++ forsum.cpp -o forsum 

to compile your code into 0 and 1 that your computer can understand. You run your executeable file by typing ./forsum

The for loop step by step

#include <iostream>
int main()
{
  int sum = 0;
  int loopcount = 0;

  for (int i = 50; i < 100; i++)
  {
    loopcount++;
    sum += i;
    std::cout << "Sum of loop number " 
    << loopcount << " is " 
    << sum << std::endl;
  }
  return 0;
}

  • The variable "sum" gets the value of 0
  • The variable loopcount gets the value of 0
  • In the "for header" the variable "i" get the value of 50
  • If the Condition "is i less than 100?" is True, the for loop keep running and executes the "for body"
  • In the "for body" the variable "loopcount" gets incremented by 1
  • In the "for body" the variable "i" gets added to the variable "sum"
  • In the "for body" the text (string) "Sum of loop number loopcoun is sum" is printet on the screen.
  • In the "for header" the varaibel "i" is incrementet by 1.
  • The whole process keeps repeating (looping) untill the "for header" condition is False

Rewrite using while loop

Now that we made program that can sum the numbers from 50 to 100 we still need to rewrite the program using a while loop. I will write that in the next blog post.
The solution to rewrite the for loop to a while lop here....

Ingen kommentarer:

Send en kommentar