Home > Mobile >  Is there a way to display the users input back to them before displaying the mean and median?
Is there a way to display the users input back to them before displaying the mean and median?

Time:02-02

I am working with the following code that allows the user to enter 9 numbers and it displays the mean and median of the numbers back to the user. I am trying to figure out the best way to revise my code so that before displaying the mean and median it will also show the 9 numbers the user entered. Thanks so much for any help in this matter!

import java.util.*;
class MeanMedian
{
   private static float mean(int arr[]){
    int n = arr.length;
    float sum = 0;
    for(int i = 0;i<n;i  ){
      sum  = arr[i];
    }
    return sum / n;
  }

  public static void selectionSort(int[] array) {
    for (int i = 0; i < array.length - 1; i  ) {
      int lowindex = i;
      for (int j = array.length - 1; j > i; j--)
        if (array[j] < (array[lowindex]))
          lowindex = j;
      int temp = array[i];
      array[i] = array[lowindex];
      array[lowindex] = temp;
    }
  }
  private static void swap(int[] array, int i, int j) {
    int temp = array[i];
    array[i] = array[j];
    array[j] = temp;
  }

  private static float median(int arr[]){
    selectionSort(arr);
    if(arr.length%2 == 0){
      return arr[(arr.length/2)-1];
    }
    else{
      return arr[arr.length/2];
    }
  }

  public static void main(String args[]){
    int n = 9;
    Scanner scanner = new Scanner(System.in);
    int arr[] =  new int[n];
    System.out.print("Enter "  n  " integers: ");
    for(int i = 0;i<n;i  ){
      arr[i] = scanner.nextInt();
    }
    
    System.out.println("mean: "  mean(arr));
    System.out.println("median: "  median(arr));
  }
}

CodePudding user response:

I am trying to figure out the best way to revise my code so that before displaying the mean and median it will also show the 9 numbers the user entered.

Something like:

// ... previous code ...
System.out.println("You entered: ");
for(int i : arr) {
    System.out.println(i);
}
System.out.println("mean: "  mean(arr));
System.out.println("median: "  median(arr));

CodePudding user response:

The following code worked perfectly and passed all checks I ran on it :)

import java.util.*;
class MeanMedian
{
   private static float mean(int arr[]){
    int n = arr.length;
    float sum = 0;
    for(int i = 0;i<n;i  ){
      sum  = arr[i];
    }
    return sum / n;
  }

  public static void selectionSort(int[] array) {
    for (int i = 0; i < array.length - 1; i  ) {
      int lowindex = i;
      for (int j = array.length - 1; j > i; j--)
        if (array[j] < (array[lowindex]))
          lowindex = j;
      int temp = array[i];
      array[i] = array[lowindex];
      array[lowindex] = temp;
    }
  }
  private static void swap(int[] array, int i, int j) {
    int temp = array[i];
    array[i] = array[j];
    array[j] = temp;
  }

  private static float median(int arr[]){
    selectionSort(arr);
    if(arr.length%2 == 0){
      return arr[(arr.length/2)-1];
    }
    else{
      return arr[arr.length/2];
    }
  }

  public static void main(String args[]){
    int n = 9;
    Scanner scanner = new Scanner(System.in);
    int arr[] =  new int[n];
    System.out.print("Enter "  n  " integers: ");
    for(int i = 0;i<n;i  ){
      arr[i] = scanner.nextInt();
    }
    
    System.out.println("You entered: ");
      for(int i : arr) {
    System.out.println(i);
    }
    System.out.println("mean: "  mean(arr));
    System.out.println("median: "  median(arr));
  }
}
  •  Tags:  
  • Related