-2

I'm trying to copy the above average values to a new array then printing the lowest value in the new array. I kept trying to copy the values but it keep copying the intair array could someone help me with that.
Please help.

public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.println("Enter the number of students");
    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    int[] arr = new int[n];
    int total = 0;

    double average=0;
    int  aboveAverage=0;
    if(n<5) {
        System.out.println("ERROR: number of students must be 5 or more ");
    }else {
        System.out.print("Enter thier marks  ");
        for(int i=0; i<arr.length; i++){
            arr[i] = scanner.nextInt();
        }
        Arrays.sort(arr);
        System.out.println("Max value "+arr[arr.length-1]);
        for(int i=0; i<arr.length; i++){
            total = total + arr[i];
        }
        average = total / arr.length;
        System.out.format("The average is: %.1f", average);
    }

    System.out.print("\nEven  : ");
    for(int a = 0 ; a < n ; a++)
    {
        if(arr[a] % 2 == 0)
        {
            System.out.print(arr[a] + " ");
        }
    }   System.out.print("\nOdd : ");
    for(int a = 0 ; a < n ; a++)
    {
        if(arr[a] % 2 != 0)
        {
            System.out.print(arr[a] + " ");
        }
    }

    for (int a = 0; a < arr.length; a++)
    {
        if (arr[a]>average)
        {
            aboveAverage++;
        }

    }
    System.out.println("\nThe number of students above average ("+average+") is "+aboveAverage );
    System.out.print("B:");
    int arr1 []= new int[n];

    for(int b = 0; b < arr.length; b++) {
        if(arr[b] > average) {       
            arr1[b]=arr[b]  ;       
            arr1=  Arrays.copyOf(arr, n);
            System.out.print(arr1[b]+" ");
            Arrays.sort(arr1);
        }             
    }
    Arrays.sort(arr1);
    System.out.print("\nMinimum number in B: "+arr1[aboveAverage]+" ");

} 
Bashir
  • 2,057
  • 5
  • 19
  • 44
  • 3
    Please, format you code properly. – ish Apr 17 '20 at 09:43
  • You have calculated the number of values that are above average, why not use that information when copying. But of course you should be able to find the min value in the previous loop so this isn't really relevant. – Joakim Danielson Apr 17 '20 at 09:47
  • 1
    Does this answer your question? [Finding the minimum value of int numbers in an array (Java)](https://stackoverflow.com/questions/27317066/finding-the-minimum-value-of-int-numbers-in-an-array-java). All you need to add to the linked question is a comparison with average. – Joakim Danielson Apr 17 '20 at 09:50

1 Answers1

0

As you have defined the average number average and counted the number of students above this average aboveAverage, you need to copy the values properly:

int arr1[] = new int[aboveAverage];

for (int i = 0, j = 0; i < arr.length && j < arr1.length; i++) {
    if (arr[i] > average) {
        arr1[j++] = arr[i];
    }
}

System.out.print("\nMinimum number in B: " + arr1[0] + " ");
System.out.println("Marks above average: " + Arrays.toString(arr1));

You won't need to call Arrays.sort on this new array arr1 because it will be sorted already.

You may also need to fix calculation of the average because you divide integer numbers and may lose precision. You can either change type of total to double or change calculation:

average = ((double) total)/arr.length;
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42