Link of the picture

So in this seventh class that I had on last Monday I started how to make a program with loops in C++.#Mastery14

What I did for this numeric program is solving the problem to the user by writing a program that asks for a range of integers and then prints the sum of the numbers in that range (inclusive).

I could use a formula to calcute this but what I want to do here is practice using a loop to do repetitive work.

For example, the sum from 6 to 10 would be 0 + 6 + 7 + 8 + 9 + 10.

Notice our sum starts with zero, why?. Because it completes the property of sums that are:

{\displaystyle m>n\;,\quad \sum _{i=m}^{n}a_{i}=0}

I talked this part in my video tutorial: #WSQ04 Fifth Tutorial SUM OF NUMBERS 23/01/17

The number of the lower limit 6 is greater than the numbers from 0 to 5, so that is why the result of the sum is zero.

And when we add each number in the range provided by the user. The mathematical formula to do this calculation is?:

sum

sum2

Pictures of author

After that I change my program to handle the user giving the upper and lower bound in the wrong order with error message.

The resources I need it to solve it are here:

sum link 1

sum link 2

The following photograph shows the solution to this problem:

sum3

Picture of author

So at first I wrote the same structure of the program just did the same as what i did in Hello World: Second Class, Second Blog (Blog of the second class 12/01/17) and Hello World.cpp,  #WSQ01 Post Fun with Numbers 16/01/17 and WSQ1.cpp#WSQ02 Post Temperature 23/01/17 and WSQ02.cpp and #WSQ03 Post Pick a Number 23/01/17 and WSQ03.cpp where i explained the application of the #MasteryTopic01 that it is for comments that could be very useful when debugging and #MasteryTopic04 that is basic output for data. Also I did some other mastery topics.

What i first put in the code was the library to call all the fuctions <iostream> of inputs and outputs of data in languague C++ #MasteryTopic06 Calling Functions and #MasteryTopic08 Importing and using libraries.

Then i had to write a command that everytime goes with the instructions of input and outputs of data that is call std but with this command helps writing these std out of the main because the machine factorizes them.

Next I initialize the begin of my program int main()

After that I had to declarate int variables x,x2,n,op1 initialized in 0, sum initialized in 0 and i

int x,x2,n,op1=0,sum=0,i;

 

Next I needed the #Mastery04 command of out data in form of text to tell the user that the program will calculate the sum of integers in the range you provide and to tell to give the lower bound

cout<<"We will calculate the sum of integers in the range you provide."<<endl;
cout<<"Please give us the lower bound:";

After that, I use the #Mastery05. This command allows enter data for inputs. In this case the user enters the value of variable ‘x’

cin>>x;//#Mastery05 This command allows enter data for inputs. In this case the user enters the
//value of variable 'x'

Next, in this instruction of the for loop, it tells that the value of the variable x is given to the variable x2

x2=x; //In this instruction of the for loop, it tells that the value of the variable x is given
//to the variable x2

After that I use the #Mastery04 command of out data in form of text

cout<<"Please give us the upper bound:";

Next I use the #Mastery05. This command allows enter data for inputs. In this case the user enters the value of variable ‘n’

cin>>n; //#Mastery05 This command allows enter data for inputs. In this case the user enters the
//value of variable 'n'

After that, I use the #Mastery10 use of the if statement. This command allows the condition if the value of x is lower or equal than the value of n and when this condition will be true, the tasks inside of the statement will execute.In this case if the value x is lower or equal than n this will occur:

if (x<=n) //#Mastery10 use of the if statement //This command allows the condition if the value of x
//is lower or equal than the value of n and when this condition will be true, the tasks inside of the statement will execute.
// In this case if the value x is lower or equal than n this will occur:
{
//#Mastery14 use of loops with for
for(i=x; i<=n ; i++) //With this command for is utilized as a loop FOR execute all the
//instruccions that has this loop until that does not complete that the variable i
// will be less or equal than n while it is increasing one in one in this variable i
//inicialized from the value x that was an input value given by the user.
{
x=i; //In this instruction of the for loop, it tells that the value of the variable i is given
//to the variable x

op1=(1*x); //In this instruction of the for loop, it tells that the value of the variable x times 1 is given
//to the variable op1

sum=sum+op1; //In this instruction of the for loop, it tells we have an accumulator that tells the value of
//the variable sum plus the variable op1 is given to the variable op1. In this case we
//have a variable whose function is save the value of the operation sum until the loop
//for finishes with all the sum of integers in the range you provide saved in variable sum


} //end of FOR

cout<<"The sum from "<<x2<<" to "<<n<<" is="<<sum<<endl;//command of out data in form of text
//In this text we show the authentic output of the value entered of x and n in the terminal interface #MasteryTopic04
}

Next I use the #Mastery14 use of loops with for. With this command for is utilized as a loop FOR execute all the instruccions that has this loop until that does not complete that the variable i will be less or equal than n while it is increasing one in one in this variable i inicialized from the value x that was an input value given by the user.

forloopoverview

Link of the picture

//#Mastery14 use of loops with for
for(i=x; i<=n ; i++) //With this command for is utilized as a loop FOR execute all the
//instruccions that has this loop until that does not complete that the variable i
// will be less or equal than n while it is increasing one in one in this variable i
//inicialized from the value x that was an input value given by the user.
{
x=i; //In this instruction of the for loop, it tells that the value of the variable i is given
//to the variable x

op1=(1*x); //In this instruction of the for loop, it tells that the value of the variable x times 1 is given
//to the variable op1

sum=sum+op1; //In this instruction of the for loop, it tells we have an accumulator that tells the value of
//the variable sum plus the variable op1 is given to the variable op1. In this case we
//have a variable whose function is save the value of the operation sum until the loop
//for finishes with all the sum of integers in the range you provide saved in variable sum


} //end of FOR

Inside of the FOR I have an instruction of the for loop, it tells that the value of the variable i is given to the variable x

x=i; //In this instruction of the for loop, it tells that the value of the variable i is given
//to the variable x

After that I have an instruction of the for loop, it tells that the value of the variable x times 1 is given to the variable op1

op1=(1*x); //In this instruction of the for loop, it tells that the value of the variable x times 1 is given
//to the variable op1

Next I need an instruction of the for loop, it tells we have an accumulator that tells the value of the variable sum plus the variable op1 is given to the variable op1. In this case we have a variable whose function is save the value of the operation sum until the loop for finishes with all the sum of integers in the range you provide saved in variable sum and the end of FOR.

sum=sum+op1; //In this instruction of the for loop, it tells we have an accumulator that tells the value of
//the variable sum plus the variable op1 is given to the variable op1. In this case we
//have a variable whose function is save the value of the operation sum until the loop
//for finishes with all the sum of integers in the range you provide saved in variable sum

}//end of FOR

After that I have a command of out data in form of text. In this text we show the authentic output of the value entered of x and n in the terminal interface #MasteryTopic04 and end of if

cout<<"The sum from "<<x2<<" to "<<n<<" is="<<sum<<endl;//command of out data in form of text
//In this text we show the authentic output of the value entered of x and n in the terminal interface #MasteryTopic04
}//end of IF

Next I have the #Mastery11 Use of else with a conditional if: if the condition does not achieve on the other hand you will do this:

else
{
cout<<endl;//comand of out data in form of text space
cout<<"Error, Wrong other of limits, Run the program again and put the correct limits!!!"<<endl; //command of out data in form of text
}



} //END OF THE PROGRAM

Finally we have a command of out data in form of text to tell the user to run the program again and put the correct limits because the user put those in the wrong order:

cout<<endl;//comand of out data in form of text space
cout<<"Error, Wrong other of limits, Run the program again and put the correct limits!!!"<<endl; //command of out data in form of text

The following pictures shows the code of the program:

1

2

3

And i also resolved it by doing this video tutorial that i made reafirming these parts at this link : #WSQ04 Fifth Tutorial SUM OF NUMBERS 23/01/17

Finally with the command ./a. out tells the terminal to run de C++ file:4

This code will be here and in Github:

Link in Github:Link in Github

If you do not want the #WSQ4.cpp there is it in here with comments. The comments are very useful to detect errors and create new things #Mastery01 #Masterytopic01 #MasteryTopic01 #MASTERY01 #MASTERYTOPIC01 :

Also in the last class i learned how to present my C++ in a better ‘readable’ way after doing it #Mastery02 #Masterytopic02 #MasteryTopic02 #MASTERY02 #MASTERYTOPIC02:

#include <iostream> //Library to call all the
//fuctions of inputs and outputs of data in languague
//C++ #MasteryTopic01 and #Mastery08 (Importing and using libraries)

using namespace std; //In C++ we need a command
// that everytime goes with the instructions of input and outputs
//of data that is call std but with this command helps writing
//these std out of the main because the machine factorizes them

int main () //Begin of the program
{
int x,x2,n,op1=0,sum=0,i; //Declarate int variables x,x2,n,op1 initialized in 0, sum initialized in 0 and i

cout<<"We will calculate the sum of integers in the range you provide."<<endl; //#Mastery04 command of out data in form of text
cout<<"Please give us the lower bound:"; //#Mastery04 command of out data in form of text
cin>>x;//#Mastery05 This command allows enter data for inputs. In this case the user enters the
//value of variable 'x'
x2=x; //In this instruction of the for loop, it tells that the value of the variable x is given
//to the variable x2
cout<<"Please give us the upper bound:"; //#Mastery04 command of out data in form of text
cin>>n; //#Mastery05 This command allows enter data for inputs. In this case the user enters the
//value of variable 'n'


if (x<=n) //#Mastery10 use of the if statement //This command allows the condition if the value of x
//is lower or equal than the value of n and when this condition will be true, the tasks inside of the statement will execute.
// In this case if the value x is lower or equal than n this will occur:
{
//#Mastery14 use of loops with for
for(i=x; i<=n ; i++) //With this command for is utilized as a loop FOR execute all the
//instruccions that has this loop until that does not complete that the variable i
// will be less or equal than n while it is increasing one in one in this variable i
//inicialized from the value x that was an input value given by the user.
{
x=i; //In this instruction of the for loop, it tells that the value of the variable i is given
//to the variable x

op1=(1*x); //In this instruction of the for loop, it tells that the value of the variable x times 1 is given
//to the variable op1

sum=sum+op1; //In this instruction of the for loop, it tells we have an accumulator that tells the value of
//the variable sum plus the variable op1 is given to the variable op1. In this case we
//have a variable whose function is save the value of the operation sum until the loop
//for finishes with all the sum of integers in the range you provide saved in variable sum


} //end of FOR

cout<<"The sum from "<<x2<<" to "<<n<<" is="<<sum<<endl;//command of out data in form of text
//In this text we show the authentic output of the value entered of x and n in the terminal interface #MasteryTopic04
}

else
{
cout<<endl;//comand of out data in form of text space
cout<<"Error, Wrong other of limits, Run the program again and put the correct limits!!!"<<endl; //command of out data in form of text
}



} //END OF THE PROGRAM

 

 

17 comentarios en “#WSQ04 Post Sum of Numbers 23/01/17 and WSQ04.cpp

  1. Wow, it’s full of comments but it’s a nice code. I like how you manage to write everything you do, step by step, because it helps other students (like me). How do you put your code in the post? It looks like a quote but with colors and stuff 😀

    Le gusta a 1 persona

Deja un comentario