-3

Ok so this is my code. I'm supposed to gather 10 numbers of input and then sort them in descending order by the number of which they appear.

Ex. {0,0,1,2,2,2]

My output would be "Mode=2 and then have 2 appears 3 times, 0 appears two times, 1 appears once."

My code can gather the 10 integers and find the mode but I'm having issues trying to sort the numbers in that way. This is what I have so far and I'm stuck.

import java.util.*;
class question2 
{
public static void main(String[] args) 
{
    Scanner scan = new Scanner(System.in);
     int[] arrTwo = new int[10];
     int x=0;
     for (int i = 0; i < 10; i++)
    {
        System.out.println("Enter number: " + (i + 1));
        arrTwo[i] = scan.nextInt();
    }
    mode(arrTwo);

    int temp;
    int bs[] = new int[10];
    for ( x=0 ; x<=8 ; ++x ) 
    {
        if (bs[x]>bs[x+1])
        {
            temp=bs[x];
            bs[x]=bs[x+1];
            bs[x+1]=temp;
            x=-1;
        }
    }
    for(int i = 0; i <= bs.length-1; i++)
        {
        for(int index=0; index <= 99 ; index++)
            {           
            if (i == i)
                {
                bs[i] +=1;
                }
            }
            System.out.println("The number " + i + " is generated " +arrTwo[i] + " times");
        }

}
public static void mode(int[] array)
{
    int modeTrack[] = new int[10];
    int max =0; int number =0;
    for (int i = 0; i < array.length; i++)
    {
        modeTrack[array[i]] += 1;
    }

    int maxIndex = 0;
    for (int i = 1; i < modeTrack.length; i++)
    {
        int newNum = modeTrack[i];
        if (newNum > modeTrack[maxIndex])
        {
            maxIndex = i;
        }

    }System.out.println("The mode is: "+maxIndex);

}

My output isn't listing my numbers, just 0-9 and the generated times is just going from 1-9 with no basis or order.

user3543981
  • 1
  • 1
  • 5
  • How do I apply that? That's new to me. – user3543981 Apr 17 '14 at 15:52
  • 1
    Sorting is one of the most researched computer science topics out there. Putting the array in a collection and using the built-in Collections.sort() is one, easy, way. There are plenty of examples of how to do that. – keyser Apr 17 '14 at 15:53
  • Alright, that wasn't too helpful. Thanks anyway – user3543981 Apr 17 '14 at 15:55
  • Look up some sorting algorithms. – Anubian Noob Apr 17 '14 at 15:56
  • `Arrays.sort` would be a start or even better think about using Lists.. – donfuxx Apr 17 '14 at 15:56
  • Actually it was, if you read between the lines. I know for a fact that there are more than 100 answers on this site that describes the procedure. If you learn how to find them you'll learn a lot of things faster. – keyser Apr 17 '14 at 15:57
  • 1
    Why putting array items to `Collection` and call `Collections.sort()`, when there is also `Arrays.sort()` ? – Betlista Apr 17 '14 at 15:57
  • Because I prefer lists. Of course there's no need :) And I didn't mean that he/she should create an array first, but directly populate a list. – keyser Apr 17 '14 at 15:58
  • Can someone please show me an example? – user3543981 Apr 17 '14 at 15:59
  • [Here's a Stackoverflow search](http://stackoverflow.com/search?q=%5Bjava%5D+sort+array), but really, Arrays.sort(someArray) is the quick answer. Just plug in your array. There's [documentation](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(int[])) too, if needed. – keyser Apr 17 '14 at 16:00

1 Answers1

0

this code:

    int bs[] = new int[10];
    for ( x=0 ; x<=8 ; ++x ){
        if (bs[x]>bs[x+1])        {
            temp=bs[x];
            bs[x]=bs[x+1];
            bs[x+1]=temp;
            x=-1;
        }
    }

does nothing. On initialization bs is filled with zeroes bs[x-1] is never greater than bs[x] because all values are the same. also

if (i == i){

is always true

Zbyszek Kisły
  • 2,110
  • 4
  • 26
  • 48