“Double S, x, z, etc…” Is that not initializing? |
This bit :
18 19 20 21 22
|
double S = -1.0; // purposely made these negative, so they
double z = -100.0; // are obviously wrong, if you get negative answers
loop, // then you didn't assign a proper value to these
double StepSize = 0.0 // How much the values change after each loop
double b = -1000.0; // put comments for all these variables, so we know what they are
|
Realise the difference between declaring & initialising. You had declarations, I was showing declaration & initialising by assigning a value to the variable at the same time. Notice I specified the type (double) for each one.
I honestly don’t understand what you mean by “Consider writing your own function to put your code into, make sure it returns a double.” |
Instead of having all the code in
main()
, declare & define your own function (MySqrt say) which does your implementation of sqrt. That way you can call it multiple times (like in a loop) without having to repeat code.
I thought that by setting my loop = 9999999 I had an end condition. |
Yes, it is an end condition, and yes I am trying to get you to use a different end condition. This was the explanation of how to do that:
TheIdeasMan wrote: |
---|
We had sqrt(101) which is sqrt(10e2), the answer is about 10e1, which is a number that has 2 sf before the decimal place. We want 5 sf, so a loop has to stop at 10e-3.
Now consider sqrt(10e4), the answer is about 10e2 - which is a number that has 3 digits before the decimal place, so a loop would have to stop at 10e-2 to get 5 sf.
Now consider sqrt(10 e6)), the answer is about 10e3 - which is a number that has 4 digits before the decimal place, so a loop would have to stop at 10e-1 to get 5 sf.
Can you see the pattern forming here? |
I thought you said you understood that, but do you really?
I know that 10.049 is roughly 1/10 of 101 or 10^2, but how would I use that information to put that in an end variable. >I’m not aloud to use the log base function.< |
It's the value of the exponent you need to look at - there is a pattern relating the exponent and achieving 5sf.
TheIdeasMan wrote: |
---|
To find the exponent of a given number, count how many times you need to divide it by 10 until it is between 1 and 10 (use a while loop to do that, and put it in a function), ..... |
So you can't use a log function, I said it may be cheating a little.
I’ve heard of using setprecision but I don’t think that’s what your talking about since that would break under small numbers. And I might be able to use scientific format but I believe my teacher would tell me to change it. |
Yes,
setprecision
and
setw
is what I meant. If your number is 12345000, then you can work out how digits that is, assign it to a variable, then use that variable as an argument to the setw function.
I probably forgot to mention you should read the tutorials, articles, information & reference sections at the top left of this page - there is a wealth of information there - with examples for each function. I should have said that right at the start.
Hope this helps much more.