0% found this document useful (0 votes)
0 views2 pages

C Programming Ex8

The document outlines a C program for implementing linear search using a pointer to an array. It includes an algorithm and a complete code example that reads an array and searches for a specified element, returning its index if found. The program successfully executes the linear search and provides appropriate output based on the search result.

Uploaded by

Malathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views2 pages

C Programming Ex8

The document outlines a C program for implementing linear search using a pointer to an array. It includes an algorithm and a complete code example that reads an array and searches for a specified element, returning its index if found. The program successfully executes the linear search and provides appropriate output based on the search result.

Uploaded by

Malathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EX.

NO: 8 LINEAR SEARCH USING POINTER TO AN ARRAY

DATE:

AIM

To Write a C program for implementing linear search using a pointer to

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.

You might also like