Java w/ data structures help

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

Java w/ data structures help

Postposted on Sun Sep 05, 2004 2:15 pm

Hi guys and gals,

Im currently taking a class in java, and out professor has assumed way to much of our knowledge. He has taught us some very very basic things, basically input and output from user nad handling some basic IOExceptions.

He gives us our first program and I am lost. Here goes..

I have to write a program to find the minimum, maximum, and average of a sequence of user-inputed numbers. Enter a negative number should also end the input phase. It then should reprint those three things after the user enters all the numbers he/she wishes.

From what I know, or think I do... I wanted to use an array to hold the values of the users input, then after a negative number is entered, break that loops and begin another to find those 3 values of min max and average.

Im a complete newb to Java, and have some c++ albeit 2 years ago. Im currently using Netbeans 3.6 as my compiler and developing environment.

Am I on track with my logic? Im really stuggling here and I know this should be easy, I DO NOT however want anyone to write this thing for me, Im just looking for suggestions on what I should be doing or thinking.

Thanks all!
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | Asus 970GTX | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W | Asus ROG Swift Gsync 27"
Gerbilus Supremus
Posts: 11960
Joined: Mon May 26, 2003 5:55 am
Location: Pittsburgh PA

Postposted on Sun Sep 05, 2004 2:51 pm

Array, calculation and loop, and comparison syntax is pretty much the same in Java as C++.

From what I can tell, you want to use a linked list instead of an array. That would avoid the hassle of making a new array and copying everything into the new one when the array gets filled up.

Or you could 'cheat' and keep track of the sum, min, max, and number of inputted numbers as you go along, and calc the avg at the end. That's probably what the professor had in mind if he only has taught what you mentioned.
Grand Gerbil Poohbah
Posts: 3575
Joined: Sun Feb 16, 2003 3:43 pm
Location: MD

Postposted on Sun Sep 05, 2004 3:15 pm

Make sure that you take a look at all coding styles for that course.

In my first term at university, we lost marks for using the break and continue keywords as well as other things. :(
Gerbil Team Leader
Posts: 291
Joined: Tue Mar 09, 2004 10:36 pm
Location: Ontario, Canada

Postposted on Sun Sep 05, 2004 5:50 pm

A little bit of pseudocode then? Let's make sure I know what you mean: you want to just have the min, max and average values at the end of it. Nothing else?

Code: Select all

// define userInput, min, max, avgTotal, and avgCount here

while (userInput not negative)

compare number to current min, max variables, change variables if neccessary

add one to avgCount, and userInput to avgTotal


// you now have min and max, all you need is average

double average = avgTotal / avgCount

No need for an array at all. I figure this is a bit neater that way. Unless you've been specifically told to use one, or you need to do some form of statistical analysis later on, I don't see the need...

Hope that helped,
Living proof of John Gabriel's theorem
Grand Gerbil Poohbah
Posts: 3507
Joined: Sat Dec 29, 2001 7:00 pm
Location: The nearest pub

Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 1 guest