Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class Competitors {
- public static void main(String[] args) {
- System.out.println("Enter the number of competitors ");
- Scanner scanner = new Scanner(System.in);
- int numberOfCompetitors = scanner.nextInt();
- if(numberOfCompetitors <= 1 ){
- System.out.println("Incorrect value");
- System.exit(0);
- }
- int[] competitors = new int[numberOfCompetitors];
- //Enter score for each competitor
- for(int i = 0; i < competitors.length; i++){
- System.out.println("Enter the time of the " + (i + 1) + "'s" + " competitor in sec.");
- competitors[i] = scanner.nextInt();
- if(competitors[i] <= 0 ){
- System.out.println("Incorrect value");
- System.exit(0);
- }
- }
- //Sorting the array
- Arrays.sort(competitors);
- //calculate the amount of time
- int sum = 0;
- for(int i = 0; i < competitors.length; i++){
- sum += competitors[i];
- }
- //avg
- double average = sum / competitors.length;
- //calculate the standard deviation
- int round = 0;
- double x = 0;
- double standardDeviation;
- while(true){
- if(round == (competitors.length)){
- standardDeviation = Math.sqrt(x / competitors.length);
- break;
- }
- x = x + Math.pow(competitors[round] - average,2);
- round++;
- }
- System.out.println("All results: " + Arrays.toString(competitors));
- System.out.printf("The fastest result: %d sec %n", competitors[0]);
- System.out.printf("The slowest result: %d sec %n",competitors[competitors.length - 1]);
- System.out.printf("Avg: %.1f%n",(double)(sum / competitors.length));
- System.out.printf("Standard deviation: %.2f sec %n",standardDeviation);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement