diff --git a/lab03/Makefile b/lab03/Makefile new file mode 100644 index 0000000..d6ae72d --- /dev/null +++ b/lab03/Makefile @@ -0,0 +1,29 @@ +# Compiler and flags +CC = gcc +CFLAGS = -Wall -std=c11 + +#Input file +INPUT = input.txt +# Source file +SRCS = lab3.c +# Output +TARGET = lab3.out + +# Default target +all: $(TARGET) + +# Rule to build the executable +$(TARGET): $(SRCS) + $(CC) $(CFLAGS) $(SRCS) -o $(TARGET) + + +convert: $(INPUT) + dos2unix $(INPUT) + +# Rule to run the program +run: $(TARGET) + ./$(TARGET) $(INPUT) + +# Rule to clean generated files +clean: + rm -f $(TARGET) diff --git a/lab03/input.txt b/lab03/input.txt new file mode 100644 index 0000000..9f7bbf9 --- /dev/null +++ b/lab03/input.txt @@ -0,0 +1,6 @@ +90 +3 +123 +123 +123 +E \ No newline at end of file diff --git a/lab03/lab3.c b/lab03/lab3.c new file mode 100644 index 0000000..0b38534 --- /dev/null +++ b/lab03/lab3.c @@ -0,0 +1,112 @@ +#include +#include +#include + +// update this with your A number +char a_num[] = "01385066"; + +void rotateArray(int arr[10][10], int n, int rows, int cols) { + + // Invalid rotation angle + if (n % 90 != 0) { + return; + } + + int numRotations = n / 90; + int tmp[rows][cols]; + + // rotate by 90 or -90 for numRotations + for (int t = 0; t < abs(numRotations); t++) { + + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + + if (numRotations > 0) { + // rotate by 90 + tmp[i][cols - 1 - j] = arr[j][i]; + } else { + // rotate by -90 + tmp[rows - 1 - i][j] = arr[j][i]; + } + + } + } + + // update arr and rotate again if needed + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + arr[i][j] = tmp[i][j]; + } + } + + } + + // Do NOT write any code below + // Output the rotated array + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + if(arr[i][j] == 0) + printf(" "); + else + printf("%d", arr[i][j]); + } + printf("\n"); + } +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + printf("Usage: %s \n", argv[0]); + return 1; + } + + char *inputFileName = argv[1]; + int rotationAngle = 0; + int dimensions = 0; + int arr[10][10] = {0}; // Initialize the array with zeros + int rows = 0, cols = 0; + char inputChar; + + // 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 first line for angle of rotation and second line for array dimensions + fscanf(file, "%d", &rotationAngle); + fscanf(file, "%d", &dimensions); + // Read the input array character by character + int counter = 0; //To skip first two lines in the file + while (rows < 10 && (inputChar = fgetc(file)) != 'E') { + if (inputChar == '\n'){counter++;} + if (inputChar == '\n' && counter>=2) { + rows++; + cols = 0; + } else if (cols < 10 && inputChar >= '0' && inputChar <= '9') { + arr[rows][cols++] = inputChar - '0'; + } + } + + fclose(file); + printf("A%s\n", a_num); + // Actual dimensions of the input array + rows = cols = dimensions; + + printf("Input array:\n"); + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + if(arr[i][j] == 0) + printf(" "); + else + printf("%d", arr[i][j]); + } + printf("\n"); + } + + printf("\nRotated array (%d degrees):\n", rotationAngle); + rotateArray(arr, rotationAngle, rows, cols); + + return 0; +}