Advertisement
Shavit

P. 135 Ex. 12.4

Mar 21st, 2014
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1. // Shavit Borisov
  2. // CW
  3.  
  4. public class Find25 {
  5.  
  6.     public static void main(String[] args)
  7.     {
  8.         final int SIZE = 20;
  9.         final int KEY = 25;
  10.         int[] A = new int[SIZE]; // Needs to be initialized with sorted values
  11.         System.out.printf("%d was found %d times in array A[%d]", KEY, findKey(A, KEY), SIZE);
  12.     }
  13.    
  14.     private static int findKey(int[] array, int key)
  15.     {
  16.         int result = 0;
  17.         for(int i = 0; i < array.length; i++)
  18.         {
  19.             if(array[i] > key)
  20.                 break;
  21.             else if(array[i] == key)
  22.                 result++;
  23.         }
  24.         return result;
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement