From 59a6b9b5f375dc14e926b998687f9c2f7d015f11 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Thu, 1 Feb 2024 10:45:55 -0600 Subject: [PATCH] cs-2124(lab): add first lab assignment --- Spring-2023/CS-2124/Lab-Work-1/CMakeLists.txt | 9 ++++++ Spring-2023/CS-2124/Lab-Work-1/README.org | 13 ++++++++ Spring-2023/CS-2124/Lab-Work-1/bin/labone | Bin 0 -> 16024 bytes Spring-2023/CS-2124/Lab-Work-1/src/lib.c | 28 ++++++++++++++++++ Spring-2023/CS-2124/Lab-Work-1/src/lib.h | 5 ++++ Spring-2023/CS-2124/Lab-Work-1/src/main.c | 24 +++++++++++++++ 6 files changed, 79 insertions(+) create mode 100644 Spring-2023/CS-2124/Lab-Work-1/CMakeLists.txt create mode 100644 Spring-2023/CS-2124/Lab-Work-1/README.org create mode 100755 Spring-2023/CS-2124/Lab-Work-1/bin/labone create mode 100644 Spring-2023/CS-2124/Lab-Work-1/src/lib.c create mode 100644 Spring-2023/CS-2124/Lab-Work-1/src/lib.h create mode 100644 Spring-2023/CS-2124/Lab-Work-1/src/main.c diff --git a/Spring-2023/CS-2124/Lab-Work-1/CMakeLists.txt b/Spring-2023/CS-2124/Lab-Work-1/CMakeLists.txt new file mode 100644 index 0000000..56649be --- /dev/null +++ b/Spring-2023/CS-2124/Lab-Work-1/CMakeLists.txt @@ -0,0 +1,9 @@ + +cmake_minimum_required(VERSION 3.25) + +set(SOURCES src/lib.h src/lib.c src/main.c) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin) + + +project(LabOne LANGUAGES C) +add_executable(labone ${SOURCES}) diff --git a/Spring-2023/CS-2124/Lab-Work-1/README.org b/Spring-2023/CS-2124/Lab-Work-1/README.org new file mode 100644 index 0000000..cfe9fed --- /dev/null +++ b/Spring-2023/CS-2124/Lab-Work-1/README.org @@ -0,0 +1,13 @@ +* Lab Work - Sorting and Binary Search +Get an array of integer, the target integer, then sort the array and find the target using binary +search +- Create a library for binary search and a sorting algorithm to sort the array and use it in main + file +- Input: + - First line contains one integer $n$, the size of the array + - Second line contains $n$ integers $a_i$, elements of the array + - Third line contains hte search integer/key $m$ +- Output: + - Print hte location of the search integer $m$ in the given array. If not found, print =-1=. +Submit the zipped folder + diff --git a/Spring-2023/CS-2124/Lab-Work-1/bin/labone b/Spring-2023/CS-2124/Lab-Work-1/bin/labone new file mode 100755 index 0000000000000000000000000000000000000000..afc436d16d8eb0d56134fe7598e1e98dd305596d GIT binary patch literal 16024 zcmeHOYj7LY6~2<=#NiPu4~l_87D_14k^IWT4p266Hrdd;n#2tKK{m3qwnSt}NGoEy zl%``S6(Sv%DU@lMDZe`W=#Nb4Fe$^-PQt{2ri?-v#y>iY+X;+90wm?-0fTzZz30f% zVz;GC`)jXf_uTW{^SbBWUEN*Z{h_gCqu=KfT>RqO0=aZRmykLO(X>Xggms8Zg)XiU zSBv?8%!5OhEl5?3>4aZ3Fl`{YI-sc6gcV8k8oEnzLNMhBk)qyA(W$EFlNb;brXrwt z)T_rT@q+rt^dU0Jl;tY^G*pJ7>5?#gngU~5t%spZ1eN&2ZAy>nU6hx-L|G46)GLu* ziS(GBCx1*ipBO$KY0781P6rNoMb#O{M-{i;A<{cU@=P_7XUe(X^`;a3Z~W~by%91v zqUq8D(_1Ac1XK3+UC^VUC5eAcc$jyRzq!WYM#?`^O0Q^6#s`|SPAX$Jr`zwz#(MS- z#P`R0`&-k4Era*2TDh{fKRP(r*>O)tIM|nn_e6qiq4tho+nQ!zniJ7rBA(0*1P3}+ z2Uo8QWmBOx6)*Ou1rD5xo3?ce{{bqPkD3K_SL$O7XonA$`A_IKL1@V=f@o_teCZr`^BnjafNOBLOA`RI*%y|DqhMv7 zaK^Fv!||j@=bS9S@oXy6*=c1X;bbpjkytn*(wTVD=@qDggR@#}+Om0lm(>nGD ztW?`VtG?QVSX(unRt}|GR^7XADAt*6kF@VwlTLR=+gINgix0IAbi_In!M;c&*xDXy z3$?%`g1Hp$fnN0Wr;^k;3v=R!(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CS-2124/Lab-Work-1/src/lib.c b/Spring-2023/CS-2124/Lab-Work-1/src/lib.c new file mode 100644 index 0000000..b8485d0 --- /dev/null +++ b/Spring-2023/CS-2124/Lab-Work-1/src/lib.c @@ -0,0 +1,28 @@ +int binary_search(int arr[], int m, int size) { + int low = 0; + int high = size - 1; + while (low != high) { + int mid = (low + high) / 2; + if (m == arr[mid]) { + return mid; + } else if (m > arr[mid]) { + low = mid + 1; + } else { + high = mid - 1; + }; + } + return -1; +} + +void bubble_sort(int a[], int n) { + int i = 0, j = 0, tmp; + for (i = 0; i < n; i++) { + for (j = 0; j < n - i - 1; j++) { + if (a[j] > a[j+1]) { + tmp = a[j]; + a[j] = a[j+1]; + a[j + 1] = tmp; + } + } + } +} diff --git a/Spring-2023/CS-2124/Lab-Work-1/src/lib.h b/Spring-2023/CS-2124/Lab-Work-1/src/lib.h new file mode 100644 index 0000000..0165c39 --- /dev/null +++ b/Spring-2023/CS-2124/Lab-Work-1/src/lib.h @@ -0,0 +1,5 @@ +#ifndef CUST_LIB +#define CUST_LIB +int binary_search(int arr[], int m, int size); +void bubble_sort(int a[], int n); +#endif // !CUST_LIB diff --git a/Spring-2023/CS-2124/Lab-Work-1/src/main.c b/Spring-2023/CS-2124/Lab-Work-1/src/main.c new file mode 100644 index 0000000..0941a8e --- /dev/null +++ b/Spring-2023/CS-2124/Lab-Work-1/src/main.c @@ -0,0 +1,24 @@ +#include "./lib.h" +#include + +int main(int argc, char *argv[]) { + printf("Enter size of array: "); + int arr_size = -1; + scanf("%d", &arr_size); + int arr[arr_size]; + printf("Enter elements:\n"); + for (int i = 0; i < arr_size; i++) { + scanf("%d", &arr[i]); + } + int m = 0; + printf("Enter search element: "); + scanf("%d", &m); + bubble_sort(arr, arr_size); + printf("Sorted elements to search in: "); + for (int i = 0; i < arr_size; i++) { + printf("%d ", arr[i]); + } + printf("\n"); + int location = binary_search(arr, m, arr_size); + printf("Location of %d was found at %d\n", m, location); +}