Advertisement
KirilZdravkov

RaceResults

Feb 20th, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.98 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class RaceResults
  5. {
  6.   public static void main(String[] args)
  7.   {
  8.     Scanner sc = new Scanner(System.in);
  9.     int runners = -1;
  10.  
  11.     do {
  12.       System.out.print("Enter number of runners: ");
  13.       if (sc.hasNextInt()) {
  14.         runners = sc.nextInt();
  15.       } else sc.next();
  16.     }
  17.     while (runners <= 0);    // Data validation for number of runners
  18.  
  19.     double[] times = new double[runners];
  20.  
  21.     int count = 0;
  22.     System.out.println("Enter times for runners in seconds.");
  23.     do {
  24.       System.out.printf("Enter time for runner# %d: ", (count + 1));
  25.  
  26.       if (sc.hasNextDouble()) {
  27.         times[count] = sc.nextDouble();
  28.         count++;
  29.       } else sc.next();
  30.     }
  31.     while (count < times.length);    //Data validation for the times of the runners
  32.    
  33.     for (int i = 0; i < times.length; i++) {
  34.       System.out.printf("\nRunner # %d time: %.3f", (i +1), times[i]);
  35.     }                                                                  
  36.  
  37.     Arrays.sort(times);
  38.  
  39.     System.out.printf("\033[32m\nFastest time is: %.3f",times[0]);
  40.     System.out.printf("\033[31m\nSlowest times is: %.3f",times[times.length - 1]);
  41.  
  42.     double sum = 0;
  43.     for (double n: times){
  44.       sum += n;
  45.     }
  46.  
  47.     double avrg = sum/runners;   //calculating avrage time for all runners
  48.  
  49.     double[] mathArray = new double[times.length];          //new array to store calculated values from the first array
  50.  
  51.     for (int i = 0; i < times.length; i++) {
  52.       mathArray[i] = Math.pow((times[i] - avrg), 2);        //substracting the avrage from each time and squaring the result and save
  53.                                                             //it in the new array
  54.     }
  55.  
  56.     double sum2 = 0;
  57.     for (double n: mathArray){
  58.       sum2 += n;
  59.     }
  60.  
  61.     sum2 = Math.sqrt(sum2/runners);                 //calculating the the standart deviation by squaring the sum of the second                             
  62.                                                     //array devided by the total number of runners
  63.  
  64.     System.out.printf("\033[39m\nStandard deviation is: %.3f ", sum2);
  65.   }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement