3 Replies - 627 Views - Last Post: 05 February 2012 - 09:23 PM Rate Topic: -----

#1 atibbits22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-February 12

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.



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;
        }
        }
        } 


Is This A Good Question/Topic? 0
  • +

Replies To: Sort 100 numbers into groups

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,251
  • Joined: 05-April 11

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?
Was This Post Helpful? 0
  • +
  • -

#3 atibbits22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-February 12

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.
Was This Post Helpful? 0
  • +
  • -

#4 MasterZeddicus  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 27
  • Joined: 30-January 12

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1