C Programming Code For Linear Search Of 1D Array – KLE Tech

Write a Modular C Programming Code for Linear Search of a 1D Array?

CODE:

#include<stdio.h>

void readArray(int[],int);
int linear(int A[],int n,int key);

int main()
{
int n,occ,key;
int arr[100];
printf("Enter the size of Array: ");
scanf("%d",&n);
printf("Enter the elements of array: \n");
readArray(arr,n);
printf("Enter the elements to be searched in array: \n");
scanf("%d",&key);
occ=linear(arr,n,key);

if(occ>0)
{
printf("Search is successful!!!\n");
printf("Occurannce=%d\n",occ);
}
else
printf("Search not successful!!!\n");

}
 

void readArray(int arr[],int n)
{
int i;
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
}

void displayArray(int arr[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
}

int linear(int arr[],int n,int key)
{
int i,occ=0;

for(i=0;i<n;i++)
{
if(key==arr[i])
{
occ++;
}
}
return(occ);
}

OUTPUT

Enter the size of Array: 5
Enter the elements of array:
3
26
5
6
4
Enter the elements to be searched in array:
5
Search is successful!!!
Occurannce=1

Process returned 0 (0x0) execution time : 12.099 s
Press any key to continue.

 

Please find some more codes of 1D Arrays, 2D Arrays, Pointers on the below page:

Top 100+ C Programming codes – KLE Technological University

 

Leave a Comment