ArraysLinear Search This program starts by generating an array of random integer values. It then picks one element from it and uses the search method to find back its index in the array. We then verify the index corresponds to the element we picked. The method search uses a simple linear search algorithm that looks at every element in the array passed as parameter and returns its index if it matches the value also passed as parameter. The method getRandomArray uses (Math.random(*10+1) to generate the elements of an array randomly between (1.10). Implement these method in the following order; main, search, getRandomArray, getRandomint. import java.util.Scanner: public class Atrayslinear Search public static void main(String[] args) { int size -10 int[] data- get RandomArray (size); if (indexFound+1) ( System.out.println("Value was not found"); 1 else if (data[indexFound] -- data[indexRandom) System.out.println("It worked!!!"); 3 ) //end of main method for (int i 0; i < data.length) data[i++) - getRandom Int (10); 1 int indexRandom - getRandom Int (size); int indexFound - search(data, data[indexRandom]);
import java.util.Scanner; public class Arrays Linear Search public static void main(String[] args) { int size 10; int[] data = getRandomArray (size); if(indexFound -- +1) { System.out.println("Value was not found"); else if (data[indexFound] == data[indexRandom)) { System.out.println("It worked!!!"); } //end of main method for (int i = 0; i < data.length;) { data[i++] = getRandomInt (10); int indexRandom = getRandomint (size); int index Found = search(data, data[indexRandom]); if (indexFound == -1){ System.out.println("Value was not found"); } else if (data[indexFound] =- data[indexRandom)) { System.out.println("It worked!!!"); 1 } //end of main method int break - getRandom Int (size); int indexFound = search(data, data[indexRandom)); if findexFound ---1) System.put.println("Value was not found"); else if (data[indexFound) data[indexRandom)) { System.out.println("It worked!!!"); 1 }//end of main method public static int search (int[] array, int value) for (int i=0;i< array.length; i++) if (array(1) value) return i; ) int[] data - new int[size]; return -1; 1 // end of search method public static int[] getRandom Array (int size) return data: 17 end of getRandomArray method public static int getRandom Int(int max) return (int) (Math.random() *max+1) 117 end of getRandomint method 1 end of class M
Trashed: public static void main(String[] args) { int size 10; int[] data getRandomArray (size) public static void main(String[] args) { int size 10; int[-1] data = getRandomArray (size); public static void search (int[] array, int value) { for (int i=0; i < array.length; i--) { if (array -= value) return i; for (int i=1 ; i < array.length; i++) { if(array -= value) return i; 3
ArraysLinear Search This program starts by generating an array of random integer values. It then picks one element from
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
ArraysLinear Search This program starts by generating an array of random integer values. It then picks one element from
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!