Compare commits

..

2 Commits

Author SHA1 Message Date
f73cfbeef9
cs-2233: complete assignment 3 2024-02-12 04:52:02 -06:00
7523a5aa42
cs-2124: checkin Assignment 2 2024-02-12 04:52:02 -06:00
16 changed files with 293 additions and 32 deletions

View File

@ -3,3 +3,5 @@ CMakeFiles
CMakeCache.txt
cmake_install.cmake
README.pdf
*.tex
.tex-out

View File

@ -0,0 +1,47 @@
* Assignment 2
- ABC123: =zfp106=
- Name: =Price Hiller=
- Course: =CS2124=
- Section: =0C3=
- Semester: =Spring 2024=
** Source Code
The full source code for this project can be found at [[https://git.orion-technologies.io/Price/college/src/branch/Development/Spring-2023/CS-2124/Assignment-2]]
** Running the Programs
1. Install [[https://cmake.org/download/][cmake]] version 3.25 or greater.
2. Ensure you have a recent version of ~make~ at the time of writing. This project successfully
compiles with ~GNU make~ version ~4.4.1~.
3. Go the directory with ~CMakeLists.txt~ and run ~cmake .~ to generate a Makefile.
4. Run ~make all~ to compile all the programs.
5. Go into the newly created ~bin~ directory where all the compiled programs will be output to
6. Programs will be named ~PartOne~, ~PartTwo~, and ~PartThree~
** Program Outputs
*** Part One
C program to Check for balanced Parentheses in an Expression using Stack. The program will
check if the expression has balanced Parentheses.
1. [[./assets/PartOne/img1.png]]
2. [[./assets/PartOne/img2.png]]
3. [[./assets/PartOne/img3.png]]
4. [[./assets/PartOne/img4.png]]
5. [[./assets/PartOne/img5.png]]
*** Part Two
Write a program which will take input (Infix expression) from user and converts the expression
to Postfix expression (using stacks).
1. [[./assets/PartTwo/img1.png]]
2. [[./assets/PartTwo/img2.png]]
3. [[./assets/PartTwo/img3.png]]
4. [[./assets/PartTwo/img4.png]]
*** Part Three
Implement code that will tell you the time binary search took when implemented through both a
recursive method and iterative method
1. [[./assets/PartThree/img1.png]]

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 211 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

View File

@ -3,6 +3,8 @@
#include <stdbool.h>
#include <stdlib.h>
// This code is pretty bad in all honesty, but I *really* didn't want to play
// the void pointer game.
#define DECL_VEC(type_name, type) \
typedef struct { \
size_t elem_count; \
@ -59,11 +61,17 @@
} \
\
type type_name##_pop(type_name *v) { \
type popped_value = v->data[v->elem_count - 1]; \
type popped_value = *type_name##_get(v, v->elem_count - 1); \
type_name##_del(v, v->elem_count - 1); \
return popped_value; \
} \
\
type type_name##_first(type_name *v) { \
type found_value = *type_name##_get(v, 0); \
type_name##_del(v, 0); \
return found_value; \
} \
\
type *type_name##_peek(type_name *v) { \
return type_name##_get(v, v->elem_count - 1); \
} \

View File

@ -1,39 +1,73 @@
#include "./lib/lib.h"
#include "lib/lib.h"
#include "lib/vec.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
DECL_VEC(IntVec, int);
DECL_VEC(CharVec, char);
int main(int argc, char *argv[]) {
print_header();
IntVec v = IntVec_new(2);
IntVec_push(&v, 1);
IntVec_push(&v, 2);
IntVec_push(&v, 99);
IntVec_push(&v, 3);
printf("%d\n", *IntVec_peek(&v));
IntVec_pop(&v);
printf("%d\n", *IntVec_peek(&v));
IntVec_pop(&v);
printf("%d\n", *IntVec_peek(&v));
printf("%d\n", *IntVec_get(&v, 0));
CharVec cv = CharVec_new(3);
CharVec_push(&cv, 'a');
CharVec_push(&cv, 'b');
CharVec_pop(&cv);
CharVec_push(&cv, 'c');
CharVec_push(&cv, 'd');
CharVec_pop(&cv);
CharVec_push(&cv, 'f');
printf("CharVec has: %zu\n", cv.elem_count);
for (int i = 0; i < cv.elem_count; i++) {
// printf("Getting out of %d\n", i);
printf("%c", *CharVec_get(&cv, i));
CharVec readStdinToNewline() {
CharVec cvec = CharVec_new(2);
int ch;
while (EOF != (ch = fgetc(stdin)) && ch != '\n') {
CharVec_push(&cvec, ch);
}
return cvec;
}
bool pairedBraces(char open_brace, char close_brace) {
if (open_brace == '\0')
return true;
int asciiDiff = 2;
if (open_brace == '(' || open_brace == ')') {
asciiDiff = 1;
}
return ((int)open_brace - ((int)close_brace - asciiDiff)) == 0;
}
bool checkCharVecBalance(CharVec *cvec) {
int balance = 0;
CharVec braceStack = CharVec_new(cvec->elem_count);
char lastOpenBrace = '\0';
while (cvec->elem_count != 0) {
char val = CharVec_first(cvec);
switch (val) {
case '(':
case '[':
case '{':
CharVec_push(&braceStack, val);
balance++;
break;
case ')':
case ']':
case '}':
if (!pairedBraces(CharVec_pop(&braceStack), val))
return 0;
balance--;
break;
}
}
if (balance == 0) {
return -1;
} else {
return cvec->elem_count;
}
}
int main() {
print_header();
printf("Enter Expression: ");
CharVec cvec = readStdinToNewline();
char *input = malloc(cvec.elem_count + 1);
strcpy(input, cvec.data);
if (checkCharVecBalance(&cvec)) {
printf("It's balanced (^_^)\n");
return EXIT_SUCCESS;
} else {
printf("It's Not Balanced (0_0)\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,104 @@
#include "lib/lib.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void q_sort(int arr[], int low, int high) {
if (low < high) {
int p = partition(arr, low, high);
q_sort(arr, low, p - 1);
q_sort(arr, p + 1, high);
}
}
int bsearch_iterative(int arr[], int key, int len) {
int low = 0;
int high = len;
while (low < high) {
int mid = (low + high) / 2;
if (key == arr[mid]) {
return mid;
} else if (key > arr[mid]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
int bsearch_recursive(int arr[], int key, int low, int high) {
if (low > high) {
return -1;
}
int mid = (low + high) / 2;
if (key == arr[mid]) {
return mid;
} else if (key > arr[mid]) {
return bsearch_recursive(arr, key, mid + 1, high);
} else {
return bsearch_recursive(arr, key, low, mid - 1);
}
}
int main() {
print_header();
printf("Enter 5 elements:\n");
int arr_len = 5;
int arr[arr_len];
for (int i = 0; i < arr_len; i++) {
scanf("%d", &arr[i]);
}
printf("\nEnter element to search for:\n");
int search_element;
scanf("%d", &search_element);
q_sort(arr, 0, arr_len);
clock_t tic, toc;
printf("\n\n--------\n=> Binary Search (Iterative Approach)");
tic = clock();
int b_iter_index = bsearch_iterative(arr, search_element, arr_len);
toc = clock();
if (b_iter_index == -1) {
printf("Unable to find the element '%d'!\n", search_element);
} else {
printf("Element found at index %d\n", b_iter_index);
}
printf("Total time taken by CPU (End Time - Start Time)/clock per sec: %f\n--------\n",
(double)(toc - tic) / CLOCKS_PER_SEC);
printf("\n--------\n=> Binary Search (Recursive Approach)");
tic = clock();
int b_rec_index = bsearch_recursive(arr, search_element, 0, arr_len);
toc = clock();
if (b_iter_index == -1) {
printf("Unable to find the element '%d'!\n", search_element);
} else {
printf("Element found at index %d\n", b_rec_index);
}
printf("Total time taken by CPU (End Time - Start Time)/clock per sec: %f\n--------\n",
(double)(toc - tic) / CLOCKS_PER_SEC);
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,66 @@
#include "lib/lib.h"
#include "lib/vec.h"
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
DECL_VEC(CharVec, char);
int priority(char x) {
switch (x) {
case '(':
return 0;
break;
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
}
return 0;
}
CharVec readStdinToNewline() {
CharVec cvec = CharVec_new(2);
int ch;
while (EOF != (ch = fgetc(stdin)) && ch != '\n') {
CharVec_push(&cvec, ch);
}
return cvec;
}
int main() {
print_header();
printf("Enter the Infix Expression:\n");
CharVec stack = CharVec_new(2);
CharVec input = readStdinToNewline();
printf("The Postfix expression:\n ");
while (input.elem_count != 0) {
char ch = CharVec_first(&input);
if (ch == ' ') {
continue;
} else if (isalnum(ch)) {
printf("%c ", ch);
} else if (ch == '(') {
CharVec_push(&stack, ch);
} else if (ch == ')') {
char x;
while ((x = CharVec_pop(&stack)) != '(') {
printf("%c ", x);
}
} else {
while (stack.elem_count != 0 && priority(*CharVec_peek(&stack)) >= priority(ch))
printf("%c ", CharVec_pop(&stack));
CharVec_push(&stack, ch);
}
}
while (stack.elem_count != 0) {
printf("%c ", CharVec_pop(&stack));
}
printf("\nEnd of Program\n");
return EXIT_SUCCESS;
}