C programming Introduction Write a C program called search.c. Implement two sear
ID: 3910262 • Letter: C
Question
C programming
Introduction
Write a C program called search.c. Implement two search functions which determine the location (index) of value s in an ordered array (ascending order) and return -1 if the value s is not present in the array. Each function uses a different algorithm to locate a value in an array.
Signature of two functions:
Instructions
In your sequential_search function, simply scan through the array and compares each array element with the value you want to search. If a match occurs the index of the matched element will be returned. If no match is found after the full scan, -1 will be returned.
In your binary_search function, considers the element at the middle of the array where middle = (left + right) / 2. The variable left and right store the left and right index that defines the current section of the array that may contain your toSearch value. At the first round, when you are looking at the whole array, left is 0 and right is size - 1. It then determines if the value of the middle element in section of the array is greater, less, or equal to toSearch value. If it is equal to toSearch, the value has been found and the index location is returned. If it is greater than toSearch, your toSearch may be located in the left half of the current section, then your left will stay same and your right will be changed to the position on the left next to the middle. If the value is less than toSearch, your toSearch may be located in the right half of the current section, then your left should be changed to the the position on the right next to the middle and your right will stay same. This process of halving the array and looking at the middle element is repeated until the element is found or not located in any of the position tested (when left is greater than right). Below is some pseudo code for this logic:
The main program initializes an array of 10 values in ascending order and a value to search (make your numbers). The two search function should be called respectively and the result be displayed using the following format depending on the location.
The value of s has been found at location l in the array. Or The value of s is not present in the array. Replace s and l with the value to search and the index (if found).
You may start with the array value as {1, 4, 6, 18, 33, 56, 64, 71, 99, 102} and set value to search to 64 to try.
here is the skeleton code
#include <stdio.h>
/**
* Searches a list of values linearly for a specific value.
*
* list - an array of numbers
* size - the size of the array
* toSearch - the value to search
*
* Returns the index where the value is stored first
* in the array or -1 if there is no matching value.
*/
int sequential_search(int *list, int size, int toSearch) {
// code here
}
/**
* Searches a list of values in a binary fashion for a specific value.
*
* list - an array of numbers
* size - the size of the array
* toSearch - the value to search
*
* Returns the index where the value is stored first
* in the array or -1 if there is no matching value.
*/
int binary_search(int *list, int size, int toSearch) {
// code here
}
int main() {
int numbers[10] = {1, 4, 6, 18, 33, 56, 64, 71, 99, 102};
int value = 60;
// your code here, you may use the given numbers array and change the value to try your code
return 0;
}
Explanation / Answer
#include /** * Searches a list of values linearly for a specific value. * * list - an array of numbers * size - the size of the array * toSearch - the value to search * * Returns the index where the value is stored first * in the array or -1 if there is no matching value. */ int sequential_search(int *list, int size, int toSearch) { // code here for(int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.