Square Root Problem

Hello, I am building a program that calculates the square root of a user imputed number with a given algorithum, then it does the cmath sqrt and shows the difference. When the difference is below .00001, the program breaks. Here is what I have so far, I am stuck on how to get it past just looping endlessly with the same number and putting 0 in for thedifference.

#include <iostream>
#include <cmath>
using namespace std;

double functionsquareroot(double& newApprox, double numberGuess);

int main()
{
double numberGuess;
double newApprox;
double squareroot;
double thedifference;
double math;
double difference;

cout << "Please enter the value to be assesed:";
cin >> numberGuess;
newApprox = numberGuess/2;

cout << "\t \t \t Square Root Comparison Table" << endl;
cout << "\t \t \t ----------------------------" << endl;
cout << "Iteration Number" << " " << "Square Root from Algorithum";
cout << " " << "cmath Answer" << " " << "The Difference" << endl;

cout.setf(0,ios::floatfield);
cout.precision(6);
thedifference = newApprox - sqrt(numberGuess);
while(thedifference != .00001)
{
for(double squareroot = 0; squareroot < 1; squareroot++)
{
cout << '\t' << functionsquareroot(newApprox, numberGuess);
}
for(math = 0; math < 1; math++)
{
cout << '\t' << " " << sqrt(numberGuess);
}
for(thedifference = 0; thedifference < 1; thedifference++)
{
thedifference = newApprox - sqrt(numberGuess);
cout << '\t' << " " << thedifference;
}
cout << endl;
}
while(thedifference <= .00001)
{
break;
}
cout << endl;
system("pause");
return 0;
}

double functionsquareroot(double& newApprox, double numberGuess)
{
double previousApprox = newApprox;
newApprox = ((numberGuess/previousApprox) + previousApprox)/2;
return newApprox;
}
closed account (zwA4jE8b)
using ++ increments the var by 1.0, so if you need to step through at a higher precsion you will need to change all your ++ ops to something like
squareroot += .000001 math += .000001 thedifference += .000001
otherwise you go from 0 to 1, and nothing in between.

the output is horrendous with what you have but it at least increments correctly.

also while(thedifference != .00001) What if it never hit exactly .00001? you should use >= or <= depending on what you need it to do.
Last edited on
closed account (zwA4jE8b)
what is this loop here for

1
2
3
4
while(thedifference <= .00001)
{
break;
}


that one doesn't really do anything. you declared a while loop up top.
Alright, I changed the ++ to a higher precision, and took out the last while statement and changed the first while to >= so that once it hits below .00001 it stops. The output right now is just an endless stream of the square root of the number I put in, no change. I need it to take the last one it calculated, with the Algorithum ((numberGuess/previousApprox) + previousApprox)/2 and give me a more precise number. For example, with that Algorithum, the square root of 2 is 1.5, then it should take 1.5 and use it to get like 1.471 or something.
closed account (zwA4jE8b)
 
for(thedifference = 0; thedifference < 1; thedifference++)


this for loop constantly resets 'thedifference'. if you need to use a different variable name for the for loop.

thedifference = newApprox - sqrt(numberGuess); sets 'thedifference equal to something, then every time the for loop is called thedifference = 0 to 1. so .9999 is reached and then you return to the while loop and this is always greater than .000001

if you want to loop through ((numberGuess/previousApprox) + previousApprox)/2 then just put that algorithm in a a single loop.

also,i believe using math::sqrt() generates a rather precise answer. the precision is outputted is determined by ios::setprecision.

Hope that helps a little.
Last edited on
Ok, I changed that and it has the first iteration, but then it just outputs the next number endlessly. This is the code again revised.

#include <iostream>
#include <cmath>
using namespace std;

double functionsquareroot(double& newApprox, double numberGuess);

int main()
{
double numberGuess;
double newApprox;
double squareroot;
double thedifference;
double math;
double difference;

cout << "Please enter the value to be assesed:";
cin >> numberGuess;
newApprox = numberGuess/2;

cout << "\t \t \t Square Root Comparison Table" << endl;
cout << "\t \t \t ----------------------------" << endl;
cout << "Iteration Number" << " " << "Square Root from Algorithum";
cout << " " << "cmath Answer" << " " << "The Difference" << endl;

cout.setf(0,ios::floatfield);
cout.precision(6);
thedifference = newApprox - sqrt(numberGuess);
while(thedifference >= .00001)
{
for(double squareroot = 0; squareroot < 1; squareroot+=.00001)
{
cout << '\t' << functionsquareroot(newApprox, numberGuess);
}
for(math = 0; math < 1; math+=.00001)
{
cout << '\t' << " " << sqrt(numberGuess);
}
for(difference = 0; difference < 1; difference+=.00001)
{
thedifference = newApprox - sqrt(numberGuess);
cout << '\t' << " " << thedifference;
}
cout << endl;
}
cout << endl;
system("pause");
return 0;
}

double functionsquareroot(double& newApprox, double numberGuess)
{
double previousApprox = newApprox;
newApprox = ((numberGuess/previousApprox) + previousApprox)/2;
return newApprox;
}
closed account (zwA4jE8b)

Please enter the value to be assesed:43
                         Square Root Comparison Table
                         ----------------------------
Iteration Number Square Root from Algorithum cmath Answer The Difference
        11.75   7.70479 6.64287 6.55799 6.55744 6.55744 6.55744 6.55744 6.55744
6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744
6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744
6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744
6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744
6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744
6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744
6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744
6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744
6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744 6.55744
6.55744  6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         6.55744         6.55744         6.55744         6.55744         6.5574
         0       0       0       0       0       0       0       0       0
 0       0       0       0       0       0       0       0       0       0
 0       0       0       0       0       0       0       0       0       0
 0       0       0       0       0       0       0       0       0       0
 0       0       0       0       0       0       0       0       0       0
 0       0       0       0       0       0       0       0       0       0
 0       0       0       0       0       0       0       0       0       0
 0       0       0       0       0       0       0       0       0       0
 0       0       0       0       0       0       0       0       0       0
 0       0       0       0       0       0       0       0       0       0
 0

Press any key to continue . . .



I changed the increment to += .001 to get less results and this is what it is giving.

The first 5 numbers change, then it repeats. I don't know exactly what you are looking for but it appears as if it does what it does in the first 5 iterations of the first loop.


I didn't notice before, because I am not paying that much attention, but this loop just outputs the exact same thing every time..... sqrt( of any constant ) will always be the same thing.... you never change the variable numberGuess, which you shouldnt.
1
2
3
4
for(math = 0; math < 1; math+=.00001)
 {
 cout << '\t' << " " << sqrt(numberGuess);
 }


if you want to output the 3 different numbers sequentially then you need to

closed account (zwA4jE8b)
I think this is what you are looking for. Copy and paste it run it, and learn. I did! (learn that is, not copy and paste) :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

double functionsquareroot(double& newApprox, double numberGuess);

 int main()
 {
	 double numberGuess;
	 double newApprox;
	 double squareroot;
	 double thedifference;

	 cout << "Please enter the value to be assesed:";
	 cin >> numberGuess;
	 newApprox = numberGuess/2;

	 cout << "\t \t \t Square Root Comparison Table" << endl;
	 cout << "\t \t \t ----------------------------" << endl;
	 cout << "Iteration Number" << " " << "Square Root from Algorithum";
	 cout << " " << "cmath Answer" << " " << "The Difference" << endl;

	 cout.setf(0,ios::floatfield);
	 cout.precision(6);
	 thedifference = newApprox - sqrt(numberGuess);
	 cout << left << endl << setw(20) << "Approximation:" << setw(15) << "Actual:" << setw(15) << "The difference:" << endl;
	 while(thedifference >= .00001)
	 {
		for(squareroot = 0; squareroot < 1; squareroot += .1)
		{
			cout << left << setw(20) << functionsquareroot(newApprox, numberGuess) <<
			setw(15) << sqrt(numberGuess);
			thedifference = newApprox - sqrt(numberGuess);
			cout << setw(15) << thedifference << endl;
		}
	}
	cout << "Press any key to exit...";
	cin.ignore(1);
	cin.get();
	return 0;
 }

 double functionsquareroot(double& newApprox, double numberGuess)
 {
 double previousApprox = newApprox;
 newApprox = ((numberGuess/previousApprox) + previousApprox)/2;
 return newApprox;
 }


The output is formatted to be in a table form. It took me a while to see that is what you were trying to do but here you go.
Last edited on
closed account (zwA4jE8b)
This is the final output.


Please enter the value to be assesed:67
                         Square Root Comparison Table
                         ----------------------------
Iteration Number Square Root from Algorithum cmath Answer The Difference

Approximation:      Actual:        The difference:
        17.75    8.18535         9.56465
        10.7623  8.18535         2.57697
        8.49387  8.18535         0.30852
        8.19096  8.18535         0.00560313
        8.18535  8.18535         1.91644e-006
        8.18535  8.18535         2.23821e-013
        8.18535  8.18535         0
        8.18535  8.18535         0
        8.18535  8.18535         0
        8.18535  8.18535         0
        8.18535  8.18535         0
Press any key to exit...
Yeah, it is that just without the for loop. I was confused at how the for loop worked, and the program works better without it because the numbers of times it is repeated isn't as many as you would think. Thanks a bunch for the help!!!!!
closed account (zwA4jE8b)
You are welcome
closed account (zwA4jE8b)
It took me a while to realize but if you remove the for loop completely the program will then output until the desired precision is reached just using the while loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

double functionsquareroot(double& newApprox, double inroot);

 int main()
 {
	 double inroot;
	 double newApprox;
	 double squareroot;
	 double thedifference;


	 cout << "Please enter the value to be assesed:";
	 cin >> inroot;

	 cout << "\t \t \t Square Root Comparison Table" << endl;
	 cout << "\t \t \t ----------------------------" << endl;
	 cout << "Iteration Number" << " " << "Square Root from Algorithm";
	 cout << " " << "cmath Answer" << " " << "The Difference" << endl;

	 cout.setf(0,ios::floatfield);
	 cout.precision(6);
	
	 newApprox = inroot/2;
	 thedifference = newApprox - sqrt(inroot);
	 
	 cout << left << endl << setw(20) << "Approximation:" << setw(15) << "Actual:" << setw(15) << "The difference:" << endl;
	 
	 while(thedifference >= .00001)
	 {
			cout << left << setw(20) << functionsquareroot(newApprox, inroot) <<
			setw(15) << sqrt(inroot);
			thedifference = newApprox - sqrt(inroot);
			cout << setw(15) << thedifference << endl;
	}
	cout << "Press any key to exit...";
	cin.ignore(1);
	cin.get();
	return 0;
 }

 double functionsquareroot(double& newApprox, double inroot)
 {
	double previousApprox = newApprox;
	newApprox = (((inroot / previousApprox) + previousApprox) / 2);
	return newApprox;
 }
Last edited on
Topic archived. No new replies allowed.