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 in = new Scanner(System.in);
- System.out.println("Enter count of runners:");
- int countOfRacers = in.nextInt(); // how many racers are in the race.
- double[] timesArray = new double[countOfRacers]; /// creates an array with the length of countOfRacers.
- double time;
- double averageTime;
- for (int i = 0; i < countOfRacers; i++) { /// this loop reads the input from the user and stores it in the arr Array.
- System.out.println("Please enter the time in seconds for runner " + (i + 1));
- time = in.nextDouble();
- timesArray[i] = time;
- }
- for (int i = 0; i < timesArray.length; i++) { /// this loop prints on the console the time of each racer.
- System.out.println("Runner number " + (i + 1) + " time is " + timesArray[i] + " seconds");
- }
- System.out.println(); // leaves space b/w different statistics.
- Arrays.sort(timesArray); //sorts from min to max value if int or double/alphabetical order if String Array!
- System.out.println("The fastest time is " + timesArray[0] + " seconds");
- System.out.println("The slowest time is " + (timesArray[timesArray.length - 1]) + " seconds");
- System.out.println(); // leaves space b/w different statistics.
- // long lay yo get average value ot the array
- //for (int i = 0; i < timesArray.length; i++) {
- // averageTime += timesArray[i] / timesArray.length;
- //}
- // short way yo get the average value ot the array.
- averageTime = Arrays.stream(timesArray).sum(); // Using Arrays.stream() to convert arr to a stream and .sum()to sum
- System.out.println("Average time is: " + averageTime / timesArray.length + " seconds");
- System.out.println();
- //// formula for Standard Deviation
- double mean;
- double variance = 0;
- double standardDeviation;
- mean = Arrays.stream(timesArray).sum() / timesArray.length;
- System.out.println("Mean = " + mean);
- for (int i = 0; i < timesArray.length; i++) {
- variance += (Math.pow(Math.abs(timesArray[i] - mean), 2)) / (timesArray.length - 1);
- }
- System.out.println("Variance = " + variance);
- standardDeviation = Math.sqrt(variance);
- System.out.printf("Standard Deviation = " + "%.2f" ,standardDeviation);
- System.out.println(" SD");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement