/*Program to implement Linear search in 1-Dimensional array
*****************************************************/
#include<stdio.h>
#include<conio.h>
#define size 20
int searchLinear(int arr[], int number, int item)
{
for(int t = 0; t<number; ++t)
{
if(arr[t] == item)
return(t+1);
}
return -1;
}
int main()
{
int array[size], num, data;
printf("\nHow many elements...?");
scanf("%d",&num);
if(num>20)
num = 20;
for(int i = 0 ; i < num ; ++ i)
{
printf("\nElement number %d : ", i+1);
scanf("%d",&array[i]);
}
printf("\n\nEnter element to be searched... :");
scanf("%d", &data);
int index;
index = searchLinear(array, num, data);
if(index == -1)
printf("\n\nElement not found");
else
printf("\n\nElement found at %d", index);
getch();
return 0;
}
*****************************************************/
#include<stdio.h>
#include<conio.h>
#define size 20
int searchLinear(int arr[], int number, int item)
{
for(int t = 0; t<number; ++t)
{
if(arr[t] == item)
return(t+1);
}
return -1;
}
int main()
{
int array[size], num, data;
printf("\nHow many elements...?");
scanf("%d",&num);
if(num>20)
num = 20;
for(int i = 0 ; i < num ; ++ i)
{
printf("\nElement number %d : ", i+1);
scanf("%d",&array[i]);
}
printf("\n\nEnter element to be searched... :");
scanf("%d", &data);
int index;
index = searchLinear(array, num, data);
if(index == -1)
printf("\n\nElement not found");
else
printf("\n\nElement found at %d", index);
getch();
return 0;
}
No comments:
Post a Comment