Babylonian method

--Originally published at my programming blog

So for this WSQ I needed to make a function that asked the user for a number and then return a floating point in which it calculated the square root of the number the user entered, but the square root needs to be calculated with the Babylonian method.

First, here’s a picture of the Babylonia method (so  you can have an idea of what I did in my code):

Screen Shot 2017-04-05 at 12.45.02 PM

(Obtained from Wikipedia)

Also check this tutorial, it helped me a lot!

So this is what I did:

  1. I included the cmath library because I’m also going to print the square root of the number to check if the code is correct.
  2. I made a function (a float) called SquareRoot and in the parameters I included the double x (which will be the number the user will enter in the int main).
  3. Then I made a double called Error and I equaled it to 0.0001.
  4. Then for the Babylonian method you need to have a reference number, so I declared the double y which I assigned to the value of the double x.
  5. Then I did a while loop that while the double y minus x over y is bigger than the error I declared earlier this would repeat: I wrote the formula for the Babylonian method, that is that y will be equal to y plus x over y and that answer over 2. (This will be an intermediate result of the square root).
  6. Then once the condition of the while loop doesn’t happen the function will print “The final result is” and then return the value of y.
  7. Finally in the int main, I ask the user for a number, which is the declared double x and then call the function.
  8. After all this I print
    Screen Shot 2017-04-05 at 12.41.57 PM
    Screen Shot 2017-04-05 at 12.29.18 PM
    Continue reading "Babylonian method"

Homework 10: Babylonian Method

--Originally published at Let's CODE

This function will calculate a square root through the Babylonian Method. First of all, I would like to explain how the method works. You have to establish a first number that is “aproximate” to the real squareroot, doesn’t matter if that number is equal to one, because we are going to make some mathematical operations. … Continue reading Homework 10: Babylonian Method

#WSQ10 Babylonian Method 21/03/17 and WSQ10.cpp

--Originally published at Solving Problems with Programming

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.

444
Continue reading "#WSQ10 Babylonian Method 21/03/17 and WSQ10.cpp"