summaryrefslogtreecommitdiff
path: root/27_matrix_input
diff options
context:
space:
mode:
Diffstat (limited to '27_matrix_input')
-rw-r--r--27_matrix_input/Makefile4
-rw-r--r--27_matrix_input/README47
-rw-r--r--27_matrix_input/blank.txt2
-rw-r--r--27_matrix_input/f0
-rw-r--r--27_matrix_input/grade.txt53
-rwxr-xr-x27_matrix_input/rotateMatrixbin0 -> 51200 bytes
-rw-r--r--27_matrix_input/rotateMatrix.c79
-rw-r--r--27_matrix_input/rotateMatrix.c~72
-rw-r--r--27_matrix_input/sample.out10
-rw-r--r--27_matrix_input/sample.txt10
10 files changed, 277 insertions, 0 deletions
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
--- /dev/null
+++ b/27_matrix_input/f
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
--- /dev/null
+++ b/27_matrix_input/rotateMatrix
Binary files 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#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