-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path6_sequential-search.cpp
More file actions
53 lines (44 loc) · 1.25 KB
/
6_sequential-search.cpp
File metadata and controls
53 lines (44 loc) · 1.25 KB
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
#include <stdio.h>
int main() {
int size, target, count = 0,i;
count++;
// Input the array size from the user
printf("Enter the size of the array: ");
count++;
scanf("%d", &size);
count++;
// Input array elements from the user
int arr[size];
printf("Enter %d elements for the array:\n", size);
count++;
count = count + size + 1;
count = count + size;
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
count++;
}
// Input the target element to search
printf("Enter the element to search: ");
scanf("%d", &target);
count+=2;
// Sequential search algorithm with count variable
count = count + size + 1;
count = count + size;
for (i = 0; i < size; i++) {
count++; // Increment count for each comparison
if (arr[i] == target) {
count+=2;
printf("Element found at index %d\n", i);
break; // Exit the loop if the element is found
}
}
// If the loop completes and the element is not found
if (i == size) {
count+=2;
printf("Element not found in the array\n");
}
// Output the count
count++;
printf("Number of steps: %d\n", count);
return 0; // Exit the program successfully
}