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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return next; | ||
return sqrtRecursive(num, next); | ||
} | ||
|
||
int main(void) { | ||
|
||
|
||
|
||
//Iterative Function | ||
|
||
cout << "Iterative Function" << endl; | ||
cout << "Please enter a number" << endl; | ||
double iFn; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
double rFp; | ||
double rFn; | ||
cin >> rFn; | ||
cin >> rFp; | ||
double answerRecursion = sqrtRecursive(rFn, rFp); | ||
cout << answerRecursion << endl; | ||
|
||
|
||
} |
There was a problem hiding this comment.
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
orpow
anywhere in the scope. To resolve this, include the standard math library.