Int linear search(int arr[], int n, int x)


Download 52.58 Kb.
Sana28.12.2022
Hajmi52.58 Kb.
#1012604
Bog'liq
3-amaliy ish


#include

int linear_search(int arr[], int n, int x)


{
int i;
for (i = 0; i < n; ++i)
{
if (arr[i] == x)
{
return i;
}
}
return -1;
}
int main()
{
int x, n, idx;
int arr[] = { 5,8,1,2,13,7,9,10,11,6 };
// length of array
n = sizeof(arr) / sizeof(int);
printf("\n\n Enter value you want to search: ");
scanf("%d", &x);
idx = linear_search(arr, n, x);
if (idx == -1)
{
printf(" %d is not found in the array \n", x);
}
else
{
printf(" %d is present in the array at index %d \n", x, idx);
}
}

Download 52.58 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling