Question
need some help with assignment dont get the best way to do it // Fig. 19.2: LinearSearchTest.java // Sequentially searching an array for an item.
need some help with assignment dont get the best way to do it
// Fig. 19.2: LinearSearchTest.java
// Sequentially searching an array for an item.
import java.security.SecureRandom;
import java.util.Arrays; 5 import java.util.Scanner; 7
public class LinearSearchTest 8 {
perform a linear search on the data
public static int linearSearch(int data[], int searchKey)
{
// loop through array sequentially
for (int index = 0; index < data.length; index++)
if (data[index] == searchKey)
return index; // return index of integer
return -1; // integer was not found } /
/ end method linearSearch
public static void main(String[] args)
21 {
22 Scanner input = new Scanner(System.in);
23 SecureRandom generator = new SecureRandom();
24
25 int[] data = new int[10]; // create array
26
27 for (int i = 0; i < data.length; i++) // populate array
28 data[i] = 10 + generator.nextInt(90);
29
30 System.out.printf("%s%n%n", ); // display array
31
32 // get input from user
33 System.out.print("Please enter an integer value (-1 to quit): ");
34 int searchInt = input.nextInt();
35
36 // repeatedly input an integer; -1 terminates the program
37 while (searchInt != -1)
38 {
39 int position = ; // perform search
40
41 if (position == -1) // not found
42 System.out.printf("%d was not found%n%n", searchInt);
43 else // found
44 System.out.printf("%d was found in position %d%n%n",
45 searchInt, position);
46
47 // get input from user
48 System.out.print("Please enter an integer value (-1 to quit): ");
49 searchInt = input.nextInt();
50 }
51 } // end main
52 } // end class LinearSearchTestExercise
19.8 on Page 837: Recursive Linear Search (10th Edition) Exercise 19.8 on Page 819: Recursive Linear Search (11 th Edition) Generate twenty random integers between 10 and 100 inclusive and store them in an array. Give the user the chance to enter the search value.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started