(C++) CPP Programs - 13
Friday, December 14, 2007
13. Write a C program to input n numbers (integer or real) and store them in an array. Conduct a linear search for a given key number and report success or failure in the form of suitable message.
/* Linear Search*/
#include
#include
void main()
{
int a[32],pos=0,scount=0,key=0,n,i;
clrscr();
printf("Enter the no. of digits\n\a");
scanf("%d",&n);
printf("Enter the elements one by one\n");
for(i=0;i <>
scanf("%d",&a[i]);
printf("Enter the key element to be searched\n\a");
scanf("%d",&key);
for(i=0;i <>
{
if(a[i]==key)
{
scount=1;
pos=i;
}
}
if(scount==1)
printf("Element found at %d and the element is %d\n\a",pos,key);
else
printf("Element not found\n\a");
getch();
}
0 comments:
Post a Comment