7
7
* @author [Ritika Mukherjee](https://github.com/ritikaa17)
8
8
*/
9
9
10
- #include < iostream> // / for IO operations
11
- #include < cassert> // / for assert
10
+ #include < iostream> // / for IO operations
11
+ #include < cassert> // / for assert
12
12
13
13
/* *
14
14
* \brief [Algorithm implementation for linear search]
18
18
* \returns index where the key-value occurs in the array
19
19
* \returns -1 if key-value not found
20
20
*/
21
- int LinearSearch (int *array, int size, int key)
21
+ int LinearSearch (int *array, int size, int key)
22
22
{
23
- for (int i = 0 ; i < size; ++i)
23
+ for (int i = 0 ; i < size; ++i)
24
24
{
25
- if (array[i] == key) {
25
+ if (array[i] == key)
26
+ {
26
27
return i;
27
28
}
28
29
}
@@ -35,10 +36,12 @@ int LinearSearch(int *array, int size, int key)
35
36
* @brief Self-test implementations
36
37
* @returns void
37
38
*/
38
- static void tests () {
39
+ static void tests ()
40
+ {
39
41
int size = 4 ;
40
42
int *array = new int [size];
41
- for (int i = 0 ; i < size; i++) {
43
+ for (int i = 0 ; i < size; i++)
44
+ {
42
45
array[i] = i;
43
46
}
44
47
@@ -47,7 +50,8 @@ static void tests() {
47
50
assert (LinearSearch (array, size, 2 ) == 2 );
48
51
49
52
size = 6 ;
50
- for (int i = 0 ; i < size; i++) {
53
+ for (int i = 0 ; i < size; i++)
54
+ {
51
55
array[i] = i;
52
56
}
53
57
@@ -63,21 +67,23 @@ static void tests() {
63
67
* @brief Main function
64
68
* @returns 0 on exit
65
69
*/
66
- int main () {
70
+ int main ()
71
+ {
67
72
int mode = 0 ;
68
73
69
74
std::cout << " Choose mode\n " ;
70
75
std::cout << " Self-test mode (1), interactive mode (2): " ;
71
76
72
77
std::cin >> mode;
73
78
74
- if (mode == 2 ) {
79
+ if (mode == 2 )
80
+ {
75
81
int size = 0 ;
76
- std::cout << " \n Enter the size of the array: " ;
82
+ std::cout << " \n Enter the size of the array [in range 1-30 ] : " ;
77
83
std::cin >> size;
78
84
79
- while (( size <= 1 ) || ( size >= 30 )) {
80
- std::cout << " Size cannot be less than zero . Please choose another value: " ;
85
+ while (size <= 0 || size > 30 ){
86
+ std::cout << " Size can only be 1-30 . Please choose another value: " ;
81
87
std::cin >> size;
82
88
}
83
89
@@ -86,7 +92,8 @@ int main() {
86
92
87
93
// Input for the array elements
88
94
std::cout << " Enter the array of " << size << " numbers: " ;
89
- for (int i = 0 ; i < size; i++) {
95
+ for (int i = 0 ; i < size; i++)
96
+ {
90
97
std::cin >> array[i];
91
98
}
92
99
@@ -97,15 +104,16 @@ int main() {
97
104
if (index != -1 )
98
105
{
99
106
std::cout << " Number found at index: " << index << " \n " ;
100
- }
107
+ }
101
108
else
102
109
{
103
110
std::cout << " Array element not found" ;
104
111
}
105
112
delete[] array;
106
113
}
107
- else {
108
- tests (); // run self-test implementations
114
+ else
115
+ {
116
+ tests (); // run self-test implementations
109
117
}
110
118
return 0 ;
111
119
}
0 commit comments