-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinearSearchBackFor.c
67 lines (63 loc) · 1.45 KB
/
linearSearchBackFor.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
#include <stdio.h>
int main()
{
int arr[10] = {10, 22, 12, 20, 55, 82, 100, 21, 50, 52};
int num;
int pos = -1;
char op;
printf("Original array: ");
for (int i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
printf("\nEnter an element to be searched: ");
scanf("%d", &num);
printf("Enter the mode for searching - f for forward or b for backward: ");
scanf(" %c", &op);
switch (op)
{
case 'b':
for (int i = 9; i >= 0; i--)
{
if (arr[i] == num)
{
pos = 10 - i;
break;
}
}
break;
case 'f':
for (int i = 0; i < 10; i++)
{
if (arr[i] == num)
{
pos = i + 1;
break;
}
}
break;
default:
printf("Invalid option\n");
return 1;
}
if (pos != -1)
{
printf("%d found at position %d\n", num, pos);
}
else
{
printf("%d not found in the array\n", num);
}
return 0;
}
// Output:
// f
// Original array: 10 22 12 20 55 82 100 21 50 52
// Enter an element to be searched: 22
// Enter the mode for searching - f for forward or b for backward: f
// 22 found at position 2
// b
// Original array: 10 22 12 20 55 82 100 21 50 52
// Enter an element to be searched: 100
// Enter the mode for searching - f for forward or b for backward: b
// 100 found at position 4