well, my assignment is to create a program that will compute a square root of a number without using the sqrt() function in the cmath lib.

here is the direct assignment guideline.
http://community.tncc.edu/faculty/lewis/csc201/pdf/lab4b.pdf

and my program is about 95% written out (minus the comments)... but it keeps getting stuck in a loop at then end output.

can anyone help?

heres my code:

``````//SQUARE ROOT GUESTIMATOR
//-----------------------
//NECESSARY PREPROCESSOR DIRECTIVES
#include <iostream>
#include <iomanip>
using namespace std;

//VOID FUNCTIONS
void getValues(float&, float&, bool&);					//WILL RECIEVE INPUT VALUES FROM USER.
void swap(float&, float&);								//WILL SWAP NEXT GUESS AND LAST GUESS IF NEEDED.
void newGuess(float, float, float&);					//WILL CALCULATE NEXT GUESS.

//MAIN PROGRAM
int main()
{
//DECLARED VARIABLES AND FUNCTIONS
float ng=0.0;						//VARIABLE FOR "NEXT GUESS"
float lg=0.0;						//VARIABLE FOR "LAST GUESS"
float n=0.0;						//VARIABLE FOR THE SQUARE ROOT OF A NUMBER
float difference=100.0;
bool dataok = false;

getValues(n,lg,dataok);
while(!(dataok=false))
{
difference= (ng - lg);
if ((difference > 0.005) || (difference < -0.005))
{
newGuess(n,lg,ng);
cout<< "The next guess for the square root is: " <<ng;
swap(lg , ng);
cout<<endl;

}
else
{
cout<< "The square root of the number given is: " <<ng <<endl;
dataok=true;
}
}
return 0;
}

//-----------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------
//DESCRIPTION:GET VALUES VOID FUNCTION
//PARAMETERS: MODIFY BY REFERENCE THE VARIABLES "N" (NUMBER) AND "LG" (LAST GUESS)
//IN: "N" (NUMBER) AND "LG" (LAST GUESS) BY REFERENCE
//OUT:
//INOUT:
//RETURNS: NONE
void getValues(float& a, float& b, bool& dataok)																//WILL RECEIVE INPUT VALUES FROM USER AND ASSIGN THEM TO VARIABLES
{
cout<< "Hello, Welcome to the C++ Square Root Calculator!" <<endl;
cout<< "Please enter the number you would wish to find the square root of: ";
cin>> a;																								//INPUT VALUE FROM USER FOR NUMBER TO FIND SQUARE ROOT OF
cout<< endl <<"Now please enter your guess of the square root: ";
cin>> b;																								//INPUT VALUE FROM USER FOR THE LAST GUESS
while(!((a>0) && (b<= (a/2))))
{
dataok=false;
getValues(a,b,dataok);
}
dataok=true;
}
//DESCRIPTION:WILL SWAP TWO FLOATS
//PARAMETERS: FLOAT A AND FLOAT B
// IN:
//OUT:
// INOUT:
//RETURNS:
void swap (float& a, float& b)												//USED TO CHANGE NEXT GUESS TO LAST GUESS BY REFERENCE
{
float temp;
temp = a;
a = b;
b = temp;
}
void newGuess(float number, float lastGuess, float& nextGuess)					//CALCULATES THE VALUE FOR THE VARIABLE: NEXT GUESS
{
nextGuess =((lastGuess + number/lastGuess)/2);
}``````

Thanks for any help!!!

~DJ

2
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by vmanes

What's wrong with this bit:

``````getValues(n,lg,dataok);
while(!(dataok=false))
{``````

Once you figure that out, you really need to go back and revisit your logic for the loop. The dataok variable looks to be doing two different jobs - somehow telling me the input values are ok to use, and being use to control the loop. Your loop logic is backwards.

Consider

``````bool done = false;
while ( ! done )
{
if( gotta do more work )
{
do it
}
else
{
done = true;
}
}``````

Also, as you have it, dataok is going to be true when you enter the loop, and where you think you're ending the loop, you set it to be----true.

bah... haha.

for boolean tests, you need to have it "= = " not just "=".

silly me!!! i knew it was a simple mistake. i have gone back to my loops and fixed the done and dataok problems. now my problem is that the loop is going one too many times.

any ideas?

here is my updated running code:

``````//DANIEL DORER
//CSC201
//LAB 4
//SQUARE ROOT GUESTIMATOR
//-----------------------
//NECESSARY PREPROCESSOR DIRECTIVES
#include <iostream>
#include <iomanip>
using namespace std;

//VOID FUNCTIONS
void getValues(float&, float&, bool&);					//WILL RECIEVE INPUT VALUES FROM USER.
void swap(float&, float&);								//WILL SWAP NEXT GUESS AND LAST GUESS IF NEEDED.
void newGuess(float, float, float&);					//WILL CALCULATE NEXT GUESS.

//MAIN PROGRAM
int main()
{
//DECLARED VARIABLES AND FUNCTIONS
float ng=0.0;						//VARIABLE FOR "NEXT GUESS"
float lg=0.0;						//VARIABLE FOR "LAST GUESS"
float n=0.0;						//VARIABLE FOR THE SQUARE ROOT OF A NUMBER
float difference=0.0;
bool dataok = false;
bool done = false;
int count=0;

getValues(n,lg,dataok);
while((done==false))
{
newGuess(n,lg,ng);
difference = (lg - ng);

if ((difference > 0.005) || (difference < -0.005))
{
cout<< "The last guess was: " <<lg <<endl;

cout<< "The next guess for the square root is: " <<ng;
swap(lg , ng);
cout<<endl;
done=false;
count++;

}
else
{
cout<< "The square root of the number given is: " <<ng <<endl;
cout<< "and it took " <<count <<" guesses" <<endl;
done=true;
}
}
return 0;
}

//-----------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------
//DESCRIPTION:GET VALUES VOID FUNCTION
//PARAMETERS: MODIFY BY REFERENCE THE VARIABLES "N" (NUMBER) AND "LG" (LAST GUESS)
//IN: "N" (NUMBER) AND "LG" (LAST GUESS) BY REFERENCE
//OUT:
//INOUT:
//RETURNS: NONE
void getValues(float& a, float& b, bool& dataok)																//WILL RECEIVE INPUT VALUES FROM USER AND ASSIGN THEM TO VARIABLES
{
cout<< "Hello, Welcome to the C++ Square Root Calculator!" <<endl;
cout<< "Please enter the number you would wish to find the square root of: ";
cin>> a;																								//INPUT VALUE FROM USER FOR NUMBER TO FIND SQUARE ROOT OF
cout<< endl <<"Now please enter your guess of the square root: ";
cin>> b;																								//INPUT VALUE FROM USER FOR THE LAST GUESS

while(!((a>0) && (b>0) && (b<= (a/2))))
{
dataok=false;
cout<< "Hello, Welcome to the C++ Square Root Calculator!" <<endl;
cout<< "Please enter the number you would wish to find the square root of: ";
cin>> a;																								//INPUT VALUE FROM USER FOR NUMBER TO FIND SQUARE ROOT OF
cout<< endl <<"Now please enter your guess of the square root: ";
cin>> b;
}
dataok=true;
}
//DESCRIPTION:WILL SWAP TWO FLOATS
//PARAMETERS: FLOAT A AND FLOAT B
// IN:
//OUT:
// INOUT:
//RETURNS:
void swap (float& a, float& b)												//USED TO CHANGE NEXT GUESS TO LAST GUESS BY REFERENCE
{
float temp;
temp = a;
a = b;
b = temp;
}
void newGuess(float number, float lastGuess, float& nextGuess)					//CALCULATES THE VALUE FOR THE VARIABLE: NEXT GUESS
{
nextGuess =(0.5f*(lastGuess + number/lastGuess));
}``````

A subtle logic problem, I think. You are computing the difference between the previous guess and the current guess which tells you how much they changed. If that change was significant, you do another round. But, the guess itself may have been within tolerance of the square root value you seek.

So, your output shows a next to last value that is within your .005 tolerance of being the square root, but had a larger difference with the previous guess. Another round ensues.

What's important - the amount of change or the closeness to square root value?

A better test is - subtract the guess^2 from the original value, and test that difference.