diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b242572 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "githubPullRequests.ignoredPullRequestBranches": [ + "main" + ] +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a8f5ad..ab4dbff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,9 +25,17 @@ add_subdirectory(hello-lib) add_subdirectory(lwlog-lib) add_subdirectory(lwlog-demo) add_subdirectory(gtest-demo) +add_subdirectory(Class_Example) +add_subdirectory(WarmUp_1) +add_subdirectory(WarmUp_2) +add_subdirectory(WarmUp_3) +add_subdirectory(WarmUp_4) +add_subdirectory(WarmUp_5) +add_subdirectory(WarmUp_6) +add_subdirectory(WarmUp_7) +add_subdirectory(WarmUp_8) +add_subdirectory(WarmUp_9) add_subdirectory(WarmUp_10) - - # You can use add_subdirectory() to add your own work. # Please don't remove the existing subdirectories. diff --git a/Class_Example/CMakeLists.txt b/Class_Example/CMakeLists.txt new file mode 100644 index 0000000..4c52a0b --- /dev/null +++ b/Class_Example/CMakeLists.txt @@ -0,0 +1,4 @@ + +add_executable( + print_args + print_args.c) diff --git a/WarmUp_10/print_args.c b/Class_Example/print_args.c similarity index 94% rename from WarmUp_10/print_args.c rename to Class_Example/print_args.c index 841d0d7..81ff0ad 100644 --- a/WarmUp_10/print_args.c +++ b/Class_Example/print_args.c @@ -4,6 +4,4 @@ int main(int argc, char *argv[]) { for (int i=1; i < argc; i++) { printf("i=%d argv[%d] = %s\n", i, i, argv[i]); - } -} - + } \ No newline at end of file diff --git a/README.md b/README.md index d62688b..25a38d5 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,6 @@ This is a starter repository for COMP 310 (Operating Systems) students. You shou ## Learning objectives - Programming in C -- Separate compilation +- Separate compilation- Build automation using CMake - Build automation using CMake - Unit testing using GoogleTest diff --git a/WarmUp_1/CMakeLists.txt b/WarmUp_1/CMakeLists.txt new file mode 100644 index 0000000..fb1e995 --- /dev/null +++ b/WarmUp_1/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable( + hello_world + hello_world.c) diff --git a/WarmUp_1/hello_world.c b/WarmUp_1/hello_world.c new file mode 100644 index 0000000..41391df --- /dev/null +++ b/WarmUp_1/hello_world.c @@ -0,0 +1,7 @@ +#include +#include + +int main(int argc, char * * argv) { + printf("Hello, World!\n"); + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/WarmUp_10/CMakeLists.txt b/WarmUp_10/CMakeLists.txt index ab1f874..e13d58e 100644 --- a/WarmUp_10/CMakeLists.txt +++ b/WarmUp_10/CMakeLists.txt @@ -1,5 +1,3 @@ - add_executable( - print_args - print_args.c) - + command_line + command_line.c) \ No newline at end of file diff --git a/WarmUp_10/command_line.c b/WarmUp_10/command_line.c new file mode 100644 index 0000000..0852e9b --- /dev/null +++ b/WarmUp_10/command_line.c @@ -0,0 +1,8 @@ +#include + +int main(int argc, char *argv[]) { + for (int i = 1; i < argc; i++) { + printf("%s\n", argv[i]); + } + return 0; +} \ No newline at end of file diff --git a/WarmUp_2/CMakeLists.txt b/WarmUp_2/CMakeLists.txt new file mode 100644 index 0000000..dace314 --- /dev/null +++ b/WarmUp_2/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable( + basic_calculator + basic_calculator.c) diff --git a/WarmUp_2/basic_calculator.c b/WarmUp_2/basic_calculator.c new file mode 100644 index 0000000..0735cb2 --- /dev/null +++ b/WarmUp_2/basic_calculator.c @@ -0,0 +1,24 @@ +#include +#include + +int main(int argc, char * * argv) { + + if (argc < 2) { + printf("Need a number!\n"); + return EXIT_FAILURE; + } + double a, b; + + printf("Enter first number: "); + scanf("%lf", &a); + + printf("Enter second number: "); + scanf("%lf", &b); + + printf("Sum: %lf\n", a + b); + printf("Difference: %lf\n", a - b); + printf("Product: %lf\n", a * b); + printf("Quotient: %lf\n", a / b); + + return 0; +} \ No newline at end of file diff --git a/WarmUp_3/CMakeLists.txt b/WarmUp_3/CMakeLists.txt new file mode 100644 index 0000000..f01edb9 --- /dev/null +++ b/WarmUp_3/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable( + array_operations + array_operations.c) \ No newline at end of file diff --git a/WarmUp_3/array_operations.c b/WarmUp_3/array_operations.c new file mode 100644 index 0000000..0e63fe5 --- /dev/null +++ b/WarmUp_3/array_operations.c @@ -0,0 +1,14 @@ +#include + +int main() { + int numbers[] = {1, 2, 3, 4, 5}; + int sum = 0; + int length = sizeof(numbers) / sizeof(numbers[0]); + for (int i = 0; i < length; i++) { + sum += numbers[i]; + } + double average = sum / (double) length; + printf("Sum: %d\n", sum); + printf("Average: %f\n", average); + return 0; +} \ No newline at end of file diff --git a/WarmUp_4/CMakeLists.txt b/WarmUp_4/CMakeLists.txt new file mode 100644 index 0000000..beecb29 --- /dev/null +++ b/WarmUp_4/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable( + reverse_string + reverse_string.c) \ No newline at end of file diff --git a/WarmUp_4/reverse_string.c b/WarmUp_4/reverse_string.c new file mode 100644 index 0000000..da1a7f0 --- /dev/null +++ b/WarmUp_4/reverse_string.c @@ -0,0 +1,14 @@ +#include +#include + +int main() { + char str[] = "Hello"; + int length = strlen(str); + for (int i = 0, x = length - 1; i < x; i++, x--) { + char temp = str[i]; + str[i] = str[x]; + str[x] = temp; + } + printf("%s\n", str); + return 0; +} \ No newline at end of file diff --git a/WarmUp_5/CMakeLists.txt b/WarmUp_5/CMakeLists.txt new file mode 100644 index 0000000..af7cbaf --- /dev/null +++ b/WarmUp_5/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable( + matrix_operations + matrix_operations.c) \ No newline at end of file diff --git a/WarmUp_5/matrix_operations.c b/WarmUp_5/matrix_operations.c new file mode 100644 index 0000000..404e362 --- /dev/null +++ b/WarmUp_5/matrix_operations.c @@ -0,0 +1,46 @@ +#include + +void addMatrices(int rows, int columns, int a[rows][columns], int b[rows][columns], int sum[rows][columns]) { + for (int i = 0; i < rows; i++) { + for (int x = 0; x < columns; x++) { + sum[i][x] = a[i][x] + b[i][x]; + } + } +} + +void multiplyMatrices(int aRows, int aColumns, int bColumns, int a[aRows][aColumns], int b[aColumns][bColumns], int product[aRows][bColumns]) { + for (int i = 0; i < aRows; i++) { + for (int x = 0; x < bColumns; x++) { + product[i][x] = 0; + for (int y = 0; y < aColumns; y++) { + product[i][x] += a[i][y] * b[y][x]; + } + } + } +} + +void printMatrix(int rows, int columns, int matrix[rows][columns]) { + for (int i = 0; i < rows; i++) { + for (int x = 0; x < columns; x++) { + printf("%d ", matrix[i][x]); + } + printf("\n"); + } +} + +int main() { + int matrix1[2][2] = {{1, 2}, {3, 4}}; + int matrix2[2][2] = {{5, 6}, {7, 8}}; + int sum[2][2]; + int product[2][2]; + + addMatrices(2, 2, matrix1, matrix2, sum); + multiplyMatrices(2, 2, 2, matrix1, matrix2, product); + + printf("Matrix Addition:\n"); + printMatrix(2, 2, sum); + printf("Matrix Multiplication:\n"); + printMatrix(2, 2, product); + + return 0; +} diff --git a/WarmUp_6/CMakeLists.txt b/WarmUp_6/CMakeLists.txt new file mode 100644 index 0000000..b235348 --- /dev/null +++ b/WarmUp_6/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable( + factorial + factorial.c) \ No newline at end of file diff --git a/WarmUp_6/factorial.c b/WarmUp_6/factorial.c new file mode 100644 index 0000000..97a794d --- /dev/null +++ b/WarmUp_6/factorial.c @@ -0,0 +1,14 @@ +public class Factorial { + public static void main(String[] args) { + int number = 5; + System.out.println("Factorial of " + number + " is: " + factorial(number)); + } + + public static int factorial(int n) { + if (n <= 0) { + return 1; + } else { + return n * factorial(n - 1); + } + } +} diff --git a/WarmUp_7/CMakeLists.txt b/WarmUp_7/CMakeLists.txt new file mode 100644 index 0000000..238a858 --- /dev/null +++ b/WarmUp_7/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable( + linear_search + linear_search.c) \ No newline at end of file diff --git a/WarmUp_7/linear_search.c b/WarmUp_7/linear_search.c new file mode 100644 index 0000000..ddca749 --- /dev/null +++ b/WarmUp_7/linear_search.c @@ -0,0 +1,23 @@ +#include + +int linearSearch(int arr[], int length, int target) { + for (int i = 0; i < length; i++) { + if (arr[i] == target) { + return i; + } + } + return -1; +} + +int main() { + int array[] = {1, 4, 7, 9, 12}; + int target = 9; + int length = sizeof(array) / sizeof(array[0]); + int index = linearSearch(array, length, target); + if (index != -1) { + printf("Element found at index: %d\n", index); + } else { + printf("Element not found.\n"); + } + return 0; +} diff --git a/WarmUp_8/CMakeLists.txt b/WarmUp_8/CMakeLists.txt new file mode 100644 index 0000000..311c4d6 --- /dev/null +++ b/WarmUp_8/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable( + linked_lists + linked_lists.c) \ No newline at end of file diff --git a/WarmUp_8/linked_lists.c b/WarmUp_8/linked_lists.c new file mode 100644 index 0000000..183687c --- /dev/null +++ b/WarmUp_8/linked_lists.c @@ -0,0 +1,63 @@ +#include +#include + +struct Node { + int data; + struct Node* next; +}; + +void push(struct Node** head_ref, int new_data) { + struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); + new_node->data = new_data; + new_node->next = (*head_ref); + (*head_ref) = new_node; +} + +void deleteNode(struct Node **head_ref, int key) { + struct Node* temp = *head_ref, *prev; + if (temp != NULL && temp->data == key) { + *head_ref = temp->next; + free(temp); + return; + } + while (temp != NULL && temp->data != key) { + prev = temp; + temp = temp->next; + } + if (temp == NULL) return; + prev->next = temp->next; + free(temp); +} + +void printList(struct Node *node) { + while (node != NULL) { + printf("%d ", node->data); + node = node->next; + } +} + +void deleteList(struct Node** head_ref) { + struct Node* current = *head_ref; + struct Node* next; + while (current != NULL) { + next = current->next; + free(current); + current = next; + } + *head_ref = NULL; +} + +int main() { + struct Node* head = NULL; + push(&head, 7); + push(&head, 1); + push(&head, 3); + push(&head, 2); + printf("\nCreated Linked list is: \n"); + printList(head); + deleteNode(&head, 1); + printf("\nLinked List after Deletion of 1: \n"); + printList(head); + deleteList(&head); + return 0; +} diff --git a/WarmUp_9/CMakeLists.txt b/WarmUp_9/CMakeLists.txt new file mode 100644 index 0000000..8155d20 --- /dev/null +++ b/WarmUp_9/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable( + file_operations + file_operations.c) \ No newline at end of file diff --git a/WarmUp_9/file_operations.c b/WarmUp_9/file_operations.c new file mode 100644 index 0000000..3929243 --- /dev/null +++ b/WarmUp_9/file_operations.c @@ -0,0 +1,27 @@ +#include +#include + +int main() { + char filename[] = "example.txt"; + FILE *fp = fopen(filename, "w"); + if (fp == NULL) { + perror("Error opening file."); + return EXIT_FAILURE; + } + fprintf(fp, "Hello, World!"); + fclose(fp); + + fp = fopen(filename, "r"); + if (fp == NULL) { + perror("Error opening file."); + return EXIT_FAILURE; + } + char line[256]; + while (fgets(line, sizeof(line), fp)) { + printf("%s", line); + } + fclose(fp); + + return 0; +} +