Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class RaceResults
- {
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- int runners = -1;
- do {
- System.out.print("Enter number of runners: ");
- if (sc.hasNextInt()) {
- runners = sc.nextInt();
- } else sc.next();
- }
- while (runners <= 0); // Data validation for number of runners
- double[] times = new double[runners];
- int count = 0;
- System.out.println("Enter times for runners in seconds.");
- do {
- System.out.printf("Enter time for runner# %d: ", (count + 1));
- if (sc.hasNextDouble()) {
- times[count] = sc.nextDouble();
- count++;
- } else sc.next();
- }
- while (count < times.length); //Data validation for the times of the runners
- for (int i = 0; i < times.length; i++) {
- System.out.printf("\nRunner # %d time: %.3f", (i +1), times[i]);
- }
- Arrays.sort(times);
- System.out.printf("\033[32m\nFastest time is: %.3f",times[0]);
- System.out.printf("\033[31m\nSlowest times is: %.3f",times[times.length - 1]);
- double sum = 0;
- for (double n: times){
- sum += n;
- }
- double avrg = sum/runners; //calculating avrage time for all runners
- double[] mathArray = new double[times.length]; //new array to store calculated values from the first array
- for (int i = 0; i < times.length; i++) {
- mathArray[i] = Math.pow((times[i] - avrg), 2); //substracting the avrage from each time and squaring the result and save
- //it in the new array
- }
- double sum2 = 0;
- for (double n: mathArray){
- sum2 += n;
- }
- sum2 = Math.sqrt(sum2/runners); //calculating the the standart deviation by squaring the sum of the second
- //array devided by the total number of runners
- System.out.printf("\033[39m\nStandard deviation is: %.3f ", sum2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement