-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8960 - Linear Search Iterative.c
85 lines (59 loc) · 1.75 KB
/
8960 - Linear Search Iterative.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**********************************************************************************************************
NAME: CANDIDA RUTH NORONHA
CLASS: SE COMPS B
ROLL NO. : 8960
BATCH: C
TITLE: LINEAR SEARCH ITERATIVE ALGORITHM
SUBMISSION DATE : 22nd February, 2021
**********************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
void linearSearchIterative(int a[],int n,int ele)
{
int flag = 0, i;
for(i = 0; i< n ; i++)
{
if (a[i] == ele) //if consecutive elements not in order then swap
{
flag = 1;
break;
}
}
if (flag== 1)
{
printf("\n %d is present at location %d",ele,i+1);
return i+1;
}
else
printf("\n %d doesn't exist in the array",ele);
}
int main()
{
int a[SIZE], i, n,ele;
printf("\n Enter the number of elements in the array : ");
scanf("%d",&n);
printf("\n Enter the elements of the array : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter the element to search :");
scanf("%d",&ele);
linearSearchIterative(a,n,ele);
printf("\n");
return 0;
}
/**********************************************************************************************************
OUTPUT :
TEST CASE 1:
Enter the number of elements in the array : 5
Enter the elements of the array : 20 4 10 67 27
Enter the element to search :67
67 is present at location 4
TEST CASE 2:
Enter the number of elements in the array : 5
Enter the elements of the array : 20 4 10 67 27
Enter the element to search :36
36 doesn't exist in the array
**********************************************************************************************************/