wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Help with sorting array of random numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with sorting array of random numbers" Watch "Help with sorting array of random numbers" New topic
Author

Help with sorting array of random numbers

Kyle Wettengel
Greenhorn

Joined: Feb 06, 2013
Posts: 14
Hey guys looking for some help with my ArrayTester class...
The array is supposed to
1.create a 20 element int array and populates it with random elements in the range 20 through 65 (both inclusive).
-I Believe I have already done this.

2.Use the java.util.Random class with a seed of 2621 to generate repeatable output for the data.
-I have also already done this

3.The tester class should then test each of these methods(mean, median, mode, max, min, standard divination)
-this is where I'm having trouble, I already have the code for the mean, mode, max, min, and standard divination.. the problem I'm having is with the median as i would like to sort my array in the tester class before having it called by my main Class.

Is there any way to do this?

Here is my code:

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

If you're asking whether it's possible to sort an array, then yes, of course it is.

Are you supposed to write code from scratch which sorts the array, or are you allowed to use a method from the standard API which sorts the array?
Kyle Wettengel
Greenhorn

Joined: Feb 06, 2013
Posts: 14
If your talking about Arrays.sort() then yes I am! however I'm having trouble even with that, as of right now when I put the Arrays.sort(numbers); outside the for loop nothing changes when I outprint the array (the numbers are still printing out, they're just not sorted) This made me believe that I had to also move my System.out.println(numbers[i]) outside the for loop as well. However when I do this I get an error saying that it cannot find the variable [i]

Here's my code:

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

Don't you want to print each of the numbers in the array at that point?
Kyle Wettengel
Greenhorn

Joined: Feb 06, 2013
Posts: 14
yes, right when there sorted I would like to print and store them so the sorted array can be used in my other class which calculates the mean median and mode.
Kyle Wettengel
Greenhorn

Joined: Feb 06, 2013
Posts: 14
I dont have to print out the array, I just thought it would be a good way to check to see if its working properly
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
But by that stage, i has gone out of scope. Quickest way to print an array like that is probably to use the toString methods in the Arrays class.
vinay chaturvedi
Greenhorn

Joined: Jan 16, 2012
Posts: 14

Arrays.sort(numbers);
//this is where the error happens
System.out.println(numbers[i]);


If you want to print the sorted array, then you have to iterate over the array.

See the below code :



You can not store the sorted array as the Array.sort() method returns void.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7029
    
  16

vinay chaturvedi wrote:If you want to print the sorted array, then you have to iterate over the array...

Or you can simply use Arrays.toString(numbers).

@Kyle: Don't re-invent the wheel, unless you're simply doing it as a programming exercise.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7029
    
  16

Kyle Wettengel wrote:yes, right when there sorted I would like to print and store them so the sorted array can be used in my other class which calculates the mean median and mode.

My suggestion: have a good look at the Arrays class (java.util.Arrays).

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
vinay chaturvedi wrote: . . . You can not store the sorted array as the Array.sort() method returns void.
You have misunderstood how the sort method works. It takes an array as its parameter and alters the state of that array. That is rather like an output parameter.
You can store the sorted array, because it is the same array you started off with.
If you are not using a toString method, you should use a for‑each loop to print that array.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with sorting array of random numbers
 
Similar Threads
Help coding
Math.random().....any formula to adjust required range?
Please help in this selection sort
It compiles but only part of it runs.
standard deviation of any sized array java