diff --git a/assignment01/project1.c b/assignment01/project1.c index b9a272e..76a9b43 100644 --- a/assignment01/project1.c +++ b/assignment01/project1.c @@ -1,8 +1,27 @@ #include + +/* +* Course: COMP 2510 +* Assignment: Assignment 1 +* Name: Braeden Sowinski +* Student ID: A01385066 +* Date: 2025-09-20 +* Description: Calculate download speeds from +* given file size and download speed +*/ + #define MEGABYTES_TO_MEGABITS 8 -int main(void) { +/** + * main program entry + * + * takes in user input for download speed + * int Mbs and file size in MBs + * and calculates the download speed + */ +int main(void) +{ float downloadSpeedMbs; float fileSizeMB; float downloadTimeSeconds; diff --git a/assignment01/project2.c b/assignment01/project2.c index 3022ab6..62c9ee0 100644 --- a/assignment01/project2.c +++ b/assignment01/project2.c @@ -1,26 +1,51 @@ #include + +/* +* Course: COMP 2510 +* Assignment: Assignment 1 +* Name: Braeden Sowinski +* Student ID: A01385066 +* Date: 2025-09-20 +* Description: Convert centimeters to feet and inches +*/ + + #define CENTIMETERS_PER_FOOT 30.48 #define CENTIMETERS_PER_INCH 2.54 #define INCHES_PER_FOOT 12 -int main(void) { +/** + * main program entry + * + * continuously takes user input for a height + * in centimeters and then converts that to + * feet and inches with proper amount of decimal + * points. + * + * Quits when a number less than or equal to 0 + * is inputed. + */ +int main(void) +{ float heightCm; int heightFeet; float heightInches; - do { - + do + { + printf("Enter a height in centimeters (<=0 to quit): "); scanf("%f", &heightCm); - if (heightCm <= 0) { + if (heightCm <= 0) + { break; } heightFeet = (heightCm / CENTIMETERS_PER_FOOT); - heightInches = (heightCm / CENTIMETERS_PER_INCH) - + heightInches = (heightCm / CENTIMETERS_PER_INCH) - (INCHES_PER_FOOT * heightFeet); printf( @@ -35,4 +60,4 @@ int main(void) { printf("Bye\n"); return 0; -} \ No newline at end of file +} diff --git a/assignment01/project3.c b/assignment01/project3.c index 78e6bb5..9ff1cc0 100644 --- a/assignment01/project3.c +++ b/assignment01/project3.c @@ -1,23 +1,46 @@ #include -int main(void) { +/* +* Course: COMP 2510 +* Assignment: Assignment 1 +* Name: Braeden Sowinski +* Student ID: A01385066 +* Date: 2025-09-20 +* Description: Calculate sum of squares from X to Y +*/ + +/** + * main program entry + * + * takes in user input for an upper and lower + * limit, then calculates the sum of squares + * from the lower to the upper limit. + * + * continuously takes input until upper limit + * is less than or equal to lower limit + */ +int main(void) +{ int max; int min; int sumOfSquares; - do { + do + { sumOfSquares = 0; printf("Enter a lower and upper limit: "); scanf("%d %d", &min, &max); - if (max <= min) { + if (max <= min) + { break; } - for (int i = min; i <= max; i++) { + for (int i = min; i <= max; i++) + { sumOfSquares += i * i; } @@ -33,4 +56,4 @@ int main(void) { printf("Done\n"); return 0; -} \ No newline at end of file +} diff --git a/assignment01/project4.c b/assignment01/project4.c index 21768cc..83f9ee9 100644 --- a/assignment01/project4.c +++ b/assignment01/project4.c @@ -1,40 +1,66 @@ #include +/* +* Course: COMP 2510 +* Assignment: Assignment 1 +* Name: Braeden Sowinski +* Student ID: A01385066 +* Date: 2025-09-20 +* Description: Demonstrate how to copy arrays using different methods +*/ + #define ARRAY_SIZE 5 -void printArray(double arr[]) { - for (int i = 0; i < 5; i++) { +/** + * printArray to validate copy + */ +void printArray(char arrName[], double arr[]) { + printf("\n%s\n", arrName); + for (int i = 0; i < 5; i++) { printf("%.1f, ", arr[i]); } printf("\n"); } +/** + * copy_arr using array notatoin + */ void copy_arr(double target[], double source[], int size) { for (int i = 0; i < size; i++) { target[i] = source[i]; } - printArray(target); + printArray("target 1:", target); } +/** + * copy_ptr copies an array using pointer arithmatic + */ void copy_ptr(double *target, double *source, int size) { for (int i = 0; i < size; i++) { *(target + i) = *(source + i); } - printArray(target); + printArray("target 2:", target); } +/** + * copy_ptrs copies an array using pointers + */ void copy_ptrs(double *target, double *source, double *lastElement) { while (source <= lastElement) { target = source; source++; target++; } - - printArray(target); + + printArray("target 3:", target); } +/** + * main program entry to test different ways + * of copying arrays + */ int main(void) { double source[ARRAY_SIZE] = {1.1, 2.2, 3.3, 4.4, 5.5}; @@ -42,9 +68,11 @@ int main(void) { double target2[ARRAY_SIZE]; double target3[ARRAY_SIZE]; + printArray("source:", source); + copy_arr(target1, source, ARRAY_SIZE); copy_ptr(target2, source, ARRAY_SIZE); copy_ptrs(target3, source, source + ARRAY_SIZE); return 0; -} \ No newline at end of file +} diff --git a/lab05/Makefile b/lab05/Makefile index f3a7738..7e4f06c 100644 --- a/lab05/Makefile +++ b/lab05/Makefile @@ -5,7 +5,7 @@ CFLAGS = -Wall -std=c11 #Input file INPUT = input2.txt OUTPUT = output.txt -REF = ref2.txt +REF = ref.txt # Source file SRCS = lab5.c # Output diff --git a/lab06/Makefile b/lab06/Makefile new file mode 100644 index 0000000..8cc561d --- /dev/null +++ b/lab06/Makefile @@ -0,0 +1,43 @@ +# Compiler and flags +CC = gcc +CFLAGS = -Wall -std=c11 + +#Input file +INPUT = input3.txt +OUTPUT = output.txt +REF = ref3.txt +# Source file +SRCS = lab6.c +# Output +TARGET = lab6.out + +# Default target +all: $(TARGET) + +# Rule to build the executable +$(TARGET): $(SRCS) + $(CC) $(CFLAGS) $(SRCS) -o $(TARGET) + + +convert_input: $(INPUT) + dos2unix $(REF) + dos2unix $(INPUT) + +convert_output: $(OUTPUT) + dos2unix $(OUTPUT) + +check: $(OUTPUT) $(REF) + @diff -q $(OUTPUT) $(REF) > /dev/null; \ + if [ $$? -eq 0 ]; then \ + echo "Pass"; \ + else \ + echo "Fail"; \ + fi + +# Rule to run the program +run: $(TARGET) + ./$(TARGET) $(INPUT) $(OUTPUT) + +# Rule to clean generated files +clean: + rm -f $(TARGET) diff --git a/lab06/input.txt b/lab06/input.txt new file mode 100644 index 0000000..aea0149 --- /dev/null +++ b/lab06/input.txt @@ -0,0 +1,6 @@ +2 +2 +2 +22 +11 +E \ No newline at end of file diff --git a/lab06/input2.txt b/lab06/input2.txt new file mode 100644 index 0000000..f2899a4 --- /dev/null +++ b/lab06/input2.txt @@ -0,0 +1,8 @@ +4 +4 +0.5 +3333 +3333 +2222 +2222 +E \ No newline at end of file diff --git a/lab06/input3.txt b/lab06/input3.txt new file mode 100644 index 0000000..1f07933 --- /dev/null +++ b/lab06/input3.txt @@ -0,0 +1,6 @@ +2 +2 +1 +11 +22 +E \ No newline at end of file diff --git a/lab06/lab6.c b/lab06/lab6.c new file mode 100644 index 0000000..5486128 --- /dev/null +++ b/lab06/lab6.c @@ -0,0 +1,165 @@ +#include +#include +#include + +// Update this with your A number +char a_num[] = "01385066"; + +void writeOutput(char outputPath[], char **arr, int *rows, int *cols) +{ + FILE *writeFile; + + writeFile = fopen(outputPath, "w"); + + if (writeFile == NULL) + { + perror("Failed to open output file\n"); + return; + } + + // matrix to output file + for (int x = 0; x < *rows; x++) + { + for (int y = 0; y < *cols; y++) + { + fputc(arr[x][y], writeFile); + } + + if (x < *rows - 1) fputc('\n', writeFile); + } + + fclose(writeFile); +} + +void zoomArray(char **arr, float n, int *rows, int *cols, char outputFileName[]) +{ + + int newRows = (int)(*rows * n); + int newCols = (int)(*cols * n); + + char **scaled = malloc(sizeof(char*) * newRows); + + if (scaled == NULL) + { + printf("Failed to malloc scaled"); + return; + } + + for (int i = 0; i < newRows; i++) + { + scaled[i] = malloc(sizeof(char) * newCols); + if (scaled[i] == NULL) + { + printf("Failed to malloc scaled[i]"); + for (int j = 0; j < i; j++) + free(scaled[i]); + + free(scaled); + return; + } + } + + for (int i = 0; i < newRows; i++) { + for (int j = 0; j < newCols; j++) { + int srcRow = (int)(i / n); + int srcCol = (int)(j / n); + + if (srcRow >= *rows) srcRow = *rows - 1; + if (srcCol >= *cols) srcCol = *cols - 1; + + scaled[i][j] = arr[srcRow][srcCol]; + } + } + + writeOutput(outputFileName, scaled, &newRows, &newCols); +} + +int main(int argc, char *argv[]) +{ + if (argc != 3) + { + printf("Usage: %s \n", argv[0]); + return 1; + } + + char *inputFileName = argv[1]; + char *outputFileName = argv[2]; + char **arr = NULL; + int rows = 0, cols = 0; + float zoomFactor = 0; + + // Read the input array from the specified file + FILE *file = fopen(inputFileName, "r"); + if (file == NULL) + { + perror("Error opening input file"); + return 1; + } + + // Read the row value + fscanf(file, "%d", &rows); + // Read the column value + fscanf(file, "%d", &cols); + // Read the zoom factor + fscanf(file, "%f", &zoomFactor); + + // Allocate memory for the 2D array + arr = (char **)malloc(rows * sizeof(char *)); + if (arr == NULL) + { + printf("Memory allocation failed.\n"); + return 1; + } + for (int i = 0; i < rows; i++) + { + arr[i] = (char *)malloc(cols * sizeof(char)); + if (arr[i] == NULL) + { + printf("Memory allocation failed.\n"); + return 1; + } + } + + // Read the elements of the array until 'E' is encountered + char inputChar; + int i = 0, j = 0; + while ((inputChar = fgetc(file)) != 'E') + { + if (inputChar >= '0' && inputChar <= '9' && i < rows && j < cols) + { + arr[i][j++] = inputChar; + if (j == cols) + { + j = 0; + i++; + } + } + } + + fclose(file); + printf("A%s\n", a_num); + + // Output the input array + printf("Input Array:\n"); + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + printf("%c", arr[i][j]); + } + printf("\n"); + } + printf("\n"); + + // Call the zoomArray function + zoomArray(arr, zoomFactor, &rows, &cols, outputFileName); + + // Free the memory allocated for the 2D array + for (int i = 0; i < rows; i++) + { + free(arr[i]); + } + free(arr); + + return 0; +} diff --git a/lab06/output.txt b/lab06/output.txt new file mode 100644 index 0000000..2e8ce46 --- /dev/null +++ b/lab06/output.txt @@ -0,0 +1,2 @@ +11 +22 \ No newline at end of file diff --git a/lab06/ref.txt b/lab06/ref.txt new file mode 100644 index 0000000..cb57e81 --- /dev/null +++ b/lab06/ref.txt @@ -0,0 +1,4 @@ +2222 +2222 +1111 +1111 \ No newline at end of file diff --git a/lab06/ref2.txt b/lab06/ref2.txt new file mode 100644 index 0000000..9012a6c --- /dev/null +++ b/lab06/ref2.txt @@ -0,0 +1,2 @@ +33 +22 \ No newline at end of file diff --git a/lab06/ref3.txt b/lab06/ref3.txt new file mode 100644 index 0000000..2e8ce46 --- /dev/null +++ b/lab06/ref3.txt @@ -0,0 +1,2 @@ +11 +22 \ No newline at end of file