My Cube Root program, a few problems?!

From: datastructure (bruss125_at_juno.com)
Date: 02/08/04


Date: 7 Feb 2004 16:59:28 -0800

I've written a cube root program, but have a few problems. Firs of
all how it works: Takes a # divides by 2 and 2^3 compares to the
inputed, loops untill less received # , gets the high and low and
finds the everage. The # is then multiplied by itself 3 tims = cube
rooted #.

1)Doesn't cube root 1 and 2 numbers.

Any suggestions on the code below, remember I can ONLY use <iostream>

// Ruslan Ben **** Cube Root **** program!

#include <iostream.h>

//********* Here the given # will be devided ************

double half(double num2, int num, double& high, double& low)
{
        num2 = num2 / 2;

        if(num2*num2*num2 == 0 || num2*num2*num2 == 1) // return an answer
early if a # entered is 1 or 0
        { // 0 / # = 0 0*0*0=0 == 0 and 1*1*1 == 1 and returns
early
                return num2;
        }
        
        else if(num2*num2*num2 > num) // if # is greater after division
        {
                high = num2; //set it as "high"
                num2=half(num2, num, high, low); //continues to look for a low #
                if(num2 == 1)
                        return 1;
                return num2;
        }
        else if(num2*num2*num2 < num) // and this will set the low for
averaging
        {
                low = num2;
                return 1;
        }
}

//*********** Finds the average of "High"+"Low"

double average(double& high, double& low, double num)
{
        double temp = (high + low)/2; //the two are averaged
        if(temp*temp*temp <= (num + 0.0000000001)&&temp*temp*temp >= (num -
0.0000000001)) //if the answer is found, return it
                return temp;
        if(temp*temp*temp > num) // if not and its too low, assign to 'low'
and recurse
        {
                high = temp;
                temp = average(high, low, num);
                return temp;//When returning through each recursion,this passes
final value through the loops
        }
        if(temp*temp*temp < num) //if too high, assign to 'high' and recurse
        {
                low = temp;
                temp = average(high, low, num);
                return temp;//When returning through each recursion,this passes
final value through the loops
        }
}
//---------------------------------------------------------------------

double cubeRoot(int num)
{
        double num2 = num; //num1 is copied to num2
        double high = 0.0; //holds "high" value
        double low = 0.0; //holds "Low" value
        num2 = half(num2, num, high, low);
        if(num2 != 1) //if half return a non-one answer, if mean it found a
perfect result by halving
                return num2;
        num2 = num;
        double ans = average(high, low, num2); //if not the high and low are
used
        return ans; // return final answer
}

//***************************************************************************************************************

void main()
{
        int num = 0; //# to be inputed
        double num2;
        char exit='y'; //exit condition
        

        while(exit == 'y'||exit =='Y')

        {
                cout << "\n\n\Enter a number to be cubeRooted (:";
                
                cin >> num;
                                                
                if(num<0)
                        cout<<"\n"<<"\n\t ***** Enter a positive number only *****
\n";//must enter a postive # only
                        else{
                
                num2 = cubeRoot(num);
                cout << "\nThe Answer is "<<num2 << ". Because " << num2 << " * " <<
num2 << " * " << num2 << " is " << num2*num2*num2 <<".\n"<<endl;}
                        
                        cout << "Do you want to continue(y or n)?";
            cin >> exit;

        }
        
}