Write a C program for sequential search using array data structure

Write a C program for sequential search using array data structure

                            
                                        



#include<stdio.h>

int compcount=0;
int linearsearch(int A[],int nint key){
        int i;
        for(i=0;i<n;i++)
        {
        compcount++;
        if(A[i]== key)
        return i;
        }
    return -1;
        }
void accept (int A[],int n){

int i;
printf("Enter the %d element ",n);
for(i=0;i<n;i++)
scanf("%d",&A[i]); 
}

void main(){

int A[20],n,key,pos;
printf("How many numbers:-");
scanf("%d",&n);
accept(A,n);
printf("Enter the key to be searched :-");
scanf("%d",& key);

pos=linearsearch(A,n,key);
if (pos==-1)

printf("\n %d not foound in the array",key);
else
printf("\n %d found at position %d",key,pos);
printf("\n the total number of comparisons = %d \n ",compcount);

}

Output :-




Post a Comment

0 Comments