Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(LinearSearch1) Implement a linear search. In this search process, we search an array by visiting och entry of the stray starting from the first and

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
(LinearSearch1) Implement a linear search. In this search process, we search an array by visiting och entry of the stray starting from the first and comparing the value in the array with the value sought. Write a method find( x. a) that will search an array of integers, x, for the value a. The method should return an integer indicating the index of the first occurrence of the value in the array, If we are unable to find the value in the array, the method should return the value - 1 For example, | 2 4 6 7 4 3 2 7 16 717 index 2 3 4 5 6 7 8 9 10 find( x, 7) will return 3, since 7 is first occurs at index (subscript) 3. findt x, 10) will return -1, since 10 does not occur in the array X 0 1 Here is a program stub for you to complete import java.util.1 class Linear Search 1 public static void main(Stringli argo) Scanner console - now Scanner(System.in) int) - 32. 27, 64, 18, 95, 14, 90, 27, 70, 60, 27) int location, seekValue 1: printListen: Systen.cot.print("nEnter a value to seek: "); neekValue - console.nextInt() location - find n, seekValue): ir location >= 0) System.out.println("Element found at location location): System.out.println("Klement not found in list") System.out.print("incalling findet (7): ) for 1-11 14: 1++) System.out.printf(" findthin, 27. d)--> v ", 1. Pindith (n. 27. )) yotem.out.printf(" calling findAll()In") intl - indalla n. 27 ) printListit) I public static void pritlist int n) Dystem.out.print(" valt) forint 101n.length; i++) System.out.print("d", 11) System.out.print("in index: ") for(int 1-1 n. langthi i.) dystem.out.printf("d", 1) System.out.println() 1 Complete these methods public static int find intint value) return -1 public static int indth (int *, int value, into) return-11) public statiointil tinda11 Intl tint value I return new int(0 public static int find(int n, int value) Now, write the missing methods in the Linear Search1 class, 1) Method find which takes a built-in array of integers, x, and a given search value, m, and returns index of the first occurrence of s, in the array, If the value does not appear in the array, the method should return the index value - 2. Examples find ( 1, 2, 3, 5, 7, 3, 3, 6, -2, 12, 14), 3) + 2 find i 1, 2, 3, 5, 7, 3, 3, 6, -2, 12, 14 , 10 ) -1 2) Method find th which takes a built-in array of integers, x, a given integer search value, and an occurrence number, n, to returns. The method will return the index of the n-th occurrence of n. If there is not an n-th occurrence of the value, the method will return the index value-1. Examples: findth 1, 2, 3, 5, 7, 3, 3, 6, -2, 12, 14, 3, 1) 2 findth(12, 3, 5, 7, 3, 3, 6, -2, 12, 14 ), 3, 2) > 5 find the 1, 2, 3, 5, 7, 3, 3, 6, -2, 12, 14 ). 3. 4) -1 3) Method findAll which takes two parameters: a built-in array of integers, and a integer value being sought, m. The method returns a built-in array of integers containing the indices of all the occurrences of in the array. If the given value, , does not appear in the array, the method should return an array of integers with elements. Examples: find 11 | 1, 2, 3, 5, 7, 3, 3, 6, -2, 12, 14 ), 3) (2, 5, 61 findA11 1 1. 2. 3, 5, 7, 3, 3, 6, -2, 12, 14 ). 10) } Sample Output: vals: 32 27 18 95 16 90 27 Index 0 1 2 3 4 5 6 7 Enter a value to seek: 90 70 60 22 8 10 I Element found at location 6 Calling tindih ? 11 Find thin, 271) --> 1 Find thin, 27, 2) -->7 findlithin, 27, 3) --> 10 Calling tindall vals 1 Index: 2 10 2 Linear Search1 x Compite Undo Cut Copy Paste Find... Close Source Code class Linear Search1 { public static void main(String[] args) { Scanner console = new Scanner(System.in): int[] n = { 32, 27. 64, 18, 95, 14, 90, 27, 70, 60, 27); int location, seekValue, 1; printList(n): System.out.print(" Enter a value to seek: "); seekValue = console.nextInt(); location = find( n, seekValue); if( location >= 0 ) System.out.println("Element found at location + location): else System.out.println("Element not found in list"); System.out.printf(" Calling findth( n. ?): "); for( 1=1; i %d ", 1, findth(n. 27. 1)): System.out.printf(" Calling findAll( n ) : "); intl z - findAll( n, 27 ); printlist( z ): public static void printList( int[] n ) System.out.printf(" vals Linear Searchl X Compile Undo Cut Copy Paste Find Close Source Code public static void printList( int[n ) { System.out.printf("vals: "); for(int i=0; i<.length>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions

Question

3. Is there opportunity to improve current circumstances? How so?

Answered: 1 week ago

Question

2. What do you believe is at the root of the problem?

Answered: 1 week ago