Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C program called search.c that prompts the user for 10 distinct positive integers entered in ascending order and stored in an array of

Write a C program called search.c that prompts the user for 10 distinct positive integers entered in ascending order and stored in an array of integers. The program then prompts the user again to search for a value s in the array that either does or doesn't exist in the array. The program then calls two search functions: Linear and Binary, one at a time to determine if the entered value s is stored in the array. Each function uses a different algorithm to locate a value in an array. Both functions return the index of the array location if the array contains the value s and -1 if it does not. The program prints out a text message after each search indicating the location of the searched value in the array or that the value was not found. The signature of the function and the definition is shown below:

Linear: This function simply iterates through the array field by field and compares the stored value in the array with the element it needs to search. If a match occurs the index of the matched element will be returned. Otherwise, -1 will be returned. The parameter s is the value that needs to be searched. The parameter list is the array of integers and the parameter n indicates the number of values in the array.

int searchLinear (int s, int *list, int n);

Binary: Binary search algorithm works on the principle of halving the given array in order to look for the target value. This function has three index variables Left (array starting index), Right (array ending index) and Middle (array midpoint) where Middle = (Left + Right)/2. This is an integer division. If List[middle] = target element then return the variable middle which is the index of the element. In the case that target element is greater than List[Middle], then continue the process on the right half of the array. If it is lesser, then continue the same process on the left half of the array. The process of halving is done only until the Left index variable is less than or equal to the Right index variable. In case the target element is not found , -1 is returned.

int searchBinary (int s, int *list, int n);

Sample Input and Output:

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

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books

Students also viewed these Databases questions