From b7e84133603d7e449af5e72a54cbb06e85785e49 Mon Sep 17 00:00:00 2001 From: SowinskiBraeden Date: Fri, 3 Oct 2025 08:59:43 -0700 Subject: [PATCH] final lab05 passes --- lab05/Makefile | 2 +- lab05/lab5.c | 36 ++++++++++++++++++++++++++++++++---- lab05/output.txt | 10 +++++----- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/lab05/Makefile b/lab05/Makefile index 7e4f06c..f3a7738 100644 --- a/lab05/Makefile +++ b/lab05/Makefile @@ -5,7 +5,7 @@ CFLAGS = -Wall -std=c11 #Input file INPUT = input2.txt OUTPUT = output.txt -REF = ref.txt +REF = ref2.txt # Source file SRCS = lab5.c # Output diff --git a/lab05/lab5.c b/lab05/lab5.c index a9d6266..73dee83 100644 --- a/lab05/lab5.c +++ b/lab05/lab5.c @@ -94,7 +94,7 @@ void drawShape( } } - // Insert coords into matrix + // Plot coordinates in matrix for (int i = 0; i < numCoords; i++) { int x = *(*(coords + i)); @@ -132,14 +132,28 @@ void drawShape( } } + // Clear spaces + // matrix to output file for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) + // only print up until last '*', dont + // print trailing spaces + int lastPoint; + for (int i = width - 1; i >= 0; i--) + { + if (*(*(matrix + i) + y) == '*') + { + lastPoint = i; + break; + } + } + + for (int x = 0; x <= lastPoint; x++) { fputc(*(*(matrix + x) + y), writeFile); } - fputc('\n', writeFile); + if (y < height - 1) fputc('\n', writeFile); } fclose(writeFile); @@ -187,7 +201,7 @@ int main(int argc, char **argv) return 1; } - for (int i = 0; i< MAX_COORDS; i++) + for (int i = 0; i < MAX_COORDS; i++) { *(coords + i) = malloc(COORD_2D * sizeof(int)); @@ -220,6 +234,20 @@ int main(int argc, char **argv) } else { + if (fscanf(inputFile, "%d", &x) == 0 || + fscanf(inputFile, "%d", &x) == 0) + { + // Free and close file before exiting + for (int i = 0; i < MAX_COORDS; i++) { + free(coords[i]); + } + free(coords); + fclose(inputFile); + + perror("Error: bad input file\n"); + return 1; + } + char c; if (fscanf(inputFile, " %c", &c) == 1 && c == 'E') { break; diff --git a/lab05/output.txt b/lab05/output.txt index baa78ad..4e33569 100644 --- a/lab05/output.txt +++ b/lab05/output.txt @@ -1,5 +1,5 @@ -* -** -*** -**** -***** +* +** +*** +**** +***** \ No newline at end of file