Advertisement
Guest User

Statistics -ROB CARNAGI

a guest
Feb 22nd, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package statistics;
  7.  
  8. /**
  9.  *
  10.  * @author caranra
  11.  */
  12. public class Statistics {
  13.  
  14.     /**
  15.      * @param args the command line arguments
  16.      */
  17.     public static void main(String[] args) {
  18.         int[] a1= new int [20];//initialize an array w/ 20 values
  19.         a1 =randomize(a1,100); //the array will have random points, 0-100
  20.         display(a1);//the array will be displayed in the outpost
  21.         a1=sort(a1);//sorts the arrays into numerical order
  22.         display(a1);//displays the sorted array
  23.         System.out.println("The average for Array 1 is: " +average(a1) );
  24.        
  25.      
  26.        
  27.     }
  28.  
  29.     public static int[] randomize(int[] a, int max) { //creates a ray that is filled with points at random
  30.         for(int i=0; i< a.length; i++) {
  31.             a[i]=(int)(Math.random()*max)+1;
  32.         }
  33.         return a;
  34.     }
  35.      
  36.     public static void display(int[] a) {
  37.         for(int i=0;i<a.length;i++){
  38.             System.out.print(a[i]+" ");
  39.          
  40.         }
  41.    System.out.println(" ");
  42.        
  43.     }
  44.     public static double average(int[]a){
  45.         double sum=0;
  46.         for(int i=0; i<a.length;i++){
  47.             sum+=a[i]; //sum=sum+a[i]
  48.         }
  49.         return sum/a.length;
  50.     }
  51.  
  52.     private static int[] sort(int[]a){
  53.      int size =a.length; //# of values in the array
  54.     int[]sorted=new int[size];
  55.     int min=0;
  56.     for(int i=0; i<a.length;i++){
  57.         min=0;
  58.         for(int j=0;j<a.length;j++){
  59.             if(a[j]<a[min]){
  60.                 min=j;
  61.             }
  62.         }
  63.         sorted[i]=a[min];
  64.         a[min]=5000;
  65.     }
  66.     return sorted;
  67.     }
  68.    
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement