Monday, May 23, 2011

Write a program to search an element in unsorted list or sorted list. In this case a sorted list the search starts from the element of 0th location and continue until the element is found or an element whose value is greater than the value being searched is reached.

#include
main()
{
int a[10],i,n,m,c=0,loc=0;
printf("Enter the size of an array");
scanf("%d",&n);
printf("\nEnter the elements of the array");
for(i=0;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the element to search");
scanf("%d",&m);
for(i=0;i<=n;i++)
{
if(a[i]==m)
{
c=1;
loc=i+1;
break;
}
}
if(c==0)
printf("\nThe number is not in the list");
else
printf("\nThe number is found at location %d",loc);
}


Output:

Enter the size of an array5

Enter the elements of the array10
20
30
40
50
enter the element to search22
The number is not in the list
Enter the size of an array5

Enter the elements of the array10
20
30
40
50
enter the element to search20

The number is found at location 1

No comments:

Post a Comment