public static void main(String[] args)
int[] sortcounts = new int[10];
int random = 0;
for (int i = 0; i < 100; i ++) {
random = (int)(Math.random() * 10);
System.out.println("number: " + random);
sortcounts[random] = sortcounts[random] +1;
}
}
}
Sort 100 numbers into groups
Page 1 of 13 Replies - 627 Views - Last Post: 05 February 2012 - 09:23 PM
#1
Sort 100 numbers into groups
Posted 05 February 2012 - 07:17 PM
Hi, I'm new to Java and I'm trying to write a program that generates 100 random numbers from 1-10 then sorts them into groups. The output should say something like: 1: 5 times etc... So far I am able to generate the 100 numbers but I am having trouble with getting the numbers sorted into the groups.
Replies To: Sort 100 numbers into groups
#2
Re: Sort 100 numbers into groups
Posted 05 February 2012 - 07:24 PM
Are your curled brackets misplaced or did it just happen when you copy pasted your code?
#3
Re: Sort 100 numbers into groups
Posted 05 February 2012 - 08:18 PM
I just copied it from the public static line and I forgot to take the {'s out.
#4
Re: Sort 100 numbers into groups
Posted 05 February 2012 - 09:23 PM
Does your lesson specifically state that you have to have the hundred numbers generated first, and THEN sorted? If not you could make this a whole lot easier on yourself.
What I would do is make two arrays, one pre made with numbers from 1-10, the other being a parallel array called counter. Then right a method to check each of the random numbers generated against your array from 1-10 and have the respective counter numbers added. For example if random numbers generated a 1, then the subscript corresponding to one on your counting array would increment by one, if it were a 5 the subscript corresponding to 5 on your parallel array would increment.
From there if you don't want to display the values that were never generated you just make a enhanced for loop and only print subscripts from count that have a value > 0.
Otherwise the other long way to do it would be to use a bubble sort (or just the Array.sort method from the API), to sort your array, and then count them until there is a change. But this is very tedious and not a very good approach in my opinion.
Cheers.
Master Zeddicus
What I would do is make two arrays, one pre made with numbers from 1-10, the other being a parallel array called counter. Then right a method to check each of the random numbers generated against your array from 1-10 and have the respective counter numbers added. For example if random numbers generated a 1, then the subscript corresponding to one on your counting array would increment by one, if it were a 5 the subscript corresponding to 5 on your parallel array would increment.
From there if you don't want to display the values that were never generated you just make a enhanced for loop and only print subscripts from count that have a value > 0.
Otherwise the other long way to do it would be to use a bubble sort (or just the Array.sort method from the API), to sort your array, and then count them until there is a change. But this is very tedious and not a very good approach in my opinion.
Cheers.
Master Zeddicus
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|