Thread: Square Root Problem

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    5

    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

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    maybe I don't understand, but why not use sqrt? Also, I don't like this line:
    Code:
    x = a = userInput
    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;

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    Last edited by anon; 10-19-2006 at 03:46 PM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    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

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    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.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    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.

  8. #8
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    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.

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Square Root Problem.
    By thekautz in forum C++ Programming
    Replies: 8
    Last Post: 11-26-2008, 12:38 PM
  2. program to calculate the square root
    By saszew in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 12:53 PM
  3. Making my own square root function
    By Alexthunder in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2006, 07:36 PM
  4. square root alternative
    By FH|RogueHunter in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2005, 10:17 PM
  5. Advanced: Calculate square root with templates
    By Sang-drax in forum Contests Board
    Replies: 32
    Last Post: 09-29-2004, 04:37 PM