0% found this document useful (0 votes)
315 views3 pages

12 Linear Search Program

This C program document contains code for performing linear search on an array using different methods: 1) A basic linear search that searches for a single occurrence and returns the index if found. 2) An expanded linear search that counts multiple occurrences of an element and reports the count. 3) A linear search function that takes the array, size and element to search as parameters and returns the index. 4) A linear search using pointers that searches through the array using pointer arithmetic and returns the index.

Uploaded by

amishadalal
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)
315 views3 pages

12 Linear Search Program

This C program document contains code for performing linear search on an array using different methods: 1) A basic linear search that searches for a single occurrence and returns the index if found. 2) An expanded linear search that counts multiple occurrences of an element and reports the count. 3) A linear search function that takes the array, size and element to search as parameters and returns the index. 4) A linear search using pointers that searches through the array using pointer arithmetic and returns the index.

Uploaded by

amishadalal
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
You are on page 1/ 3

Linear search c program

#include <stdio.h>

int main()
{
int array[100], search, c, n;

printf("Enter the number of elements in


array\n");
scanf("%d",&n);

printf("Enter %d integer(s)\n", n);

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


scanf("%d", &array[c]);

printf("Enter the number to search\n");


scanf("%d", &search);

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


{
if (array[c] == search) /* if required
element found */
{
printf("%d is present at location %d.\n",
search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n",
search);

return 0;
}
Linear search for multiple
occurrences
#include <stdio.h>

int main()
{
int array[100], search, c, n, count
= 0;

printf("Enter the number of


elements in array\n");
scanf("%d", &n);

printf("Enter %d numbers\n", n);

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


scanf("%d", &array[c]);

printf("Enter the number to


search\n");
scanf("%d", &search);

for (c = 0; c < n; c++) {


if (array[c] == search) {
printf("%d is present at
location %d.\n", search, c+1);
count++;
}
}
C program for linear search
if (count == 0)using function Linear search function using pointers
printf("%d is not present in long linear_search(long *pointer, long
array.\n",
#include search);
<stdio.h> n, long find)
else {
long linear_search(long [], long,%dlong);
printf("%d is present times long c;
in array.\n", search, count);
int main() for (c = 0; c < n; c++) {
{ if (*(pointer+c) == find)
return
long 0;
array[100], search, c, n, position;
} return c;
}
printf("Input number of elements in array\n");
scanf("%ld", &n);
return -1;
}
printf("Input %d numbers\n", n);

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


scanf("%ld", &array[c]);

printf("Input number to search\n");


scanf("%ld",&search);

position = linear_search(array, n, search);

if (position == -1)
printf("%d is not present in array.\n", search);
else
printf("%d is present at location %d.\n", search, position+1);

return 0;
}

long linear_search(long a[], long n, long find) {


long c;

for (c = 0 ;c < n ; c++ ) {


if (a[c] == find)
return c;
}

return -1; }

You might also like