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

Linear Search Program in C

This document describes the linear search algorithm. It includes the algorithm steps, a flowchart showing the process, an example C program implementing linear search, and instructions for compiling and running the program. The program takes an array as input, searches for a given element, and outputs whether the element was found and its position if so. Sample inputs and outputs are provided showing the program searching arrays and correctly identifying found and not found elements.

Uploaded by

prasad9440024661
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)
166 views5 pages

Linear Search Program in C

This document describes the linear search algorithm. It includes the algorithm steps, a flowchart showing the process, an example C program implementing linear search, and instructions for compiling and running the program. The program takes an array as input, searches for a given element, and outputs whether the element was found and its position if so. Sample inputs and outputs are provided showing the program searching arrays and correctly identifying found and not found elements.

Uploaded by

prasad9440024661
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/ 5

RAGHU INSTITUTE OF TECHNOLOGY

SPECIFICATION
(5)(b). Program to implement linear search
ALGORITHM:
Step 1: Start
Step 2: Declare integer variables a[50],n,f=0,size;
Step 3: Read size
Step 4: i is initialized as 0 in steps of 1 do the condition i<size is checked until it fails
Step 4.1: read a[i]
Step 5:Read n
Step 6: i is initialized as 0 in steps of 1 do the condition i<size is checked until it fails
Step 6.1: check a[i]==n then
Step 6.1.1: f++
Step 6.1.2: stop otherwise goto step 6.1
Step 7: Check f==1 then
Step 7.1: Display element found at position I otherwise
Step 7.2: Display element not found
Step 8: Stop

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

FLOWCHART:
start

declare a[50] , I,n,size and intialise f=0

read size
false
For i=0 in steps of 1 do where i<size
true
read a[i]

read n
false
For i=0 in steps of 1 do where i<size

false

true

a[i]==n

f f+1
Element not found
false

true
f==1

Element not found

Department of Computer Science & Engg

Stop

Element found in
position i

RAGHU INSTITUTE OF TECHNOLOGY

PROGRAM
/* C Program to implement linear search*/
Program name:
/* Done By : C-Faculty
#include<stdio.h>
#include<curses.h>
int main()
{
int a[50],n,f=0,size;
printf(enter the size);
scanf(%d,&size);
printf(enter the elements)
for(i=0;i<size;i++)
{
scanf(%d,&a[i]);
}
printf(enter the element to be searched);
scanf(%d,&n);
for(i=0;i<size;i++)
{
if(a[i]==n)
{
f++;
break;
}
Department of Computer Science & Engg

// wk5b.c
Dated: 15/10/2013*/

RAGHU INSTITUTE OF TECHNOLOGY

}
if(f==1)
printf(element found in %d location,i);
else
printf(element not found);
return(0);
}
PROCEDURE FOR EXECUTING THE PROGRAM:
Step 1: After typing the program, press ESC button+shift+: and then type wq(to save the
program and quit)
Step 2: Now compile the program by using the following command
cc wk5b.c lcurses -lm
Step 3: Now go for running the program by using the command
./a.out

Step 4: To create an executing file use the command


cc wk5b.c -curses o linearsearch

EXPECTED I/P AND O/P:


Output (1)
Enter the size of the array 5
Enter ther elements 5 6 7 8 9 10
Enter the element to be searched 9
The element found in 3 location
Output (2)
Enter the size of the array 4
Enter ther elements 1 4 7 6
Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

Enter the element to be searched 4


The element found in 1 location

ORIGINAL OUTPUT :
Output (1)
Enter the size of the array 5
Enter ther elements 5 6 7 8 9 10
Enter the element to be searched 9
The element found in 3 location
Output (2)
Enter the size of the array 10
Enter ther elements 1 2 4 7 8 9 11 13 15 20
Enter the element to be searched 11
The element found in 6 location

--xXx--

Department of Computer Science & Engg

You might also like