• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doing Algebra within the Array

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the easiest way to have an array like:

double [] grades = new doulble [9];

put nine grades in there, and be able to find the mean of them?

is there a quicker way of doing this besides somehting like

[1] + [2] ..... /9 ?

i would think so, any help would be great
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming you already have the grades array, there is no faster way to calculate their mean than adding them up and dividing by the total.If, however, the grades are coming from some other source, you don't need to store them into an array to calculate the mean, but I don't think this is what you mean (no pun intended ).
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find your question a little bit ambigous...

If you want to find the Sum of two number (lets say A and B) you simply write:
A + B

If you want to find the Mean for 9 grades (in your example) you write:
(grade[0] + grade[1] + ... + grade[8]) / grade.length;

It won't get any simpler than that!!!
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using loops doesn't make your code more efficient (quicker), it simply make it more dynamic, readable and easy to write. In this given case I would have avoided using the unnecessary loop if performance is what you�re after.

The performance here will not be noticeable of course regardless you are using loops or not, but this just me nitpicking.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vicken Karaoghlanian:
Using loops doesn't make your code more efficient (quicker), it simply make it more dynamic, readable and easy to write. In this given case I would have avoided using the unnecessary loop if performance is what you�re after.

The performance here will not be noticeable of course regardless you are using loops or not, but this just me nitpicking.



Correct me if I'm wrong, but the whole idea of OOP design especially one that can be reused and extended would push us to design with the loop. The answer will come with the other, but what happens if you have to do the same calculation with another list of grades; one that is 50 numbers long? You have to go back and touch already valid code.

That just doesn't make sense to me. I agree that for a small example of problem for a class its fine, but why not get into the mindset of writing quality code?
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matt's right. Throw a method declatation around Davaid's code...

...and now it's ready to go into a utility class somewhere, ready and waiting for anytime that you need to calculate a mean of a double array. Of course, with method overloading, you can write a calculateMean for floats, ints, longs, etc; put them in the class as well, and you're golden. Now, anytime you want to calcualte the mean, it's one line of code:



The primary rule to good OOP coding is Unless performance is a problem, don't sacrifice reusability for performance. In this case, there is going to be no noticable performance difference between the loop and writing it out.
[ January 27, 2005: Message edited by: Joel McNary ]
 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joel McNary:
Matt's right.



Considering I'm still learning, that's nice to hear.
[ January 27, 2005: Message edited by: Matt Fielder ]
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[JM] Unless performance is a problem, don't sacrifice reusability for performance.

That is true. However, the issue in question here is to know the "quickest" way to calculate the mean and NOT the best way to calculate it, now for me this sounds more like a performance question rather than a reusability question, and the direct answer to it would be not to use loops. Although I agree that the correct and most appropriate way to calculate the mean is indeed through the use of loops (as Joel suggested), I don't believe it is the accurate answer for this question.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vicken Karaoghlanian:
Using loops doesn't make your code more efficient (quicker), it simply make it more dynamic, readable and easy to write.

Perhaps I was incorrect, but I assumed (ya I know) that Sean meant "quicker" as in "quicker to type and debug" since he first asked for the "easiest" way and he didn't post it in the Performance forum.

That he abbreviated the formula said to me, "Do I really need to type out each of the nine array entries?" To that I say, "No, not in general." Were this going to run in a performance-critical loop, calculating the mean of twenty million sets of 9 grades I might consider expanding it out.

But hey, it kicked off a good discussion so I suppose assumptions aren't always bad.
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic