so i basically got my case ones down
using logic and newton method
Code:
void Roots(double a, int b)
{
double x,d = 10;
int c;
switch(b)
{
case 2:
for(x = 0; x < a; x = x + .001)
{
if ( a / x >= x - 0.001 && a / x <= x + 0.001 )
{
cout << "\n\tThe square root of " << a << " = " << fixed << setprecision(3) << x;
}
}
break;
case 3:
if ( a > 0 )
{
for (c = 0; c < 15; c++)
{
d = d - (d*d*d - a)/(4*d*d);
}
cout << "\n\tThe third root of " << a << " = " << fixed << setprecision(3) << d ;
}
else
{
a = a * -1;
for (c = 0; c < 15; c++)
{
d = d - (d*d*d - a)/(4*d*d);
}
cout << "\n\tThe third root of " << a * -1 << " = " << fixed << setprecision(3) << d * -1;
}
break;
case 4:
for (c = 0; c < 15; c++)
{
d = d - (d*d*d*d - a)/(4*d*d*d);
}
cout << "\n\tThe 4th root of " << a << " = " << fixed << setprecision(3) << d;
break;
default:
cout << "\n\tYou didn't assign that root";
}
}
but i haven't learned overloading yet, is it possible to use a logical iterator to calculate the nth root without using newton method?
oh a side note, i was practicing ways of doing it differently
Code:
case 4:
/*
for (c = 0; c < 15; c++)
{
d = d - (d*d*d*d - a)/(4*d*d*d);
}
cout << "\n\tThe 4th root of " << a << " = " << fixed << setprecision(3) << d;
break;
*/
for (x = 0; x < a; x++)
{
if ( x * x * x * x <= a && (x + 1 ) * (x + 1)*(x + 1)*(x + 1) > a)
{
for ( y = 0; y < 1; y = y + .1)
{
x = x + y;
if ( x * x * x * x <= a && (x + .1 ) * (x + .1) * (x + .1) * (x + .1) > a)
{
for (z = 0; z < .1; z = z +.01)
{
x = x + z;
if ( x * x * x * x <= a && (x + .01 ) * (x + .01) * (x + .01) * (x + .01) > a)
{
for (Third = 0; Third < .01; Third = Third + .001)
{
x = x + Third;
if (x * x * x * x <= a && (x + .001 ) * (x + .001) * (x + .001) * (x + .001) > a)
{
cout << "\n\tThe 4th root of " << a << " = " << x;
}
}
}
}
}
}
}
}
break;
This worked for perfect 4th roots like 81 and 16. But when its a number like 4 or 20 or etc.., it returns nothing. Why?