0% found this document useful (0 votes)
38 views5 pages

C Program for Linear Search Implementation

The document contains two questions and answers regarding implementing linear and binary searches using pointers in C language. The first section provides code to implement linear search on an integer array using pointers. It prompts the user for array size and elements, the search item, and prints the position if found or not found. The second section provides code to implement binary search. It sorts the array, prompts for search item, calls a recursive search function that takes pointers to return the position, and prints if found or not found.

Uploaded by

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

C Program for Linear Search Implementation

The document contains two questions and answers regarding implementing linear and binary searches using pointers in C language. The first section provides code to implement linear search on an integer array using pointers. It prompts the user for array size and elements, the search item, and prints the position if found or not found. The second section provides code to implement binary search. It sorts the array, prompts for search item, calls a recursive search function that takes pointers to return the position, and prints if found or not found.

Uploaded by

Wong Kar Wai
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Q. Write a program in ‘C’ language to implement linear search using pointers.

Ans:

/*Write a program for linear searching*/

# include<stdio.h>

main()

int arr[20],n,i,item;

clrscr();

printf("How many elements you want to enter in the array : ");

scanf("%d",&n);

for(i=0; i < n;i++)

printf("Enter element %d : ",i+1);

scanf("%d", &arr[i]);

printf("Enter the element to be searched : ");

scanf("%d",&item);

for(i=0;i < n;i++)

if(item == arr[i])

printf("%d found at position %d\n",item,i+1);


break;

}/*End of for*/

if(i == n)

printf("Item %d not found in array\n",item);

getch();

Q. Write a program in ‘C’ language to implement binary search using pointers.

Ans:

/*binary search using pointers*/

# include<stdio.h>

# include<conio.h>

void main()

int search(int *,int,int,int,int *);

int arr[]={0,1,2,3,4,5,7,12,53,31,78,87,65,45,100,200};

int i,j,n=15,temp,num,pos;

char ans;

clrscr();

printf("Do u want to enter values to array automaticaly y/n:");

scanf("%c",&ans);
if(ans=='n')

printf("Enter number of elts, max is 15 :");

scanf("%d",&n);

printf("Enter %d elements...\n",n);

for(i=0;i<n;i++)

scanf("%d",&arr[i]);

for(i=0;i<n-1;i++)

for(j=i;j<n;j++)

if(arr[i]< arr[j])

temp=arr[j];

arr[j]=arr[i];

arr[i]=temp;

printf("\nEntered array after sorting is...\n");

for(i=0;i<n;i++)

printf("%d ",arr[i]);

sear: printf("\nEnter the number to be searched:");


scanf("%d",&num);

if(search(arr,0,n-1,num,&pos))

printf("Entered number %d found at position %d\n",num,pos+1);

else

printf("Entered number %d not found \n",num);

printf("\Search again y/n :");

scanf(" %c",&ans);

if(ans=='y')goto sear ;

int search(int *arr,int spos,int epos,int num,int *pos)

int mid;

if(spos > epos)

*pos=-1;

return(0);

mid=(epos+spos)/2;

if(*(arr+mid)==num)

*pos=mid;

return(1);
}

if(*(arr+mid)> num)

{ return( search(arr,mid+1,epos,num,pos) ); }

if(*(arr+mid) < num)

{ return( search(arr,spos,mid-1,num,pos) ); }

You might also like