commit 9fa339652bc4c14145121fb5933732f6ffe6fd7e Author: SowinskiBraeden Date: Mon Sep 15 18:52:55 2025 -0700 initial commit diff --git a/lab01/Makefile b/lab01/Makefile new file mode 100644 index 0000000..10d7030 --- /dev/null +++ b/lab01/Makefile @@ -0,0 +1,26 @@ +# Makefile +# Compiler and flags +CC = gcc +CFLAGS = -Wall -std=c11 + +# Source files +SRCS = hello.c + +# Output +TARGET = hello.out + +# Default target +all: $(TARGET) + +# Rule to build the executable +$(TARGET): $(SRCS) + $(CC) $(CFLAGS) $(SRCS) -o $(TARGET) + +# Rule to run the program +run: $(TARGET) + ./$(TARGET) + +# Rule to clean generated files +clean: + rm -f $(TARGET) + diff --git a/lab01/hello.c b/lab01/hello.c new file mode 100644 index 0000000..a104b37 --- /dev/null +++ b/lab01/hello.c @@ -0,0 +1,7 @@ +#include + +int main() { + printf("Hello World\n"); + return 0; +} + diff --git a/lab01/hello.out b/lab01/hello.out new file mode 100755 index 0000000..aed49e8 Binary files /dev/null and b/lab01/hello.out differ diff --git a/lab02/Makefile b/lab02/Makefile new file mode 100644 index 0000000..455b9d4 --- /dev/null +++ b/lab02/Makefile @@ -0,0 +1,25 @@ +# Makefile +# Compiler and flags +CC = gcc +CFLAGS = -Wall -std=c11 + +# Source files +SRCS = lab2.c + +# Output +TARGET = lab2.out + +# Default target +all: $(TARGET) + +# Rule to build the executable +$(TARGET): $(SRCS) + $(CC) $(CFLAGS) $(SRCS) -o $(TARGET) + +# Rule to run the program +run: $(TARGET) + ./$(TARGET) + +# Rule to clean generated files +clean: + rm -f $(TARGET) diff --git a/lab02/lab2.c b/lab02/lab2.c new file mode 100644 index 0000000..959db36 --- /dev/null +++ b/lab02/lab2.c @@ -0,0 +1,57 @@ +#include + +void multiplicationTable(int n) { + printf("Multiplication table of %d\n", n); + + for (int i = 1; i <= 10; i++) { + printf("%d x %d = %d\n", n, i, n * i); + } + + printf("\n"); +} + +void numberAnalysis(int n) { + int c = (n > 0) - (n < 0); + + switch (c) { + case -1: // n is negative + multiplicationTable(n); + + printf("Sum not applicable for negative numbers\n"); + printf("The number is Negative.\n"); + + break; + case 0: // n is zero + multiplicationTable(n); + + printf("Sum not applicable for zero\n"); + printf("The number is Zero.\n"); + + break; + default: // n is positive + multiplicationTable(n); + + int sum = 0; + int current = 0; + while (current <= n) { + sum += current; + current++; + } + + printf("Sum of 1 to %d = %d\n", n, sum); + printf("The number is Positive.\n"); + + break; + } +} + +int main(void) { + int number; + + printf("Enter a number: "); + scanf("%d", &number); + + numberAnalysis(number); + + return 0; +} diff --git a/lab02/main.out b/lab02/main.out new file mode 100755 index 0000000..11415f1 Binary files /dev/null and b/lab02/main.out differ diff --git a/testing/main.c b/testing/main.c new file mode 100644 index 0000000..f2833fd --- /dev/null +++ b/testing/main.c @@ -0,0 +1,135 @@ +#include +#include +#include +#include +#include + +#define NUMBER_PARTICLES 100 // 100 + +typedef struct { + int x; + int y; + int lastX; + int lastY; + int velocityX; + int velocityY; +} PARTICLE; + +void render(char **space, int WIDTH, int HEIGHT) { + system("clear"); + for (int y = 0; y < HEIGHT; y++) { + for (int x = 0; x < WIDTH; x++) { + printf("%c", space[y][x]); + } + printf("\n"); + } +} + +void updateParticles(PARTICLE *particles, int WIDTH, int HEIGHT) { + for (int i = 0; i < NUMBER_PARTICLES; i++) { + PARTICLE p = particles[i]; + + p.lastX = p.x; + p.lastY = p.y; + p.x = p.x + p.velocityX; + p.y = p.y + p.velocityY; + + if (p.x <= 0 || p.x >= WIDTH - 1) { + p.velocityX *= -1; + } + + if (p.y <= 0 || p.y >= HEIGHT - 1) { + p.velocityY *= -1; + } + + particles[i] = p; + } +} + +void update2DSpace(PARTICLE *particles, char **space) { + for (int i = 0; i < NUMBER_PARTICLES; i++) { + PARTICLE p = particles[i]; + + space[p.y][p.x] = '*'; + } +} + +char** create2DSpace(int WIDTH, int HEIGHT) { + char **space = malloc(HEIGHT * sizeof(char*)); + if (space == NULL) { + perror("Failed to allocate space"); + return NULL; + } + + for (int i = 0; i < HEIGHT; i++) { + + space[i] = malloc(WIDTH * sizeof(char)); + + if (space[i] == NULL) { + perror("Failed to allocate space"); + + for (int j = 0; j < HEIGHT; j++) { + free(space[i]); + } + + free(space); + + return NULL; + } + } + + for (int i = 0; i < HEIGHT; i++) { + for (int j = 0; j < WIDTH; j++) { + space[i][j] = ' '; + } + } + + return space; +} + +PARTICLE* generateParticles(int WIDTH, int HEIGHT) { + PARTICLE *particles = malloc(NUMBER_PARTICLES * sizeof(PARTICLE)); + + if (particles == NULL) { + perror("Failed to allocate memory for particles"); + return NULL; + } + int blockWidth = sqrt((float) NUMBER_PARTICLES); + + int index = 0; + for (int i = -(blockWidth / 2); i < blockWidth / 2; i++) { + for (int j = -(blockWidth / 2); j < blockWidth / 2; j++) { + // printf("%d - (%d, %d)\n", index, WIDTH / 2 + i, HEIGHT / 2 + j); + PARTICLE p = {WIDTH / 2 + i, HEIGHT / 2 + j, WIDTH / 2 + i, HEIGHT / 2 + j, 0, 1}; + particles[index] = p; + index++; + } + } + + return particles; +} + +int main(void) { + struct winsize w; + ioctl(0, TIOCGWINSZ, &w); + + int HEIGHT = w.ws_row; + int WIDTH = w.ws_col; + + char **space = create2DSpace(WIDTH, HEIGHT); + + PARTICLE *particles = generateParticles(WIDTH, HEIGHT); + + // return 0; + int micro = 100000; + + while (1) { + updateParticles(particles, WIDTH, HEIGHT); + space = create2DSpace(WIDTH, HEIGHT); // resset grid space + update2DSpace(particles, space); + render(space, WIDTH, HEIGHT); + usleep(0.5 * micro); + } + + return 0; +} \ No newline at end of file diff --git a/testing/main.out b/testing/main.out new file mode 100755 index 0000000..7435722 Binary files /dev/null and b/testing/main.out differ