• Post Reply Bookmark Topic Watch Topic
  • New Topic

Another beginner question on loops  RSS feed

 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So here is another problem from my exercises:




What is happening when it runs the loop is it's performing integer math as opposed to floating point math thus I get 1.0 every time as the sum. I feel like everything looks as it should to perform floating point math so what am I doing wrong here?

Thanks for any help and input, really struggling!






 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the weird formatting, I thought I used the code block correctly - apparently not
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You used quote instead of code for one, and didn't code the tag correctly on the other. I fixed it for you.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Borchgrevink wrote:Sorry for the weird formatting, I thought I used the code block correctly - apparently not

Not so sure. It looks fine to me. Not quite sure what happened.

However, look at your "sum =" statement. It would appear that you're trying to use a compound expression with more than one assignment (not a good idea) and you've got it wrong.

Winston

[Edit] Aha! Must have looked at it after David got his sticky mitts on it.

PS: Please DontWriteLongLines. It makes your post very hard to read. The problem in your case is your comments.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
David Borchgrevink wrote:Sorry for the weird formatting, I thought I used the code block correctly - apparently not

Not so sure. It looks fine to me. Not quite sure what happened.

However, look at your "sum =" statement. It would appear that you're trying to use a compound expression with more than one assignment (not a good idea) and you've got it wrong.

Winston


yea for some reason the sum = line wasn't how I have it in notepad ++

mine looks like:



to me it looks like that would be right right? sum = 1.0/count + 1 which would be right?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Borchgrevink wrote:to me it looks like that would be right right? sum = 1.0/count + 1 which would be right?

I think you're getting too bogged down in the intricacies of casting. The fact is that the statement is probably right for the calculation, but you never update the count. Personally, I'd use a for loop, and forget all this business of trying to convert absolutely everything to a double.

Another tip: Don't try to do too much at once. Break up that calculation into individual pieces, and if need be store the interim values in a couple of variables.

Do one thing at a time. Once you've got it working, then worry about optimization (or shorteneing it).

Winston
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
David Borchgrevink wrote:to me it looks like that would be right right? sum = 1.0/count + 1 which would be right?

I think you're getting too bogged down in the intricacies of casting. The fact is that the statement is probably right for the calculation, but you never update the count. Personally, I'd use a for loop, and forget all this business of trying to convert absolutely everything to a double.

Once you've got it working, then worry about optimization.

Wnston


you were exactly right. the equations were right, but i was never adding the sum of each iteration of the loop to the next, it was this endless loop of doing the exact same equation and the sum never changed. I added 2 more variables and it works as it should now. I would love to use a for loop, but the specific exercise called for only using while loops. thanks so much for responding though; had I not figured this out, I would have been pulling my hair out (only 7 total class days thus far - very green)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!