C Programming Ex8
C Programming Ex8
DATE:
AIM
an array
ALGORITHM
Step 1: Start
Step 2: Declare array ‘a’, pointer p and element to be searched ‘m’;
Step 3: Read the array element
Step 4: Read the element to be searched
Step 5: =-a; f=-1
for each item in the array
if element == m
return element's index as f
Step 6:
if f==-1
print ‘Element not found’
else
print ‘element found at position f’
Step 7: Stop
PROGRAM :
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,m,f, a[15],*p;
clrscr();
printf("enter the size of array:");
scanf("%d",&n);
printf("enter the elements:");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("enter the element to be searched:");
scanf("%d",&m);
p=a;
f=-1;
for(i=0;i<n;i++){
if(m==*(p+i))
{
f=i;
break;
}
}
if(f>=0){
printf("%d is present in the position %d",m,f+1);
} else {
printf("%d is not present in the array",m);
}
getch();
}
RESULT:
Thus, the C for implementing linear search using a pointer to an array
was executed successfully.