Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
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
• Ron McLeod
• Paul Clapham
• Rob Spoor
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Junilu Lacar
• Tim Cooke
Saloon Keepers:
• Tim Holloway
• Piet Souris
• Stephan van Hulst
• Tim Moores
• Carey Brown
Bartenders:
• Frits Walraven
• Himai Minh

# Doing Algebra within the Array

Ranch Hand
Posts: 69
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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:

It won't get any simpler than that!!!

Vicken Karaoghlanian
Ranch Hand
Posts: 522
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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: 1843
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
[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
• Number of slices to send:
Optional 'thank-you' note:

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.

 Always! Wait. Never. Shut up. Look at this tiny ad. the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising