-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18 jan 2024 linear search in array.c
93 lines (70 loc) · 1.43 KB
/
18 jan 2024 linear search in array.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
86
87
88
89
90
91
92
93
/*
sorting -
bubble
insertion
selection
quick
merge
searching -
linear
binary
*/
/*
linear search - unsorted array
time complexity - O(n)
space complexity - O(1)
traverse the array one by one element each time and also search the element at same time
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int size, choice, value, i;
printf("How many elements you want to enter in the array\n");
scanf("%d", &size);
int array[size];
printf("Enter 1 for auto generated array\n");
printf("Enter 2 for manually entering element\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
srand(time(NULL));
for (i = 0; i < size; i++)
array[i] = rand() % 100;
}
break;
case 2:
{
for (i = 0; i < size; i++)
{
printf("Enter element %d\n", i + 1);
scanf("%d", &array[i]);
}
}
break;
default:
printf("\nInvalid choice");
break;
}
printf("Enter the element you want to enter\n");
scanf("%d", &value);
printf("\tThe array is\n\t");
for (i = 0; i < size; i++)
printf("%d ", array[i]);
for (i = 0; i < size; i++)
{
if (value == array[i])
{
printf("\nElement found at location %d", i + 1);
break;
}
}
if (i == size)
{
printf("\nelement not found");
}
return 0;
}