So in this fourteen week class I started with doing this WSQ12 I started reviewing in creating and calling functions in C++.#Mastery06, #Mastery07, #Mastery16 Use of recursion for repetitive algorithms, #Mastery17 When to use what type of repetition in a program, #Mastery18 Creation and use of Arrays/ Vectors in C++. Furthermore, in this stage I have all the topics of the course from 1 to 22.

What I did for this numeric program is solving the problem to the user by creating a program with writing this assignment where I will estimate the mathematical constant e. I should create a function called calculuate_e which receives one parameter called precision that should specify the number of decimal points of accuracy.

I will want to use the infinite series to calculate the value, stopping when the accuracy is reached (previous and current calculation are the same at the specified accuracy).

Hence, the resources I need it to solve this program are here:

For doing this tutorial you need to do the factorial, therefore you need to see my tutorial #WSQ06 Factorial Calculator 12/02/17 and WSQ06.cpp

ken bauer

And the next picture shows us how the number e is calculated with infinite series and I will do this infinite serie in c++

Resultado de imagen para e number

Link of picture

The following photograph shows the solution to this problem:

w1

w2

w3

p666

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#WSQ03 Post Pick a Number 23/01/17 and WSQ03.cpp#WSQ04 Post Sum of Numbers 23/01/17 and WSQ04.cpp#WSQ05 Six Tutorial On To Functions 12/02/17 and WSQ05.cpp#WSQ06 Factorial Calculator 12/02/17 and WSQ06.cpp#WSQ07 Lists 03/03/17 and WSQ07.cpp, #WSQ08 Yo soy 196 11/03/17 and WSQ08.cpp#WSQ09 Multipart Data and Files 10/03/17 and WSQ09.cpp , #WSQ10 Babylonian Method 21/03/17 and WSQ10.cpp and #WSQ11 Go Bananas 15/04/17 and WSQ11.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 of inputs and outputs of data in languague C++ #MasteryTopic01.

#include <iostream> //Library to call all the
//fuctions of inputs and outputs of data in languague
//C++ #MasteryTopic01

Then, we need to use a Library to call all the fuctions of math commands in languague C++ #MasteryTopic01


#include <cmath> //Library to call all the
//fuctions of math commands in languague
//C++ #MasteryTopic01

Next, 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

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

 

Next we have our int main and we declarate float variable precision

float precision; //Declarate float variable precision

Then we need a #Mastery04 command of out data in form of text

cout<<"Specify the number of decimal points of accuracy: "; //#Mastery04 command of out data in form of text

After that, we need a #Mastery05 This command allows enter data for inputs. In this case this enters the value of variable ‘precision’

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

«Finally» the program comes back to the int main when I called the function calculate_e with the precision parameter, we need a command of out data in form of text. In this text we show the authentic output of the value entered of precision in the terminal interface #MasteryTopic04

cout<<"The estimation of e with "<<precision<<" decimal points is e = "<<calculuate_e (precision)<<endl; //command of out data in form of text
//In this text we show the authentic output of the value entered of precision in the terminal interface #MasteryTopic04

Next in the calculate_e function. This is the structure of a function where you have subrutines in order to make different tasks, in this case we have a FLOAT function with the name «calculuate_e» and with float parameter precision in order to have float results inside this function.

float calculuate_e (float precision) //This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have a FLOAT function with the name "calculuate_e" and with float parameter precision
//in order to have float results inside this function.

Furthermore in the function, we need to declarate float variable e initialized in 1

float e = 1; //Declarate float variable e initialized in 1

Furthermore in the function, we need to declarate float variable previuse.

float previuse; //Declarate float variable previuse

Next, we need to declarate an int variable i initialized in 1

int i=1; //Declarate float variable i initialized in 1

Moreover, with this command do is utilized as a loop do execute all the instruccions that has this loop until that does not complete that the variable of the absolute value of the difference between e and previuse abs e-previuse>precision will be less or equal than the numerical value of precision variable while it is increasing one in one in this variable i inicialized from the value 1. This was the condition of the WSQ

do //With this command do is utilized as a loop do execute all the
//instruccions that has this loop until that does not complete that the variable abs e-previuse>precision
// will be less or equal than the numerical value of precision variable while it is increasing one in one in this variable i
//inicialized from the value 1
{
previuse=e;
e = e + 1/(factorial(i)); //This is a math operation between 'e' and
// the value of the float function factorial with the int parameter i and stands for
//do the sum of e and the inverse operation of the factorial function with the i parameter and saved it in the float
//variable 'e', this is the operation of the infinite series thanks to the recursion #Mastery16 Use of recursion for repetitive
//algorithms
cout<<e<<endl;
i++;
} while(abs (e-previuse)>precision);

Then, we need to give the value of e to variable previuse

previuse=e;//Give the value of e to variable previuse

Next, This is a math operation between ‘e’ and the value of the float function factorial with the int parameter i and stands for do the sum of e and the inverse operation of the factorial function with the i parameter and saved it in the float variable ‘e’, this is the operation of the infinite series thanks to the recursion #Mastery16 Use of recursion for repetitive algorithms.

e = e + 1/(factorial(i)); //This is a math operation between 'e' and
// the value of the float function factorial with the int parameter i and stands for
//do the sum of e and the inverse operation of the factorial function with the i parameter and saved it in the float
//variable 'e', this is the operation of the infinite series thanks to the recursion #Mastery16 Use of recursion for repetitive
//algorithms

Moreover, after this last operation the program goes to a function that calculates the factorial. This is the structure of a function where you have subrutines in order to make different tasks, in this case we have a float function with the name «factorial» and with int parameters n in order to have int results inside this function.

float factorial(int n) //This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have a float function with the name "factorial" and with int parameters n in order to
//have int results inside this function.

In addition, we need the #Mastery10 use of the if statement. This command allows the condition if the value of n is equal to 0 and when this condition will be true, the tasks inside of the statement will execute. In this case if the value of n is equal than 0 this will occur:

if(n==0) //#Mastery10 use of the if statement //This command allows the condition if the value of n
//is equal to 0 and when this condition will be true, the tasks inside of the statement will execute.
// In this case if the value of n is equal than 0 this will occur:
{
return 1; //return the value 1 to the function calculuate_e
}

Inside of the if condition we have a return statement. We need to return the value 1 to the function calculuate_e

return 1; //return the value 1 to the function calculuate_e

Next, we use a #Mastery11 Use of else with a conditional if: // if the condition does not achieve on the other hand you will do this:

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

return n*factorial(n-1); //return the recursion value n!=n (n-1)! to the function calculuate_e like we did in the last WSQ6

}

Inside the else, we need to return the recursion value n!=n (n-1)! to the function calculuate_e like we did in the last WSQ6

return n*factorial(n-1); //return the recursion value n!=n (n-1)! to the function calculuate_e like we did in the last WSQ6

Furthermore, the program goes to the calculate_e function and we need a command of out data in form of text. In this text we show the authentic output of the value of e in the terminal interface #MasteryTopic04

cout<<e<<endl; //command of out data in form of text
//In this text we show the authentic output of the value of e in the terminal interface #MasteryTopic04

After that, we need this  math operation between ‘i’ and ‘1’ and stands for adding those values and saved it in int variable ‘i’

i++; //This is a math operation between 'i' and '1' and stands for adding
//those values and saved it in int variable 'i'

Next. we need a return statement. This return statement with this command you return the value of the previus operations of «calculuate_e» function inside of the variable float e in order to call it in THE MAIN FUNCTION

return e; //With this command you return the value of the previus operations of "calculuate_e" function inside of the
//variable float e in order to call it in THE MAIN FUNCTION

And i also resolved it by doing this video tutorial that i made reafirming these parts at this link :#WSQ12 Tutorial 13 16/04/17 Estimating e and WSQ12.cpp

Finally with the command ./a.out and WSQ12.cpp  and tells the terminal to run the C++ file:

eular

This code for solution will be here and in Github:

Link in Github: WSQ12.cpp

If you do not want the WSQ12.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

#include <cmath> //Library to call all the
//fuctions of math commands in languague
//C++ #MasteryTopic01

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

float factorial(int n) //This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have a float function with the name "factorial" and with int parameters n in order to
//have int results inside this function.
{
if(n==0) //#Mastery10 use of the if statement //This command allows the condition if the value of n
//is equal to 0 and when this condition will be true, the tasks inside of the statement will execute.
// In this case if the value of n is equal than 0 this will occur:
{

return 1; //return the value 1 to the function calculuate_e

}

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

return n*factorial(n-1); //return the recursion value n!=n (n-1)! to the function calculuate_e like we did in the last WSQ6

}

}

float calculuate_e (float precision) //This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have a FLOAT function with the name "calculuate_e" and with float parameter precision
//in order to have float results inside this function.

{

float e = 1; //Declarate float variable e initialized in 1
float previuse; //Declarate float variable previuse
int i=1; //Declarate float variable i initialized in 1

do //With this command do is utilized as a loop do execute all the
//instruccions that has this loop until that does not complete that the variable abs e-previuse>precision
// will be less or equal than the numerical value of precision variable while it is increasing one in one in this variable i
//inicialized from the value 1
{
previuse=e;//Give the value of e to variable previuse
e = e + 1/(factorial(i)); //This is a math operation between 'e' and
// the value of the float function factorial with the int parameter i and stands for
//do the sum of e and the inverse operation of the factorial function with the i parameter and saved it in the float
//variable 'e', this is the operation of the infinite series thanks to the recursion #Mastery16 Use of recursion for repetitive
//algorithms
cout<<e<<endl; //command of out data in form of text
//In this text we show the authentic output of the value of e in the terminal interface #MasteryTopic04
i++; //This is a math operation between 'i' and '1' and stands for adding
//those values and saved it in int variable 'i'
} while(abs (e-previuse)>precision);

return e; //With this command you return the value of the previus operations of "calculuate_e" function inside of the
//variable float e in order to call it in THE MAIN FUNCTION

//cout<< fixed <<setprecision(precision)<<endl; //float notation for manipulator of significant figures or decimal points after
//the decimal point. Hence, for example of precision=5 then the value will be setprecision(5) and with fixed after the decimal point
//this shows for the number of decimal places that the user wants. In consequence the value of variable e wil suffer after this operation
//cout<<e<<endl;

}

int main()

{
float precision; //Declarate float variable precision

cout<<"Specify the number of decimal points of accuracy: "; //#Mastery04 command of out data in form of text
cin>>precision; //#Mastery05 This command allows enter data for inputs. In this case this enters the
//value of variable 'precision'

cout<<"The estimation of e with "<<precision<<" decimal points is e = "<<calculuate_e (precision)<<endl; //command of out data in form of text
//In this text we show the authentic output of the value entered of precision in the terminal interface #MasteryTopic04

return 0; // This command allows label the final of the function main ()
}
Anuncio publicitario

Deja una respuesta

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Salir /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Salir /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Salir /  Cambiar )

Conectando a %s