-
Square Root Problem
The basic structure of the program is this:
1. issue an interactive prompt for the user to enter a non-negative number,
2. verify that the input is indeed non-negative, or that the user wishes to end the program,
3. calculate the square root of the input value, using the method to be described shortly,
4. print some information about how well the method worked,
5. go back to step 1.
Here is the square root method. Suppose the input value is in the variable a, type double. For convenience and to explain what’s going on, we’ll write the value a = b^2, but we don’t know the value of b yet (its value is sqrt(a) but we’re trying to calculate b ourselves). Start to compute a sequence of values xN by setting x0 = a. Once we know xn we can calculate xn+1 by the rule
xn+1 = ½ (xn + a/xn)
and then we just keep going until the sequence xN converges to b.
I have decided to use nested loops for the program but they aren't really working...
Code:
#include <iostream>
#include <cmath> // Needed for rand operations.
int main ()
{
char quit='N';
double userInput, estimate, sqrt_answer, y,x,a;
while (toupper(quit) != 'Y')
{
cout <<"enter non zero number *****:"<<flush;
cin >> userInput;
If (userInput <=0)
{
cout <<"invalid. would you like to quit?(y/n):"<<flush;
cin>> quit;
}
x = a = userInput;
y= 0;
//sqrt
lasty= 0;
for(i=0;i<=15;i++)
{
y=0.5*(x+a/x);
x=y;
if (y == lasty)
break;
lasty=y;
I dont really understand what to do next to make this work....
Please help me out..
Thanks
-
maybe I don't understand, but why not use sqrt? Also, I don't like this line:
I like it like this:
Code:
a = userInput;
x = a;
Secondly, both your while and last for loops have no closing parentheses. Also, if the user inputs a wrong anwer, it will be delt with at least once. You should put break after this:
Code:
cout <<"invalid. would you like to quit?(y/n):"<<flush;
cin>> quit;
-
Code:
#include <iostream> //everything is in std:: namespace
#include <cmath> // Needed for rand operations.???
int main ()
{
char quit='N';
double userInput, estimate, sqrt_answer, y,x,a; //unused-unneeded variables
while (toupper(quit) != 'Y')
{
cout <<"enter non zero number *****:"<<flush;
cin >> userInput;
If (userInput <=0) //C++ is case sensitive
{
cout <<"invalid. would you like to quit?(y/n):"<<flush;
cin>> quit;
} //but you still go on calculating the sqrt
x = a = userInput;
y= 0;
//sqrt
lasty= 0; //undeclared
for(i=0;i<=15;i++) //undeclared i
{
y=0.5*(x+a/x);
x=y;
if (y == lasty)
break;
lasty=y;
/*close the loop, print lasty (or y, whichever), ask if user wants to quit, close
loop and main, and hand it in*/
If you fix the many errors, handling negative input is your biggest problem. You really can't use break, because that would exit the main loop and quit, while the user may have replied they want to continue. So you might rather need the continue keyword. Or just put everything that follows into an else block.
-
hey
yeah, i know... i could use the sqrt but this problem has to be dealt using that formula
and thus y=0.5*(x+a/x)
now what i dont know is how to close off this program using the for or whatever loops so it actually works
-
In addition, requirement 4 seems to indicate that you might also need to display, how many iterations it took to reach the result (probably 15 in most cases, because I don't trust comparing doubles with ==), and/or comparison of the result with sqrt() result.
Loops (blocks) are usually closed using the } bracket. I actually thought you didn't notice you had left out a part :)
-
here's my complete code (untested, so you compile):
Code:
#include <iostream>
#include <cmath>
using namepace std;
int main (int argc, char **argv)
{
char quit = 'N';
double userinput, y, x, a, lasty; //All of our variables
while (toupper(quit) != 'Y')
{
cout << "Please enter a non-zero number *****:" << flush;
cin >> userinput; //Get the number to be square rooted
if (userinput <= 0) //Is it a nonvalid number?
{
cout << "The number you entered was invalid. Would you like to quit (y/n)?" << flush;
cin >> quit; //Does the user want to quit?
continue; //If he wants to quit, then quit. Else, continue.
}
a = userinput;
x = a;
/*********Square root loop*************/
lasty = 0; //Null out lasty
for (int i = 0; i <= 15; i++)
{
y = 0.5*(x+a/x); //This is our actual formula
x = y;
if (y == lasty)
{
break; //Our formula has reached its end prematurely (under 15 loops)
}
lasty = y
}
cout << "The square root is " << y << "." << endl;
cout << "Would you now like to quit (y/n)?" << flush;
cin >> quit;
}
cout << "Ending...." << endl;
}
I hope that helps you.
-
Hey.
Thank you so much.. that works and the loop is complete...
anon had a point... requirement 4 does say i need to give some information about how many loops it would have taken it to find the root... how could i do this... (print how many loops it took)
you've really helped me a lot and i appreciate it.
-
Stick an int n = 0 at the start, then in the loop put n++;
Each time the loop goes through n is incremented, thus n == number of times looped.
-
You're welcome! I hope this has been a good lesson on those little things you need to do the make C or C++ work! :)