need help in sorting array(java)!!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wing ver ka gundam
    New Member
    • Mar 2007
    • 2

    #1

    need help in sorting array(java)!!!

    THanks~
    this is the example and my quote are below
    Write a program in JAVA that allows the user to input 5 ints. The program will then output these five values in reverse order. The program will then output the values in order with a statement on how many of the remaining values are larger, smaller, or equal.

    Example 1:
    Input:
    1
    2
    3
    4
    5

    Output:
    5
    4
    3
    2
    1
    1 - 4 larger, 0 smaller, 0 equal
    2 - 3 larger, 2 smaller, 0 equal
    3 - 2 larger, 2 smaller, 0 equal
    4 - 1 larger, 3 smaller, 0 equal
    5 - 0 larger, 4 smaller, 0 equal



    and my qupte are as follow but i am lose in how to
    count how many int are greater/less/equals then the currenty index int in a array. everytime the max value changes. Not sure how to make the max constant after it find the max int number in the array.

    public static void main( String[] args )
    {
    Scanner scan = new Scanner(System. in);

    int[] numbers = new int[5];

    for(int index = 0 ; index < numbers.length; index ++)
    {
    System.out.prin t("enter number"+(index + 1) + " : ");

    numbers[index] = scan.nextInt();
    }


    for (int j=0; j <= (numbers.length -1) ;j++)

    int small = 0;
    int greater = 0;
    int count= 0;


    for (int n=(numbers.leng th -1); n>=0 ; n--)

    if (numbers[j] > numbers[n])
    {
    greater++;
    }
    else if (numbers[j] < numbers[n])
    {
    small++;
    }
    else if (numbers[j] == numbers[n])
    {
    count++;
    }

    System.out.prin tln(+values[j]+": " + greater+ "greater: "
    +small+ "smaller: " +count+ "equal" );
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by wing ver ka gundam
    THanks~
    this is the example and my quote are below
    Write a program in JAVA that allows the user to input 5 ints. The program will then output these five values in reverse order. The program will then output the values in order with a statement on how many of the remaining values are larger, smaller, or equal.

    Example 1:
    Input:
    1
    2
    3
    4
    5

    Output:
    5
    4
    3
    2
    1
    1 - 4 larger, 0 smaller, 0 equal
    2 - 3 larger, 2 smaller, 0 equal
    3 - 2 larger, 2 smaller, 0 equal
    4 - 1 larger, 3 smaller, 0 equal
    5 - 0 larger, 4 smaller, 0 equal



    and my qupte are as follow but i am lose in how to
    count how many int are greater/less/equals then the currenty index int in a array. everytime the max value changes. Not sure how to make the max constant after it find the max int number in the array.

    public static void main( String[] args )
    {
    Scanner scan = new Scanner(System. in);

    int[] numbers = new int[5];

    for(int index = 0 ; index < numbers.length; index ++)
    {
    System.out.prin t("enter number"+(index + 1) + " : ");

    numbers[index] = scan.nextInt();
    }


    for (int j=0; j <= (numbers.length -1) ;j++)

    int small = 0;
    int greater = 0;
    int count= 0;


    for (int n=(numbers.leng th -1); n>=0 ; n--)

    if (numbers[j] > numbers[n])
    {
    greater++;
    }
    else if (numbers[j] < numbers[n])
    {
    small++;
    }
    else if (numbers[j] == numbers[n])
    {
    count++;
    }

    System.out.prin tln(+values[j]+": " + greater+ "greater: "
    +small+ "smaller: " +count+ "equal" );
    1.) Please use code tags when posting code.
    2.) Your example seems to be contradicting your problem statement
    "The program will then output the values in order with a statement on how many of the remaining values are larger, smaller, or equal"

    So isn't it supposed to be

    1 - 4 larger, 0 smaller, 0 equal
    2 - 3 larger, 0 smaller, 0 equal
    3 - 2 larger, 0 smaller, 0 equal
    4 - 1 larger, 0 smaller, 0 equal
    5 - 0 larger, 0 smaller, 0 equal

    Comment

    • wing ver ka gundam
      New Member
      • Mar 2007
      • 2

      #3
      i am really confused about the same/smaller/ larger counter
      since i am a beginner in java programming

      so what errors did i made in this program?
      and how can i correct it

      thanks~

      Comment

      Working...