aspose file tools*
The moose likes Beginning Java and the fly likes Doing Algebra within the Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doing Algebra within the Array" Watch "Doing Algebra within the Array" New topic
Author

Doing Algebra within the Array

Sean Magee
Ranch Hand

Joined: Aug 23, 2004
Posts: 69
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
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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 ).
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
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!!!


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
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.
Matt Fielder
Ranch Hand

Joined: Oct 27, 2004
Posts: 158
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?
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

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 ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Matt Fielder
Ranch Hand

Joined: Oct 27, 2004
Posts: 158
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

Joined: Jul 21, 2003
Posts: 522
[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

Joined: Aug 07, 2003
Posts: 1646
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doing Algebra within the Array