From 442a49ad5a48d417345959b903ae6a6d32d55759 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Fri, 15 Apr 2022 15:51:30 -0500 Subject: Great C programming fun Excellent fundamentals and displine training, many tools and techniques exercises: gdb, emacs, valgrind, git --- 27_matrix_input/Makefile | 4 ++ 27_matrix_input/README | 47 ++++++++++++++++++++++++ 27_matrix_input/blank.txt | 2 + 27_matrix_input/f | 0 27_matrix_input/grade.txt | 53 +++++++++++++++++++++++++++ 27_matrix_input/rotateMatrix | Bin 0 -> 51200 bytes 27_matrix_input/rotateMatrix.c | 79 ++++++++++++++++++++++++++++++++++++++++ 27_matrix_input/rotateMatrix.c~ | 72 ++++++++++++++++++++++++++++++++++++ 27_matrix_input/sample.out | 10 +++++ 27_matrix_input/sample.txt | 10 +++++ 10 files changed, 277 insertions(+) create mode 100644 27_matrix_input/Makefile create mode 100644 27_matrix_input/README create mode 100644 27_matrix_input/blank.txt create mode 100644 27_matrix_input/f create mode 100644 27_matrix_input/grade.txt create mode 100755 27_matrix_input/rotateMatrix create mode 100644 27_matrix_input/rotateMatrix.c create mode 100644 27_matrix_input/rotateMatrix.c~ create mode 100644 27_matrix_input/sample.out create mode 100644 27_matrix_input/sample.txt (limited to '27_matrix_input') diff --git a/27_matrix_input/Makefile b/27_matrix_input/Makefile new file mode 100644 index 0000000..f6b6c00 --- /dev/null +++ b/27_matrix_input/Makefile @@ -0,0 +1,4 @@ +CFLAGS=-ggdb3 -Wall -Werror -pedantic -std=gnu99 + +rotateMatrix: rotateMatrix.c + gcc ${CFLAGS} -o rotateMatrix rotateMatrix.c diff --git a/27_matrix_input/README b/27_matrix_input/README new file mode 100644 index 0000000..26031a6 --- /dev/null +++ b/27_matrix_input/README @@ -0,0 +1,47 @@ +In a previous project, you wrote a function + + void rotate(char matrix[10][10]) + +which performed 90 degree clockwise rotation of a 10x10 +matrix. In that assignment, we gave you a compiled +object file which read the input matrix from a file, +called your function to do the rotation, and then +printed the result to the screen. + +In this assignment, you will write the code that we +previously gave you, making the complete program on your own. +You are encouraged to make use of your previously written +rotate function in this assignment. + +For this problem, you will be writing a program which +performs a 90 degree clockwise rotation of a 10x10 matrix. +There is nothing special about a 10x10 matrix---I just need +to fix the matrix size, since we have not learned about dynamic +memory allocation yet, so we do not have the knowledge needed +to read in any size of matrix. + +To keep the input processing simple, the matrix will be a matrix +of characters (so you will have something like + char matrix[10][10] +in your program), which will be read from a file. Each line +in the input file should contain 10 characters (plus a newline). + +Requirements: +============= + - Create a file called rotateMatrix.c + - Your program will take one command line argument, a string + specifying the input file to read. + - The input file should contain 10 lines, each of which + have 10 (non-newline) characters (plus a newline). + - Your program should then rotate this 90 degrees clockwise, + and print the result on stdout. + Note that sample.txt provides sample input, and + sample.out provides sample output. + - If there are any errors, your program should print an + appropriate message to stderr, and exit with EXIT_FAILURE. + +Hints: +------ + - You may find the strchr useful for error checking that + you read a proper line (10 non-newline characters, then a newline). + diff --git a/27_matrix_input/blank.txt b/27_matrix_input/blank.txt new file mode 100644 index 0000000..139597f --- /dev/null +++ b/27_matrix_input/blank.txt @@ -0,0 +1,2 @@ + + diff --git a/27_matrix_input/f b/27_matrix_input/f new file mode 100644 index 0000000..e69de29 diff --git a/27_matrix_input/grade.txt b/27_matrix_input/grade.txt new file mode 100644 index 0000000..14df468 --- /dev/null +++ b/27_matrix_input/grade.txt @@ -0,0 +1,53 @@ +Grading at Mon 13 Dec 2021 02:22:53 AM UTC +Attempting to compile rotateMatrix.c +testcase1 passed +################################################# +testcase2: NonExistentFile + (should indicate an error) +testcase2 passed +################################################# +testcase3: blank.txt + (should indicate an error) +testcase3 passed +################################################# +testcase4: short-line.txt + (should indicate an error) +testcase4 passed +################################################# +testcase5: short-file.txt + (should indicate an error) +testcase5 passed +################################################# +testcase6: long-line.txt + (should indicate an error) +testcase6 passed +################################################# +testcase7: long-file.txt + (should indicate an error) +testcase7 passed +################################################# +testcase8: long-line-2.txt + (should indicate an error) +testcase8 passed +################################################# +testcase9: normal1.txt normal2.txt + (should indicate an error) +testcase9 passed +################################################# +testcase10: normal1.txt +Your file matched the expected output +testcase10 passed +################################################# +testcase10: normal2.txt +Your file matched the expected output +testcase10 passed +################################################# +testcase10: normal3.txt +Your file matched the expected output +testcase10 passed +################################################# +testcase10: eof.txt +Your file matched the expected output +testcase10 passed + +Overall Grade: A diff --git a/27_matrix_input/rotateMatrix b/27_matrix_input/rotateMatrix new file mode 100755 index 0000000..f810ebf Binary files /dev/null and b/27_matrix_input/rotateMatrix differ diff --git a/27_matrix_input/rotateMatrix.c b/27_matrix_input/rotateMatrix.c new file mode 100644 index 0000000..8697a0d --- /dev/null +++ b/27_matrix_input/rotateMatrix.c @@ -0,0 +1,79 @@ +#include +#include +#include + +#define LINE_SIZE 12 + +void rotate(char matrix[10][10]) { + int layer = 0; + char temp1; + char temp2; + char temp3; + + while (layer <= 10 / 2) { + for (int i = layer; i < 10 - 1 - layer; i++) { + temp1 = matrix[i][10 - 1 - layer]; + temp2 = matrix[10 - 1 - layer][10 - 1 - i]; + temp3 = matrix[10 - 1 - i][layer]; + + matrix[i][10 - 1 - layer] = matrix[layer][i]; + matrix[10 - 1 - layer][10 - 1 - i] = temp1; + matrix[10 - 1 - i][layer] = temp2; + matrix[layer][i] = temp3; + } + layer++; + } +} + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, "Usage: rotateMatrix inputFileName\n"); + return EXIT_FAILURE; + } + + FILE *f = fopen(argv[1], "r"); + if (f == NULL) { + fprintf(stderr, "Could not open file\n"); + return EXIT_FAILURE; + } + char line[LINE_SIZE]; + char matrix[10][10]; + int lineNumber = 0; + while (fgets(line, LINE_SIZE, f) != NULL) { + if (lineNumber >= 10) { + fprintf(stderr, "Too many lines!\n"); + return EXIT_FAILURE; + } + if (strchr(line, '\n') == NULL) { + fprintf(stderr, "Line is too long!\n"); + return EXIT_FAILURE; + } + if ((strchr(line, '\n') - line) != 10) { + fprintf(stderr, "Line is too short!\n"); + return EXIT_FAILURE; + } + for (int j = 0; j < 10; j++) { + if (line[j] == '\n') { + fprintf(stderr, "Line is too short!\n"); + return EXIT_FAILURE; + } + matrix[lineNumber][j] = line[j]; + } + lineNumber++; + } + + if (lineNumber != 10) { + fprintf(stderr, "Not enough number of lines\n"); + return EXIT_FAILURE; + } + + rotate(matrix); + for (int i = 0; i < 10; i++) { + for (int j = 0; j < 10; j++) { + printf("%c", matrix[i][j]); + } + printf("\n"); + } + + return EXIT_SUCCESS; +} diff --git a/27_matrix_input/rotateMatrix.c~ b/27_matrix_input/rotateMatrix.c~ new file mode 100644 index 0000000..3aa97bc --- /dev/null +++ b/27_matrix_input/rotateMatrix.c~ @@ -0,0 +1,72 @@ +#include +#include +#include + +#define LINE_SIZE 12 + +void rotate(char matrix[10][10]) { + int layer = 0; + char temp1; + char temp2; + char temp3; + + while (layer <= 10/2) { + for (int i = layer; i < 10 - 1 - layer; i++) { + temp1 = matrix[i][10-1-layer]; + temp2 = matrix[10-1-layer][10-1-i]; + temp3 = matrix[10-1-i][layer]; + + matrix[i][10-1-layer] = matrix[layer][i]; + matrix[10-1-layer][10-1-i] = temp1; + matrix[10-1-i][layer] = temp2; + matrix[layer][i] = temp3; + } + layer++; + } + +} + +int main(int argc, char ** argv) { + if (argc != 2) { + fprintf(stderr,"Usage: rotateMatrix inputFileName\n"); + return EXIT_FAILURE; + } + + FILE * f = fopen(argv[1], "r"); + if (f == NULL) { + perror("Could not open file"); + return EXIT_FAILURE; + } + char line[LINE_SIZE]; + char matrix[10][10]; + int lineNumber = 0; + while (fgets(line, LINE_SIZE, f) != NULL && lineNumber < 10) { + if (strchr(line, '\n') == NULL) { + printf("Line is too long!\n"); + return EXIT_FAILURE; + } + for (int j = 0; j<10; j++) { + if (line[j] == '\n') { + printf("Line is too short!\n"); + return EXIT_FAILURE; + } + matrix[lineNumber][j] = line[j]; + //printf("%c", matrix[i][j]); + } + lineNumber++; + } + if (lineNumber < 9) { + printf("Not enough number of lines\n"); + return EXIT_FAILURE; + } + + rotate(matrix); + for (int i=0; i<10; i++) { + for (int j=0; j<10; j++) { + printf("%c", matrix[i][j]); + } + printf("\n"); + } + + return EXIT_SUCCESS; +} diff --git a/27_matrix_input/sample.out b/27_matrix_input/sample.out new file mode 100644 index 0000000..6280f8c --- /dev/null +++ b/27_matrix_input/sample.out @@ -0,0 +1,10 @@ +CH.....*a0 +oe....*.b1 +dl...*..c2 +il..*...d3 +no.*....e4 +gW*.....f5 + o......g6 +Fr......h7 +ul......i8 +nd......j9 diff --git a/27_matrix_input/sample.txt b/27_matrix_input/sample.txt new file mode 100644 index 0000000..767b4c3 --- /dev/null +++ b/27_matrix_input/sample.txt @@ -0,0 +1,10 @@ +0123456789 +abcdefghij +*......... +.*........ +..*....... +...*...... +....*..... +.....*.... +HelloWorld +Coding Fun -- cgit v1.2.3