Thread: Square Root Problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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.

  3. #3
    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.

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