Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added WarmUp_10 #2

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"githubPullRequests.ignoredPullRequestBranches": [
"main"
]
}
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 4 additions & 0 deletions Class_Example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

add_executable(
print_args
print_args.c)
4 changes: 1 addition & 3 deletions WarmUp_10/print_args.c → Class_Example/print_args.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions WarmUp_1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_executable(
hello_world
hello_world.c)
7 changes: 7 additions & 0 deletions WarmUp_1/hello_world.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char * * argv) {
printf("Hello, World!\n");
return EXIT_SUCCESS;
}
6 changes: 2 additions & 4 deletions WarmUp_10/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@

add_executable(
print_args
print_args.c)

command_line
command_line.c)
8 changes: 8 additions & 0 deletions WarmUp_10/command_line.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>

int main(int argc, char *argv[]) {
for (int i = 1; i < argc; i++) {
printf("%s\n", argv[i]);
}
return 0;
}
3 changes: 3 additions & 0 deletions WarmUp_2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_executable(
basic_calculator
basic_calculator.c)
24 changes: 24 additions & 0 deletions WarmUp_2/basic_calculator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <stdlib.h>

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;
}
3 changes: 3 additions & 0 deletions WarmUp_3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_executable(
array_operations
array_operations.c)
14 changes: 14 additions & 0 deletions WarmUp_3/array_operations.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>

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;
}
3 changes: 3 additions & 0 deletions WarmUp_4/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_executable(
reverse_string
reverse_string.c)
14 changes: 14 additions & 0 deletions WarmUp_4/reverse_string.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>
#include <string.h>

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;
}
3 changes: 3 additions & 0 deletions WarmUp_5/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_executable(
matrix_operations
matrix_operations.c)
46 changes: 46 additions & 0 deletions WarmUp_5/matrix_operations.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <stdio.h>

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;
}
3 changes: 3 additions & 0 deletions WarmUp_6/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_executable(
factorial
factorial.c)
14 changes: 14 additions & 0 deletions WarmUp_6/factorial.c
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
3 changes: 3 additions & 0 deletions WarmUp_7/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_executable(
linear_search
linear_search.c)
23 changes: 23 additions & 0 deletions WarmUp_7/linear_search.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>

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;
}
3 changes: 3 additions & 0 deletions WarmUp_8/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_executable(
linked_lists
linked_lists.c)
63 changes: 63 additions & 0 deletions WarmUp_8/linked_lists.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <stdio.h>
#include <stdlib.h>

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;
}
3 changes: 3 additions & 0 deletions WarmUp_9/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_executable(
file_operations
file_operations.c)
27 changes: 27 additions & 0 deletions WarmUp_9/file_operations.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>

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;
}