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;
cout<< "Please check your values and re-enter"<<endl<<endl;
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