#include <stdio.h>
int linear_search(int a[], int k, int se);
int main()
{
int arr[100], n, i, pos, se, choice;
printf("\n= = = = = = = = = = Linear search in C = = = = = = = = = =\n");
printf("How many integers you wanna store..? : ");
scanf("%d", &n);
printf("Enter %d integers into the array:\n", n);
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
do
{
printf("Press 1 to continue\n");
printf("Press 0 to terminate\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
printf("The array elements are:\n");
for(i=0; i<n; i++)
{
printf("%d\t",arr[i]);
}
printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
printf("Enter the elements you wanna search: ");
scanf("%d",&se);
pos=linear_search(arr,n,se);
if(pos==0)
{
printf("\n%d is not present in array.!!\n", se);
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
continue;
}
printf("\nThe element %d is present at -%d- position:\n",se,pos);
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
continue;
case 0:
printf("\nProgram Terminated\n");
return;
default:
printf("\nInvalid Input..!!\n");
}
}while(1);
}
int linear_search(int arr[], int k, int se)
{
int i;
for (i=0; i<k; i++)
{
if (arr [i]==se)
{
return i+1;
}
}
return 0;
}
OutOut:
manoj@ubuntu:~/cp$ gcc linearsearch.c -o linearsearch
manoj@ubuntu:~/cp$ ./linearsearch
= = = = = = = = = = Linear search in C = = = = = = = = = =
How many integers you wanna store..? : 5
Enter 5 integers into the array:
1
2
3
4
5
Press 1 to continue
Press 0 to terminate
1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
The array elements are:
1 2 3 4 5
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter the elements you wanna search: 5
The element 5 is present at -5- position:
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Press 1 to continue
Press 0 to terminate
1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
The array elements are:
1 2 3 4 5
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter the elements you wanna search: 1
The element 1 is present at -1- position:
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Press 1 to continue
Press 0 to terminate
1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
The array elements are:
1 2 3 4 5
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter the elements you wanna search: 3
The element 3 is present at -3- position:
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Press 1 to continue
Press 0 to terminate
1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
The array elements are:
1 2 3 4 5
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter the elements you wanna search: 10
10 is not present in array.!!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Press 1 to continue
Press 0 to terminate
0
Program Terminated
manoj@ubuntu:~/cp$
int linear_search(int a[], int k, int se);
int main()
{
int arr[100], n, i, pos, se, choice;
printf("\n= = = = = = = = = = Linear search in C = = = = = = = = = =\n");
printf("How many integers you wanna store..? : ");
scanf("%d", &n);
printf("Enter %d integers into the array:\n", n);
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
do
{
printf("Press 1 to continue\n");
printf("Press 0 to terminate\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
printf("The array elements are:\n");
for(i=0; i<n; i++)
{
printf("%d\t",arr[i]);
}
printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
printf("Enter the elements you wanna search: ");
scanf("%d",&se);
pos=linear_search(arr,n,se);
if(pos==0)
{
printf("\n%d is not present in array.!!\n", se);
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
continue;
}
printf("\nThe element %d is present at -%d- position:\n",se,pos);
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
continue;
case 0:
printf("\nProgram Terminated\n");
return;
default:
printf("\nInvalid Input..!!\n");
}
}while(1);
}
int linear_search(int arr[], int k, int se)
{
int i;
for (i=0; i<k; i++)
{
if (arr [i]==se)
{
return i+1;
}
}
return 0;
}
OutOut:
manoj@ubuntu:~/cp$ gcc linearsearch.c -o linearsearch
manoj@ubuntu:~/cp$ ./linearsearch
= = = = = = = = = = Linear search in C = = = = = = = = = =
How many integers you wanna store..? : 5
Enter 5 integers into the array:
1
2
3
4
5
Press 1 to continue
Press 0 to terminate
1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
The array elements are:
1 2 3 4 5
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter the elements you wanna search: 5
The element 5 is present at -5- position:
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Press 1 to continue
Press 0 to terminate
1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
The array elements are:
1 2 3 4 5
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter the elements you wanna search: 1
The element 1 is present at -1- position:
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Press 1 to continue
Press 0 to terminate
1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
The array elements are:
1 2 3 4 5
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter the elements you wanna search: 3
The element 3 is present at -3- position:
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Press 1 to continue
Press 0 to terminate
1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
The array elements are:
1 2 3 4 5
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter the elements you wanna search: 10
10 is not present in array.!!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Press 1 to continue
Press 0 to terminate
0
Program Terminated
manoj@ubuntu:~/cp$
No comments:
Post a Comment