// ITI 1120 // Name: Diana Inkpen, Student# 123456 //import java.io.*; /** * Returns the position of k in array, or -1 if k does not appear in the array. * Ex 6.15 in lecture notes */ class FindsInArrayPosition { public static void main (String[] args) { // DECLARE VARIABLES/DATA DICTIONARY int [] a; int k; int 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 = findKPos(a, k); // PRINT OUT RESULTS AND MODIFIEDS System.out.println( "The value appeared in array in position " + result); } public static int findKPos(int [] x, int k) { // VARABLE DECLARATIONS int index; // intermediate int position = -1; // RESULT //BODY OF THE ALGORITHM // break the loop if the element is found for (index = 0; index < x.length && position == -1; index++) { if(x[index]==k) position = k; } // RETURN RESULTS return position; } }