mirror of
https://github.com/RolandWH/ceesort.git
synced 2025-03-14 17:01:36 +00:00
added linear search
This commit is contained in:
parent
8b200c099f
commit
2c9ce86b76
@ -7,6 +7,7 @@
|
||||
|
||||
#include "bubble.h"
|
||||
#include "bsrch.h"
|
||||
#include "lsrch.h"
|
||||
#include "ccolour/colour.h"
|
||||
|
||||
#include <ctype.h>
|
||||
@ -16,7 +17,7 @@
|
||||
#include <string.h>
|
||||
|
||||
|
||||
inline void quit_err(const char* msg)
|
||||
static inline void quit_err(const char* msg)
|
||||
{
|
||||
ChangeColour(msg, RED_FOREGROUND, DEFAULT_COLOR, true);
|
||||
exit(EXIT_FAILURE);
|
||||
@ -62,6 +63,7 @@ int main(int argc, char** argv)
|
||||
printf(
|
||||
"1. Binary search\n"
|
||||
"2. Bubble sort\n"
|
||||
"3. Liniar search\n"
|
||||
"Please pick a mode of operation: "
|
||||
);
|
||||
fgets(choice_str, 16, stdin);
|
||||
@ -70,7 +72,7 @@ int main(int argc, char** argv)
|
||||
// TODO: Add checking that number is in range
|
||||
while (!isint(choice_str))
|
||||
{
|
||||
printf("Sorry but you must enter a number between 1 and 2\n");
|
||||
printf("Sorry but you must enter a number between 1 and 3\n");
|
||||
printf("Try again: ");
|
||||
fgets(choice_str, 16, stdin);
|
||||
choice_str[strcspn(choice_str, "\n")] = 0;
|
||||
@ -108,7 +110,17 @@ int main(int argc, char** argv)
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
else if (choice == 3)
|
||||
{
|
||||
char target_str[16];
|
||||
printf("Enter number to search for: ");
|
||||
fgets(target_str, 16, stdin); target_str[strcspn(target_str, "\n")] = 0;
|
||||
while (!isint(target_str))
|
||||
{ printf("Sorry but you must enter a whole number, try again: "); fgets(target_str, 16, stdin);
|
||||
}
|
||||
int target = atoi(target_str); int result = lsrch(arr, n, target); if (result == -1) puts("Value not found"); else printf("Value found at index: %d\n", result);
|
||||
}
|
||||
|
||||
free(arr);
|
||||
return 0;
|
||||
}
|
||||
|
17
src/lsrch.c
Normal file
17
src/lsrch.c
Normal file
@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Filename: lsrch.c
|
||||
* Author(s): Roland (r.weirhowell@gmail.com)
|
||||
* Description: Perform linear search on an integer array
|
||||
* License: MIT (https://spdx.org/licenses/MIT.html)
|
||||
*/
|
||||
|
||||
#include "lsrch.h"
|
||||
|
||||
int lsrch(int* arr, int n, int target)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
if (arr[i] == target) return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
12
src/lsrch.h
Normal file
12
src/lsrch.h
Normal file
@ -0,0 +1,12 @@
|
||||
/*
|
||||
* Filename: lsrch.h
|
||||
* Author(s): Roland (r.weirhowell@gmail.com)
|
||||
* Description: Header file for lsrch.c
|
||||
* License: MIT (https://spdx.org/licenses/MIT.html)
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
int lsrch(int* arr, int n, int target);
|
||||
|
Loading…
x
Reference in New Issue
Block a user