// ITI 1120 // Name: Diana Inkpen, Student# 123456 //import java.io.*; /** * This checks if k appears in an array. * Ex 6.14 in lecture notes */ class FindsInArray { public static void main (String[] args) { // DECLARE VARIABLES/DATA DICTIONARY int [] a; int k; boolean result; // READ IN GIVENS System.out.println( "Please enter the array:" ); a = ITI1120.readIntLine( ); System.out.println( "Please enter the element to search for:" ); k = ITI1120.readInt( ); // CALLS THE ALGORITHM result = findK(a, k); // PRINT OUT RESULTS AND MODIFIEDS System.out.println( "The value appeared in array: " + result); } public static boolean findK(int [] x, int k) { // VARABLE DECLARATIONS int index; // intermediate boolean found = false; // RESULT //BODY OF THE ALGORITHM // break the loop if the element is found for (index = 0; index < x.length && !found; index++) { if(x[index]==k) found = true; } // RETURN RESULTS return found; } }