From e2feb409affef574366c22e67ed564318019d1dd Mon Sep 17 00:00:00 2001 From: Kharaj Chakraborty Date: Fri, 27 Mar 2026 19:50:03 +0530 Subject: [PATCH] Update linear_search.c 1. If The Element Is Found - linearsearch() function will return the index of that element which will later get printed out through the main() function. 2. If The Element Is Not Found - linearsearch() function will return 0. --- searching/linear_search.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/searching/linear_search.c b/searching/linear_search.c index 6982512cc4..1e425c4d26 100644 --- a/searching/linear_search.c +++ b/searching/linear_search.c @@ -7,27 +7,29 @@ int linearsearch(int *arr, int size, int val) for (i = 0; i < size; i++) { if (arr[i] == val) - return 1; + return i; } return 0; } int main() { - int n, i, v; + int n, i, v, r; printf("Enter the size of the array:\n"); - scanf("%d", &n); // Taking input for the size of Array + scanf("%d", &n); // Taking input for the size of Array int *a = (int *)malloc(n * sizeof(int)); printf("Enter the contents for an array of size %d:\n", n); for (i = 0; i < n; i++) - scanf("%d", &a[i]); // accepts the values of array elements until the - // loop terminates// + scanf("%d", &a[i]); // accepts the values of array elements until the + // loop terminates// printf("Enter the value to be searched:\n"); - scanf("%d", &v); // Taking input the value to be searched - if (linearsearch(a, n, v)) - printf("Value %d is in the array.\n", v); + scanf("%d", &v); // Taking input the value to be searched + + r = linearsearch(a, n, v); + if (r != 0) + printf("Value %d is in the array at index %d.\n", v, r); else printf("Value %d is not in the array.\n", v);