diff --git a/00-WSLSetup/hello b/00-WSLSetup/hello new file mode 100644 index 0000000..bfdb158 Binary files /dev/null and b/00-WSLSetup/hello differ diff --git a/01-StackMachine/stackm.c b/01-StackMachine/stackm.c index 5d83178..8f9d57f 100644 --- a/01-StackMachine/stackm.c +++ b/01-StackMachine/stackm.c @@ -7,7 +7,7 @@ typedef struct node { int value; - struct node* next; + struct node *next; } node; typedef struct stackm { @@ -16,63 +16,134 @@ typedef struct stackm { void smInit(struct stackm *myStack){ - (myStack->top) = NULL; + myStack->top = NULL; } int smSize(struct stackm *myStack){ - while(myStack->top->next != NULL) { - + node *current = myStack->top; + int count = 0; + while(current != NULL) { + count++; + current = current->next; } - return myStack->top; + return count; } int smPush(struct stackm *myStack, int toStore){ - node node; - node.value = toStore; - - *(myStack->top->next) = node; + + node *newNode = (node *)malloc(sizeof(node)); + if (newNode == NULL) { + return 0; + } else { + newNode->value = toStore; + newNode->next = myStack->top; + myStack->top = newNode; + return 1; + } } int smPop(struct stackm *myStack){ - + if(myStack->top == NULL) { + return 0; + } + node *temp = myStack->top; + myStack->top = temp->next; + free(temp); + return 1; } int smTop(struct stackm *myStack, int* toStore){ - + if (myStack->top == NULL || toStore == NULL) { + return 0; + } else { + toStore = (int*)myStack->top; + return 1; + } } void smClear(struct stackm *myStack){ - + while (myStack->top != NULL) { + node *temp = myStack->top; + myStack->top = temp->next; + free(temp); + } } void smPrint(struct stackm *myStack){ - + node *temp = myStack->top; + printf("==Stack Contents==\n"); + printf("Top -> "); + while (temp != NULL) { + printf("%d ", temp->value); + temp = temp->next; + printf("\n"); + } } int smAdd(struct stackm* myStack){ - + if(smSize(myStack) <= 2){ + return 0; + } else { + int sum = (myStack->top->value) + (myStack->top->next->value); + smPush(myStack,sum); + return 1; + } } int smSub(struct stackm* myStack){ - + if(smSize(myStack) <= 2){ + return 0; + } else { + int result = (myStack->top->value) - (myStack->top->next->value); + smPop(myStack); + smPop(myStack); + smPush(myStack,result); + return 1; + } } int smMult(struct stackm* myStack){ - + if(smSize(myStack) <= 2){ + return 0; + } else { + int sum = (myStack->top->value) * (myStack->top->next->value); + smPush(myStack,sum); + return 1; + } } int smRotate(struct stackm* myStack, int depth){ - + if(depth == 0){ + return 0; + } else { + node *temp = myStack->top; + int count = 1; + while (count < depth && temp != NULL) { + temp = temp->next; + count++; + } + if (temp == NULL) { + return 0; + } + node *nNode = temp; + while (temp->next != NULL) { + temp = temp->next; + } + temp->next = myStack->top; + myStack->top = nNode->next; + nNode->next = NULL; + return 1; + } } #endif \ No newline at end of file diff --git a/02-TeenyTinyShell/ttsh b/02-TeenyTinyShell/ttsh new file mode 100644 index 0000000..b8bdfd9 Binary files /dev/null and b/02-TeenyTinyShell/ttsh differ diff --git a/02-TeenyTinyShell/ttsh.c b/02-TeenyTinyShell/ttsh.c index 9d61ed4..88992a2 100644 --- a/02-TeenyTinyShell/ttsh.c +++ b/02-TeenyTinyShell/ttsh.c @@ -10,6 +10,7 @@ #include #include +#include #define INPUT_MAX 256 #define CMD_MAX 5 @@ -71,6 +72,8 @@ int main() char user_input[INPUT_MAX]; char cmd_strs[CMD_MAX][INPUT_MAX]; + int quit_flag = 0; + // TODO need to be able to get input from // the user in a loop @@ -84,6 +87,11 @@ int main() } // TODO: Figure out how to handle the 'quit' command + if (strcmp(user_input, "quit") == 0){ + printf("Quitting..."); + exit(0); + } + // Chop the input into command strings int cmd_count = parse_commands(user_input, cmd_strs); @@ -101,6 +109,7 @@ int main() // NOTE: the command name is always the first argument // 2) fork a process // 3) execute the command with execvp + } return 0; diff --git a/02-TeenyTinyShell/ttsh.o b/02-TeenyTinyShell/ttsh.o new file mode 100644 index 0000000..1aab76a Binary files /dev/null and b/02-TeenyTinyShell/ttsh.o differ diff --git a/03-MatrixAddition (fork)/mat b/03-MatrixAddition (fork)/mat new file mode 100644 index 0000000..e69de29 diff --git a/03-MatrixAddition (fork)/matAddFork.c b/03-MatrixAddition (fork)/matAddFork.c new file mode 100644 index 0000000..5b9944e --- /dev/null +++ b/03-MatrixAddition (fork)/matAddFork.c @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include + +int main() { + int A[MAX][MAX], B[MAX][MAX], C[MAX][MAX]; + int i, j, k, sum = 0; + int shmid; + int *shm; + + // Allocate shared memory + shmid = shmget(IPC_PRIVATE, MAX * MAX * sizeof(int), 0666 | IPC_CREAT); + if (shmid < 0) { + perror("shmget"); + exit(1); + } + + // Attach shared memory + shm = shmat(shmid, NULL, 0); + if (shm == (int *)-1) { + perror("shmat"); + exit(1); + } + + // Initialize matrices A and B + for (i = 0; i < MAX; i++) { + for (j = 0; j < MAX; j++) { + A[i][j] = i + j; + B[i][j] = i - j; + } + } + + // Fork a child process + pid_t pid = fork(); + + if (pid == -1) { + perror("fork"); + exit(1); + } + else if (pid == 0) { // Child process + for (i = 0; i < MAX; i++) { + for (j = 0; j < MAX; j++) { + sum = 0; + for (k = 0; k < MAX; k++) { + sum += A[i][k] * B[k][j]; + } + *(shm + i * MAX + j) = sum; + } + } + exit(0); + } + else { // Parent process + wait(NULL); + + // Retrieve result from shared memory + for (i = 0; i < MAX; i++) { + for (j = 0; j < MAX; j++) { + C[i][j] = *(shm + i * MAX + j); + } + } + + // Print result matrix C + printf("Matrix C:\n"); + for (i = 0; i < MAX; i++) { + for (j = 0; j < MAX; j++) { + printf("%d ", C[i][j]); + } + printf("\n"); + } + + // Detach and remove shared memory + shmdt(shm); + shmctl(shmid, IPC_RMID, NULL); + munmap(shm); + } + + return 0; +} \ No newline at end of file diff --git a/03-MatrixAddition (fork)/matRead.c b/03-MatrixAddition (fork)/matRead.c new file mode 100644 index 0000000..f643d39 --- /dev/null +++ b/03-MatrixAddition (fork)/matRead.c @@ -0,0 +1,16 @@ +#include + +int main(int argc, char* argv[]) +{ + int rows, columns; + FILE* input = fopen(argv[1], "r"); + fscanf(input, "%d", &rows); + fscanf(input, "%d", &columns); + for(int i = 0; i < rows; i++) { + for(int j = 0; j < columns; j++) { + int value; + fscanf(input, "%d", &value); + } + } + fclose(input); +} diff --git a/CS3841.code-workspace b/CS3841.code-workspace new file mode 100644 index 0000000..813e2f5 --- /dev/null +++ b/CS3841.code-workspace @@ -0,0 +1,12 @@ +{ + "folders": [ + { + "uri": "vscode-remote://wsl+ubuntu/mnt/c/Users/Trevor/Documents/GitRepos/CS3841" + } + ], + "settings": { + "files.associations": { + "stdio.h": "c" + } + } +} \ No newline at end of file