It is often necessary to search an arrary for a given value. Using the windows32
ID: 3532488 • Letter: I
Question
It is often necessary to search an arrary for a given value. Using the windows32 or windows64 framework,
WRITE A PROGRAM THAT INPUTS A COLLECTION OF INTERGERS INTO AN ARRAY OF DOUBLEWORDS AND THEN SEQUENTIALLY SEARCHES FOR VALUES STORED IN THE ARRAY.
Implement the following design.
nbrElts:=0;
get address of array;
prompt for and input number;
while (number>0) loop
add 1 to nbrElts;
store number at address in array;
get address of next array element;
prompt for and input number;
end while;
repeat
prompt for and input keyValue;
get address array;
count:=1;
forever loop
if count>nbrElts
then
display keyValue, "not in array";
exit loop;
end if;
if keyValue = current element of array
then
display keyValue,"is element",count;
exit loop;
end if;
add 1 to count;
get address of next array element;
end loop;
prompt for and input response;
until(response='N') or (response='n')
Explanation / Answer
searching a string in an array public static void main(String[] args) { // define an array to be searched int[] sourceArray = {2,4,7,10,11,45,50,59,60,66,69,70,79}; // perform a binary search int index = binarySearch(10, sourceArray); // report to the user if (index >= 0) { System.out.println("Element was found at position " + index); } // -1 was returned, must not have found the item else { System.out.println("Element was not found."); } } // a binary search efficiently searches a pre-sorted array by looking at the midpoint // if the midpoint of the array is the number we care about, we are done! // if not, it will see if the number we care about is greater than or lower than the midpoint // it will then search that half of the array using the same technique public static int binarySearch(int num, int[] arrayToSearch) { // begin by defining the low point in the array & the high point in the array int low = 0; int high = arrayToSearch.length - 1; // keep looping as long as there is distance between the high and low points while (high >= low) { // look at the midpoint int mid = (high + low) / 2; // is the midpoint holding the number we care about? if so, stop! we're done! if (arrayToSearch[mid] == num) { return mid; } // otherwise it isn't - we need to keep searching else { // is the number we are searching for lower than the midpoint? if (numRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.