In: Computer Science
Write a C program in Unix which uses a function called search to find
the location of a value in THREE arrays of floats. The function should
take three parameters :
the value to be found
the array to be searched
the size of the array
N.B.!!!! The main program should read in three arrays of varying size
example : array a has twelve elements
array b has six elements
array c has nine elements
array d has four elements
input to these arrays terminated by illegal input(see handout)
the function should return for each array the index where the
value is found. If not found the function should return -1.
#include <stdio.h> int search(float value, float arr[], int size) { for(int i=0; i<size; i++) { if(arr[i] == value) { return i; } } return -1; } int main(void) { float a[12] = {3.4, 4.5, 6.5, 2.2, 2.1, 3.2, 4.5, 4.5, 7.2, 7.1, 8.9, 9.5}; float b[6] = {3.4, 4.5, 6.5, 2.2, 2.1, 3.2}; float c[9] = {3.4, 4.5, 6.5, 2.2, 2.1, 3.2, 4.5, 4.5, 7.2}; float d[4] = {3.4, 4.5, 6.5, 2.2}; printf("Index of 2.1 in a: %d \n", search(2.1, a, 12)); return 0; }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.