So in this twelve week class I started with doing this WSQ10 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++. Futhermore, in this stage I have all the topics of the course from 1 to 20.

What I did for this numeric program is solving the problem to the user by creating a program with writing a function to calculate the square root of a number using the Babylonian method. You can search for that method, it will be easy to find. Hence, you can have it here in the survey that we did last week:

bab

Then, we need that this the function should receive a number and return floating point number. Obviously you should test your function, so create a main program that asks the user a value, calculates the square root and displays that.

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

ken bauer

Similar code provided by Xochitl96

The following photograph shows the solution to this problem:

ba1

ba2

ba3

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 and #WSQ09 Multipart Data and Files 10/03/17 and WSQ09.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.

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

Next, we need to use a Library to call all the functions of MATH of data in languague C++ #MasteryTopic01

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

After that, 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

Then we have the Begin of the program

int main() //Begin of the program

Next, we need the #Mastery04 command of out data in form of numeric value. With this command we give the value 16.23654 to square_root function.

cout<<square_root(16.23654); //#Mastery04 command of out data in form of numeric value. with this command we give the value 16.23654 to
//square_root function.

After that, the main program escapes from the main function, goes to a new function and we need to use this 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 «square_root» and with float parameter x in order to have float results inside this function.

float square_root(float x) //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 "square_root" and with float parameter x
//in order to have float results inside this function.

Next, we need to Declarate decimal float variable resp

float resp; //Declarate decimal float variable resp

After that, we need to declarate decimal float variable a initialized in 0

float a=0; //Declarate decimal float variable a initialized in 0

After that, we need to use this math operation between ‘resp’ and ‘x’ and stands for do the division between those values and saved it in float variable ‘resp’

resp=x/2; //This is a math operation between 'resp' and 'x' and stands for do the division between
//those values and saved it in float variable 'resp'

Next we use the #Mastery13 Use of loops with «while» and «do while». This command is the loop do-while that allows you in doing at least once of the instruccions in this loop and while this case of abs(a-resp) don´t be higher than 0.0001, it will execute forever every instructions of the loop label in this loop.

do
{
cout<<resp<<endl; //command of out data in form of text
//In this text we show the authentic output of the value of resp in the terminal interface #MasteryTopic04
a=resp; // This command allows giving the value of the declared decimal variable 'resp' to
//the declared decimal variable 'a'.
resp=(resp+x/resp)/2;//This is a math operation between 'resp' and 'x' and stands for do the babylonian method between
//those values and saved it in float variable 'resp'


}while( abs(a-resp) > 0.0001);

After that, we need a command of out data in form of text. In this text we show the authentic output of the value of resp in the terminal interface #MasteryTopic04

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

Next, we need this command allows giving the value of the declared decimal variable ‘resp’ to the declared decimal variable ‘a’.

a=resp; // This command allows giving the value of the declared decimal variable 'resp' to
//the declared decimal variable 'a'.

Furthermore, this is a math operation between ‘resp’ and ‘x’ and stands for do the babylonian method between those values and saved it in float variable ‘resp’

resp=(resp+x/resp)/2;//This is a math operation between 'resp' and 'x' and stands for do the babylonian method between
//those values and saved it in float variable 'resp'

Moreover, this command is the loop do-while that allows in doing at least 1 time of the instructions written in this loop and while in this case The value of abs(a-resp) don’t be higher to the value of 0.0001, it will execute forever all of the instrucccions lable in this loop.

while( abs(a-resp) > 0.0001); // This command is the loop do-while that allows in doing
//at least 1 time of the instructions written in this loop and while in this case
//The value of abs(a-resp) don't be higher to the value of 0.0001, it will execute forever
//all of the instrucccions lable in this loop.

Finally we have a return statement. With this command you return the value of the previus operations of «square_root» function inside of the in order to call it in THE MAIN FUNCTION.

return resp; //With this command you return the value of the previus operations of "square_root" function inside of the
//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 :#WSQ10 Tutorial 11 Babylonian method 21/03/17 and WSQ10.cpp

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

444

This code for solution will be here and in Github:

Link in Github:WSQ10.cpp

If you do not want the WSQ10.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 of data 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 square_root(float x) //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 "square_root" and with float parameter x
//in order to have float results inside this function.

{

float resp; //Declarate decimal float variable resp
float a=0; //Declarate decimal float variable a initialized in 0

resp=x/2; //This is a math operation between 'resp' and 'x' and stands for do the division between
//those values and saved it in float variable 'resp'

//Next we use the #Mastery13 Use of loops with "while" and "do while"
// This command is the loop do-while that allows you in doing at least once of
//the instruccions in this loop and while this case of
//abs(a-resp) don´t be higher than 0.0001, it will execute forever every instructions of the loop
//label in this loop.
do
{
cout<<resp<<endl; //command of out data in form of text
//In this text we show the authentic output of the value of resp in the terminal interface #MasteryTopic04
a=resp; // This command allows giving the value of the declared decimal variable 'resp' to
//the declared decimal variable 'a'.
resp=(resp+x/resp)/2;//This is a math operation between 'resp' and 'x' and stands for do the babylonian method between
//those values and saved it in float variable 'resp'


}while( abs(a-resp) > 0.0001); // This command is the loop do-while that allows in doing
//at least 1 time of the instructions written in this loop and while in this case
//The value of abs(a-resp) don't be higher to the value of 0.0001, it will execute forever
//all of the instrucccions lable in this loop.

return resp; //With this command you return the value of the previus operations of "square_root" function inside of the
//in order to call it in THE MAIN FUNCTION.

}



int main() //Begin of the program
{
cout<<square_root(16.23654); //#Mastery04 command of out data in form of numeric value. with this command we give the value 16.23654 to
//square_root function.

}

Anuncio publicitario

2 comentarios en “#WSQ10 Babylonian Method 21/03/17 and WSQ10.cpp

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