Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code to fix issue #865 #866

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions C++/Algorithms/Mathematical/SquareRoot.cpp
@@ -0,0 +1,51 @@
#include <iostream>
using namespace std;


//These 2 functions will accept user input and return the resulting Square Root of that user input


//Iterative Function
double sqrtIterative(double x) {
double sqrt = x * pow(0.3, log10(x)); //Square Root Value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are multiple errors right here. You have not declared log10 or pow anywhere in the scope. To resolve this, include the standard math library.

for (int i = 0; i < 5; i++)
sqrt = (sqrt + (x / sqrt)) * 0.5;
return sqrt;
}


//Recursive function
double sqrtRecursive(double num, double prev)
{
double next = (prev + num / prev) / 2; //Smallest Square Root Value
if (fabs(next - prev) < DBL_EPSILON * next)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fabs is not declared in this scope. To resolve DBL_EPSILON is not declared in the scope, either define a constant or write a function to return the value of DBL_EPSILON.

return next;
return sqrtRecursive(num, next);
}

int main(void) {



//Iterative Function

cout << "Iterative Function" << endl;
cout << "Please enter a number" << endl;
double iFn;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please have elaborate and meaningful variable declarations. Change the same wherever applicable.

cin >> iFn;
double answerIterative = sqrtIterative(iFn);
cout << answerIterative << endl;


//Recursive Function
cout << "Recursive Function" << endl;
cout << "Please enter 2 numbers, the first will be the square root number" << endl;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To demonstrate the recursive algorithm, instead of asking the user for a second number, you could define another constant or even generate a random number within a range specified and pass it as an argument to your sqrtRecursive() function thereby asking the user only to specify a single number that which they desire to find the square root of.

double rFp;
double rFn;
cin >> rFn;
cin >> rFp;
double answerRecursion = sqrtRecursive(rFn, rFp);
cout << answerRecursion << endl;


}