Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Don't understand why one way works but not the other  RSS feed

 
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm taking an intro to Java course (some may have seen previous questions posted).


The code below is for an interactive example from my text. I didn't write the majority of the code, the scenario is:

Modify the program to print the average (mean) as well as the sum. Hint: You don't actually have to change the loop, but rather change what you print.



This is the initial modification that I attempted. It did not provide the proper output.
I added line 21, as well as line 26



Output for the above code was:

Enter 8 integer values...
Value:
Value:
Value:
Value:
Value:
Value:
Value:
Value:
Sum: 920
Average: 0



based on input of:
3 5 234 346 234 73 26 -1.

If I remove the variable initialization at line 21, and then I change line 26 to do the same thing that I was trying to do at line 21,
everything works out fine. I achieve proper output with the Average computed properly at 115.

Code that worked:




What I don't understand is why the second way worked, but the first didn't. Was I not essentially doing the same thing, only with a variable?

Thanks
 
Bartender
Posts: 690
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your first example, you calculate avgVal on line 21 BEFORE you calculate the sumVal. That is, sumVal is still zero.
In the second example, you calculate the average AFTER you have summed up all of the values.
 
Saloon Keeper
Posts: 2476
317
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bill Platt wrote:This is the initial modification that I attempted. It did not provide the proper output.
I added line 21, as well as line 26


It looks like you are calculating the average before calculating the sum of all the entered values (which is used in your average calculation).

Try calculating your average after completing the summing work.
 
Bill Platt
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:In your first example, you calculate avgVal on line 21 BEFORE you calculate the sumVal. That is, sumVal is still zero.
In the second example, you calculate the average AFTER you have summed up all of the values.



OK, thanks. I see that now.

One of these days I will get where I am thinking in the proper circles.

It didn't even occur to me that the second loop was doing the calculations for sumVal.

That probably should have been obvious.


Bill
 
Those are the largest trousers in the world! Especially when next to this ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!