Professional Documents
Culture Documents
Specification (5) (B) - Program To Implement Linear Search Algorithm
Specification (5) (B) - Program To Implement Linear Search Algorithm
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
FLOWCHART:
start
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
Stop
Element found in
position i
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*/
}
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
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--