Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with sorting array of random numbers

 
Kyle Wettengel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't you want to print each of the numbers in the array at that point?
 
Kyle Wettengel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic