Write a program to read and store n+ve numbers in array and search a number k provided by the user using a. Linear Searchb. Binary Search
Question
Write a program to read and store n+ve numbers in array and search a number k provided by the user using a. Linear Searchb. Binary Search
Solution
Sure, here is a step-by-step guide to write a program that reads and stores positive numbers in an array and performs a linear search and binary search to find a number provided by the user:
-
Start by declaring the necessary variables and arrays. You will need an integer variable
nto store the number of elements in the array, an integer variablekto store the number to be searched, and an integer arrayarrto store the positive numbers. -
Prompt the user to enter the value of
nand read it from the input. -
Create a loop to read
npositive numbers from the user and store them in thearrarray. Make sure to validate that the numbers entered are positive. -
Prompt the user to enter the number
kto be searched and read it from the input. -
Implement the linear search algorithm to find
kin thearrarray. Start from the first element and compare it withk. If a match is found, print the index of the element. If the end of the array is reached without finding a match, print a message indicating thatkis not found. -
Sort the
arrarray in ascending order using any sorting algorithm (e.g., bubble sort, insertion sort, etc.). -
Implement the binary search algorithm to find
kin the sortedarrarray. Set the lower boundlowto the first index of the array and the upper boundhighto the last index. Repeat the following steps untillowis less than or equal tohigh:- Calculate the middle index
midas the average oflowandhigh. - If the element at index
midis equal tok, print the index and exit the loop. - If the element at index
midis greater thank, sethightomid - 1. - If the element at index
midis less thank, setlowtomid + 1.
- Calculate the middle index
-
If the loop in step 7 completes without finding a match, print a message indicating that
kis not found in the array.
That's it! You have now implemented a program that reads and stores positive numbers in an array and performs a linear search and binary search to find a number provided by the user.
Similar Questions
Write a program to perform binary search on a list of integers given below, to search for an element input by the user. If it is found display the element along with its position, otherwise display the message "Search element not found".5, 7, 9, 11, 15, 20, 30, 45, 89, 97
Write a C program to Search a Key string using Binary search TechniqueWrite a program to search a key string in the given array of strings using binary search.At the time of execution, the program should print the message on the console as:Enter value of n : For example, if the user gives the input as:Enter value of n : 4Next, the program should print the messages one by one on the console as:Enter string for a[0] : Enter string for a[1] : Enter string for a[2] : Enter string for a[3] : if the user gives the input as:Enter string for a[0] : AppleEnter string for a[1] : OrangeEnter string for a[2] : KiwiEnter string for a[3] : MangoNext, the program should print the message on the console as:Enter key string : if the user gives the input as:Enter key string : Kiwithen the program should print the result as:After sorting the strings in the array areValue of a[0] = AppleValue of a[1] = KiwiValue of a[2] = MangoValue of a[3] = OrangeThe key string Mango is found at the position 2Similarly if the key element is given as Litchi for the above case then the program should print the output as "The key string Litchi is not found in the array".Fill in the missing code so that it produces the desired result.Sample Test CasesTest Case 1:Expected Output:Enter·value·of·n·:·4Enter·string·for·a[0]·:·AppleEnter·string·for·a[1]·:·BananaEnter·string·for·a[2]·:·OrangeEnter·string·for·a[3]·:·MangoEnter·key·string·:·MangoAfter·sorting·the·strings·in·the·array·areValue·of·a[0]·=·AppleValue·of·a[1]·=·BananaValue·of·a[2]·=·MangoValue·of·a[3]·=·OrangeThe·key·string·Mango·is·found·at·the·position·2
Alex wants to create a program to search a target value in a sorted array. The program should input the array size and elements, as well as the target value. Utilizing binary search, it should determine if the target is present, and if so, provide the index. If not, display a message indicating its absence.Assist Alex in completing the program efficiently.Input format :The first line of input consists of an integer N, representing the size of the sorted array.The second line consists of N space-separated integers, the elements of the sorted array in ascending order.The third line consists of an integer target, the value to search for in the array.Output format :If the target is present in the array, print "The target value X is present at index Y", where X is the target element and Y is the index position (index starts from 0).If the target is not present, print "The target value X is not present in the array", where X is the target element.
Program to implement linear search.
Program to demonstrate the use of Binary Search to search a given element in a sorted array in ascending order.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.