-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
76 lines (66 loc) · 1.8 KB
/
main.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
/*
* Name: Aryan Puthran
* Date: 10/13/2023
* Course: CPT_S 121
* Assignment: Sequential Search
*
* Description: This program will search through an array and find a
* specified element. Sequential search traverses the array starting
* from the left and moving to the right until found.
*/
// Include required libraries
#include <stdio.h>
// List function prototypes
int sequentialSearch(int values[], int target, int valuesLength);
int main()
{
// Declare required variables - can be edited
int values[5] = { 0, 5, -2, 2, 23 };
int target = -2;
// Declare required variables - do not edit (is overwritten)
int valuesLength = -1, result = -1;
// Call sequentialSearch() and find valuesLength using memory
valuesLength = sizeof(values) / sizeof(values[0]);
result = sequentialSearch(values, target, valuesLength);
// Print details
printf("Searching array for %d...\n", target);
printf("%d is at the index %d.", target, result);
// Return a code, "success"
return 0;
}
/*
* Description: Traverses an array and finds a target number.
* Date: 10/13/2023
*
* Preconditions: valuesLength is in the range [0, length of values], values[] is valid.
* Postconditions: The index of the element is returned, -1 if it's not
* found.
*/
int sequentialSearch(int values[], int target, int valuesLength)
{
// Declare required variables
int result = -1, isFound = 0, index = 0;
// Use a while loop so you can stop once the item is found
while (index < valuesLength && !isFound)
{
// Determines if value at current index matches target
if (values[index] == target)
{
// Update isFound
isFound = 1;
}
else
{
// Increment index to check next element
index++;
}
}
if (!isFound)
{
return -1;
}
else
{
return index;
}
}