programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# can't get it to print remainder in double

aaron butcher
Ranch Hand
Posts: 32

Actual output Expected output
-------------------------------------------------------------------------------------
Enter the initial mass: 100.0 Enter the initial mass: 100.0
Enter the rate of decay as a percent: 5.0 Enter the rate of decay as a percent: 5.0
95.00 95.00
- 90.00 90.25
- 85.00 85.74
- 80.00 81.45
- 75.00 77.38
- 70.00 73.51
- 65.00 69.83
- 60.00 66.34
- 55.00 63.02
- 50.00 59.87
- 45.00 56.88
- 40.00 54.04
- 35.00 51.33
- 30.00 48.77
- 25.00 14
- 20.00
- 15.00

Carey Brown
Saloon Keeper
Posts: 3310
46
Line 13 will always be true.

Carey Brown
Saloon Keeper
Posts: 3310
46
Line 14 - you are reducing mass by decayRate, not by the percent of mass as defined by decayRate.

Carey Brown
Saloon Keeper
Posts: 3310
46
Carey Brown wrote:Line 13 will always be true.

Unless mass is zero.

aaron butcher
Ranch Hand
Posts: 32
Carey Brown wrote:
Carey Brown wrote:Line 13 will always be true.

Unless mass is zero.

so how do i fix these sir.

Carey Brown
Saloon Keeper
Posts: 3310
46
You could start by implementing:
"Stop when the current mass is less than half the original mass of the substance."
You have the current mass, but you don't have the original mass.

aaron butcher
Ranch Hand
Posts: 32
Carey Brown wrote:You could start by implementing:
"Stop when the current mass is less than half the original mass of the substance."
You have the current mass, but you don't have the original mass.

like this

Carey Brown
Saloon Keeper
Posts: 3310
46
aaron butcher wrote:
Carey Brown wrote:You could start by implementing:
"Stop when the current mass is less than half the original mass of the substance."
You have the current mass, but you don't have the original mass.

like this

What if 100.0 is not what you answered to the prompt?

aaron butcher
Ranch Hand
Posts: 32

Carey Brown
Saloon Keeper
Posts: 3310
46
aaron butcher wrote:

Getting closer. Not sure why you're subtracting decayRate at this point, and, decayRate is a multiplier as a percent which is not what you have.
In the mean time, replace both the for() loop and the if() with a single while() that implements your 'stop' requirement.

aaron butcher
Ranch Hand
Posts: 32

i need it to be a for loop so i can give the count of years it took at the end of it
still not getting what to do any hints

Carey Brown
Saloon Keeper
Posts: 3310
46
aaron butcher wrote:

i need it to be a for loop so i can give the count of years it took at the end of it
still not getting what to do any hints

Alright, if you want to use a for loop then rewrite the while (middle) portion of the loop to not depend on 'i' and instead implement the requirements.

aaron butcher
Ranch Hand
Posts: 32
Carey Brown wrote:
aaron butcher wrote:

i need it to be a for loop so i can give the count of years it took at the end of it
still not getting what to do any hints

Alright, if you want to use a for loop then rewrite the while (middle) portion of the loop to not depend on 'i' and instead implement the requirements.

I'm lost how to do that.

Carey Brown
Saloon Keeper
Posts: 3310
46
aaron butcher wrote:
Carey Brown wrote:
aaron butcher wrote:

i need it to be a for loop so i can give the count of years it took at the end of it
still not getting what to do any hints

Alright, if you want to use a for loop then rewrite the while (middle) portion of the loop to not depend on 'i' and instead implement the requirements.

I'm lost how to do that.

Requirement: Stop when the current mass is less than half the original mass
So, if you need a conditional expression that says to continue while ...
then you'd have to reverse the conditional laid out in your requirement.

Also, 'i' is a terrible name for your variable. Use something like 'count' instead, it's more descriptive.

A for() loop has three segments: for( [initialization] ; [while expression] ; [end-of-loop expression] ).

aaron butcher
Ranch Hand
Posts: 32
Carey Brown wrote:
aaron butcher wrote:
Carey Brown wrote:
aaron butcher wrote:

i need it to be a for loop so i can give the count of years it took at the end of it
still not getting what to do any hints

Alright, if you want to use a for loop then rewrite the while (middle) portion of the loop to not depend on 'i' and instead implement the requirements.

I'm lost how to do that.

Requirement: Stop when the current mass is less than half the original mass
So, if you need a conditional expression that says to continue while ...
then you'd have to reverse the conditional laid out in your requirement.
Also, 'i' is a terrible name for your variable. Use something like 'count' instead, it's more descriptive.

A for() loop has three segments: for( [initialization] ; [while expression] ; [end-of-loop expression] ).

Carey Brown
Saloon Keeper
Posts: 3310
46
aaron butcher wrote:

aaron butcher
Ranch Hand
Posts: 32
Carey Brown wrote:
aaron butcher wrote:

so this

Carey Brown
Saloon Keeper
Posts: 3310
46
aaron butcher wrote:

You still have the if() statement.
You are initializing currentMass inside of the loop so it will be reset each time. I don't think that's what you want.

aaron butcher
Ranch Hand
Posts: 32
Carey Brown wrote:
aaron butcher wrote:

You still have the if() statement.
You are initializing currentMass inside of the loop so it will be reset each time. I don't think that's what you want.

ok if is gone

Carey Brown
Saloon Keeper
Posts: 3310
46
aaron butcher wrote:

so this

You are thrashing.
for( count=0 ; [replace-me] ; count++ )

There's nothing that says the 'count' variable needs to appear anywhere in the [replace-me] portion.

I think it might be clearer to you if you implement it as a while() loop

Carey Brown
Saloon Keeper
Posts: 3310
46
Requirement: Stop when the current mass is less than half the original mass
But a while statement needs: Continue when ...
You need to massage the stop-when expression so that it works as a continue-when expression.

aaron butcher
Ranch Hand
Posts: 32
int count = 0;

Carey Brown
Saloon Keeper
Posts: 3310
46
aaron butcher wrote: int count = 0;

Getting there. The problem is with your "currentmass < mass/2" expression, that is the expression used to STOP but you need one for GO.
And for clarity I would use orgMass in the expression just like your requirements ask for.

aaron butcher
Ranch Hand
Posts: 32

ok got it to print right except for the remainder

Carey Brown
Saloon Keeper
Posts: 3310
46
aaron butcher wrote:

ok got it to print right except for the remainder

Major progress. I'm glad you did away with 'mass' and are only using 'currentmass'.
Your while expression still needs a minor tweak: The opposite of A<B is A>=B.
you should work on your "... - decayRate" formula now.
If your mass is 95 then how would you calculate 5% of 95 ?

aaron butcher
Ranch Hand
Posts: 32
Carey Brown wrote:
aaron butcher wrote:

ok got it to print right except for the remainder

Major progress. I'm glad you did away with 'mass' and are only using 'currentmass'.
Your while expression still needs a minor tweak: The opposite of A<B is A>=B.
You should work on your "... - decayRate" formula now.
If your mass is 95 then how would you calculate 5% of 95 ?

well i would think 95/5 but not sure how to turn that around to code

Carey Brown
Saloon Keeper
Posts: 3310
46
aaron butcher wrote:
Carey Brown wrote:
If your mass is 95 then how would you calculate 5% of 95 ?

well i would think 95/5 but not sure how to turn that around to code

No. You need to look up some of your old algebra.
1 % = 1.0 / 100.0
2 % = 2.0 / 100.0
...
Any idea now how to calculate 5% of 95 ?

aaron butcher
Ranch Hand
Posts: 32
Carey Brown wrote:
aaron butcher wrote:
Carey Brown wrote:
If your mass is 95 then how would you calculate 5% of 95 ?

well i would think 95/5 but not sure how to turn that around to code

No. You need to look up some of your old algebra.
1 % = 1.0 / 100.0
2 % = 2.0 / 100.0
...
Any idea now how to calculate 5% of 95 ?

5 % = 5.0 / 95.0

Campbell Ritchie
Marshal
Posts: 56529
172
Carey Brown wrote: . . .
Unless mass is zero.
And if you wait long enough, mass will become 0.0.
Remember a double has a maximum value of (2 − 2⁻⁵²) × 2¹⁰²³ and a minimum non‑zero value of 2⁻¹⁰⁷⁴, so after 1024+1074+1 halvings, any finite double is bound to underflow into zero.

Campbell Ritchie
Marshal
Posts: 56529
172
aaron butcher wrote:5 % = 5.0 / 95.0
No, that is not the correct formula.

Campbell Ritchie
Marshal
Posts: 56529
172
aaron butcher wrote: . . . not sure how to turn that around to code
The arithmetical operators were chosen to use keys already present on a typewriter, so the code for arithmetic looks very similar to what you would have written on paper.

aaron butcher
Ranch Hand
Posts: 32
Campbell Ritchie wrote:
aaron butcher wrote: . . . not sure how to turn that around to code
The arithmetical operators were chosen to use keys already present on a typewriter, so the code for arithmetic looks very similar to what you would have written on paper.

(decayRate/100)*mass;

Campbell Ritchie
Marshal
Posts: 56529
172
Yes, but the () are redundant and you can remove them.

aaron butcher
Ranch Hand
Posts: 32
Campbell Ritchie wrote:Yes, but the () are redundant and you can remove them.

got it thanks alot guys

Campbell Ritchie
Marshal
Posts: 56529
172
You're welcome

You do realise that formula will fall down horribly if you do it in integer arithmetic?