• Post Reply Bookmark Topic Watch Topic
  • New Topic

While loops  RSS feed

 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Newton’s Law of Cooling states that when a hot liquid is placed in a cool
room, each minute the decrease in the temperature is approximately proportional
to the difference between the liquid’s temperature and the room’s temperature.
That is, there is a constant k such that each minute the temperature loss is
k # (liquids temperature - rooms temperature). Suppose a cup of 212°F coffee is
placed in a 70°F room and that k = .079. Determine the number of minutes required
for the coffee to cool to below 150°F.

I'm not completely sure about my math here. I haven't fully coded yet, I just want to get the math right first.
 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shal Lango wrote:Newton’s Law of Cooling states that when a hot liquid is placed in a cool
room, each minute the decrease in the temperature is approximately proportional
to the difference between the liquid’s temperature and the room’s temperature.
That is, there is a constant k such that each minute the temperature loss is
k # (liquids temperature - rooms temperature). Suppose a cup of 212°F coffee is
placed in a 70°F room and that k = .079. Determine the number of minutes required
for the coffee to cool to below 150°F.

I'm not completely sure about my math here. I haven't fully coded yet, I just want to get the math right first.


I meant minutes += 1 on line 9
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code won't achieve what the formula says.

One of the reasons are, that you never update liquid's (which is in a cup I pressume) temperature.
 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Your code won't achieve what the formula says.

One of the reasons are, that you never update liquid's (which is in a cup I pressume) temperature.


Like this?
k = 0.79;
room = 70;
cup = 212;
tempLoss = 0;
minutes = 0;

while (cup <= 212 & cup >= 150) {
    tempLoss = k*(cup - room);
    tempLoss = cup
    minutes += 0;
}
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really.

Read the formula you wrote once again.

An example. I have 100 $ in my account. For the internet I pay 20$ a month. How many months I can be calmed?

What you saying now is:


While the answer should be 5, in your case would be 1 using that loop.
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, maybe subtraction does the job?

If I have 100$ in my account, and every month pay 20$. That means after or before month begins I pay 100 - 20 = 80. Then after the month 80 - 20...
 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:So, maybe subtraction does the job?

If I have 100$ in my account, and every month pay 20$. That means after or before month begins I pay 100 - 20 = 80. Then after the month 80 - 20...


cup -= tempLoss
?
 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:So, maybe subtraction does the job?

If I have 100$ in my account, and every month pay 20$. That means after or before month begins I pay 100 - 20 = 80. Then after the month 80 - 20...


cup -= tempLoss
?
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a basic thing. I have explained I think try do the math on a paper and see if it makes sense.
 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:That is a basic thing. I have explained I think try do the math on a paper and see if it makes sense.


Thank you!
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What types are the three numbers? You can get incorrect results if you use the wrong types.
You have written 0.79 when the question says 0.079.
Why have you written the bit about <= 212? Does that do anything useful?
There is probably a method using logarithms which is simpler but you are probably being taught how to write a loop.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!