How Ever
How Ever

Reputation: 11

How to display the numbers of this exercise in ascending order in java?

I got this exercise where i need to build a program in java that creates a 1-dimensional table where 10 integers will be stored, which will be read from the keyboard. At the end the program will display all the integers that are larger than average. (As you can see i've done this). But i need to display the numbers that are larger than average in ascending order. So there should be another instruction in the end, please help 🙏 I should say that im a beginner in java though. shuma=sum, mesatarja=average tabela = array though

please see it and help me solve this :)

Scanner in = new Scanner (System.in);

    int [] tabela = new int [10];
    
    System.out.print("Ju lutem jepni 10 nr te plote: ");
    
    for (int i = 0; i<tabela.length; i++) {
        tabela[i] = in.nextInt();
    }
    
    System.out.println("Tabela = " + Arrays.toString(tabela));

    int shuma = 0;
    
    for (int i = 0; i < tabela.length; i++)
    shuma = shuma + tabela[i];
    
    double mesatarja = shuma*1.0/tabela.length;
    
    System.out.println("Mesatarja e numrave eshte: " + mesatarja);
    
    System.out.print("Numrat me te medhenj se mesatarja jane: ");
    
    for (int i = 0; i < tabela.length; i++) {
        if (tabela[i] > mesatarja) {
        System.out.print(tabela[i] + ", ");
        }
        }

Upvotes: 0

Views: 132

Answers (1)

Yevhenii Chykalov
Yevhenii Chykalov

Reputation: 432

The simplest idea that comes to mind is to sort the array. You should use any sort algorithm after filling your array. Example using "buble sort":

for (int i = 0; i < tabela.length-1; i++) 
    for (int j = 0; j < tabela.length-i-1; j++) 
        if (tabela[j] > tabela[j+1]) 
           { 
             int temp = tabela[j]; 
             tabela[j] = tabela[j+1]; 
             tabela[j+1] = temp; 
           } 

Here you can find an explanation of the algorithms and their examples.

Upvotes: 1

Related Questions