Advertisement
Shavit

P. 135 Ex. 12.16

Mar 21st, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1. // Shavit Borisov
  2. // CW
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class BiggerThanFirstAndLastAverage {
  7.  
  8.     public static void main(String[] args)
  9.     {
  10.         Scanner in = new Scanner(System.in);
  11.         int A[]; // Needs to be initialized with sorted values
  12.        
  13.         System.out.printf("What size is your array? ");
  14.         A = new int[in.nextInt()];
  15.        
  16.         int smallerThanAverage = smallerThanKey(A, ((A[0] + A[A.length - 1]) / 2));
  17.        
  18.         System.out.printf("%s", smallerThanAverage >= A.length ? "Most values are less than first and last average" : "Most values are more than first and last average");
  19.         in.close();
  20.     }
  21.    
  22.     private static int smallerThanKey(int[] array, int key)
  23.     {
  24.         int result = 0;
  25.         for(int i = 0; i < array.length; i++)
  26.         {
  27.             if(array[i] > key)
  28.                 break;
  29.             else
  30.                 result++;
  31.         }
  32.         return result;
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement