Can someone explain this question
Author |
Message |
chromium
|
Posted: Sat Mar 01, 2014 7:51 pm Post subject: Can someone explain this question |
|
|
Im not understanding what this questions is asking:
Please can someone explain it to me. Thanks. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Panphobia
|
Posted: Sat Mar 01, 2014 8:13 pm Post subject: RE:Can someone explain this question |
|
|
It seems pretty simple, I just solved it. You just need to implement a recursive function that uses that approximation expression. So basically if you know what recursion is, you know you want the function to stop calling itself at some time, so this is your base case, in the question it says exactly when to stop. So
code: |
if(TARGET HAS BEEN MET)
return answer
else
RECURSIVE CALL |
|
|
|
|
|
|
chromium
|
Posted: Sat Mar 01, 2014 8:34 pm Post subject: Re: RE:Can someone explain this question |
|
|
Panphobia @ Sat Mar 01, 2014 8:13 pm wrote: It seems pretty simple, I just solved it. You just need to implement a recursive function that uses that approximation expression. So basically if you know what recursion is, you know you want the function to stop calling itself at some time, so this is your base case, in the question it says exactly when to stop. So
code: |
if(TARGET HAS BEEN MET)
return answer
else
RECURSIVE CALL |
But what am i supposed to be returning? The cube root of 'a'? |
|
|
|
|
|
Panphobia
|
Posted: Sat Mar 01, 2014 8:39 pm Post subject: RE:Can someone explain this question |
|
|
which is t, so yes! |
|
|
|
|
|
chromium
|
Posted: Sat Mar 01, 2014 8:48 pm Post subject: Re: RE:Can someone explain this question |
|
|
Panphobia @ Sat Mar 01, 2014 8:39 pm wrote: which is t, so yes!
K Ive done this so far:
code: | public static double betterCubeRoot (double a, double t)
{
double tCubed = Math.pow (t, 3);
double dif = Math.abs (tCubed - a);
double eq = ((a / (t * t)) + 2 * t) / 3;
if (dif < 0.001)
{
return eq;
}
else
{
return betterCubeRoot (eq, t) ;
}
} |
Im still kind of unsure of what the values of a and t are. |
|
|
|
|
|
Panphobia
|
Posted: Sat Mar 01, 2014 8:50 pm Post subject: RE:Can someone explain this question |
|
|
a is the number you're trying to get the cube of
t is the cube root approximation
so with that out there, looking at your recursive call, is it right? |
|
|
|
|
|
chromium
|
Posted: Sat Mar 01, 2014 8:52 pm Post subject: Re: RE:Can someone explain this question |
|
|
Panphobia @ Sat Mar 01, 2014 8:50 pm wrote: a is the number you're trying to get the cube of
t is the cube root approximation
so with that out there, looking at your recursive call, is it right?
I dont think so. It isnt correctly returning the cube root of a. What initial value should t have? |
|
|
|
|
|
Panphobia
|
Posted: Sat Mar 01, 2014 8:58 pm Post subject: Re: Can someone explain this question |
|
|
For this to work t can start out as any positive number, but anyway. Look at your logic right now, go through your code and figure out where the problem is. You are passing it a value t in your main function/method, but is t ever changed? Also a is supposed to stay constant since it is the number you are comparing to t^3. Look at your base case too, you are checking if Math.abs(t*t*t-a) < 0.001, so why are you returning eq? the difference was calculating with t, not eq. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Sat Mar 01, 2014 9:06 pm Post subject: RE:Can someone explain this question |
|
|
Read through the question again, it tells you. |
|
|
|
|
|
|
|