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 --- 20_rot_matrix/.gitignore | 1 + 20_rot_matrix/README | 37 +++++++++++++++++++++++++++++++++++++ 20_rot_matrix/grade.txt | 13 +++++++++++++ 20_rot_matrix/read-matrix.o | Bin 0 -> 3592 bytes 20_rot_matrix/rotate-matrix | Bin 0 -> 17160 bytes 20_rot_matrix/rotate.c | 24 ++++++++++++++++++++++++ 20_rot_matrix/sample.out | 10 ++++++++++ 20_rot_matrix/sample.txt | 10 ++++++++++ 8 files changed, 95 insertions(+) create mode 100644 20_rot_matrix/.gitignore create mode 100644 20_rot_matrix/README create mode 100644 20_rot_matrix/grade.txt create mode 100644 20_rot_matrix/read-matrix.o create mode 100755 20_rot_matrix/rotate-matrix create mode 100644 20_rot_matrix/rotate.c create mode 100644 20_rot_matrix/sample.out create mode 100644 20_rot_matrix/sample.txt (limited to '20_rot_matrix') diff --git a/20_rot_matrix/.gitignore b/20_rot_matrix/.gitignore new file mode 100644 index 0000000..7889cbc --- /dev/null +++ b/20_rot_matrix/.gitignore @@ -0,0 +1 @@ +rotate diff --git a/20_rot_matrix/README b/20_rot_matrix/README new file mode 100644 index 0000000..8e0f90b --- /dev/null +++ b/20_rot_matrix/README @@ -0,0 +1,37 @@ +For this problem, you will be writing a function which +performs a 90 degree clockwise rotation of a 10x10 matrix. +There is nothing special about a 10x10 matrix---we are just +fixing the size so that you can read the input in a future +assignment after you have learned about reading files, +but before you have learned about dynamic memory allocation. + +In particular, you should write + + void rotate(char matrix[10][10]) + +in a file called rotate.c + +This function takes a 10 by 10 matrix of characters and rotates +it 90 degrees clockwise, updating the matrix that was passed in +(remember that arrays are pointers, so you will modify +the array in the frame where it was created). + +As you have not yet learned to read from files, we have +provided a compiled object file, read-matrix.o. This +object file has a main function which will read +the input file (specified as a command line arugments +to your program), call your rotate function, and +then print the result. + +If you compiled your code (and linked with read-matrix.o) +into a program called rotate-matrix, you might run it as + +./rotate-matrix sample.txt + +It will then print the resulting matrix, which in this case +should look like the contents of the file sample.out. +(Remember that you can use > to redirect the output +of a program to a file, and use diff to compare +the contents of two files). + +Note that you do not have to complete the rotation 'in place'. diff --git a/20_rot_matrix/grade.txt b/20_rot_matrix/grade.txt new file mode 100644 index 0000000..dbcb1bd --- /dev/null +++ b/20_rot_matrix/grade.txt @@ -0,0 +1,13 @@ +Grading at Mon 25 Oct 2021 02:26:46 AM UTC +Attempting to compile rotate.c +Running testcase 1 +Your file matched the expected output +testcase1 passed +Running testcase 1 +Your file matched the expected output +testcase1 passed +Running testcase 1 +Your file matched the expected output +testcase1 passed + +Overall Grade: A diff --git a/20_rot_matrix/read-matrix.o b/20_rot_matrix/read-matrix.o new file mode 100644 index 0000000..03b1ca1 Binary files /dev/null and b/20_rot_matrix/read-matrix.o differ diff --git a/20_rot_matrix/rotate-matrix b/20_rot_matrix/rotate-matrix new file mode 100755 index 0000000..1826b1c Binary files /dev/null and b/20_rot_matrix/rotate-matrix differ diff --git a/20_rot_matrix/rotate.c b/20_rot_matrix/rotate.c new file mode 100644 index 0000000..79d7f3b --- /dev/null +++ b/20_rot_matrix/rotate.c @@ -0,0 +1,24 @@ +#include +#include + +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++; + } + +} diff --git a/20_rot_matrix/sample.out b/20_rot_matrix/sample.out new file mode 100644 index 0000000..6280f8c --- /dev/null +++ b/20_rot_matrix/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/20_rot_matrix/sample.txt b/20_rot_matrix/sample.txt new file mode 100644 index 0000000..767b4c3 --- /dev/null +++ b/20_rot_matrix/sample.txt @@ -0,0 +1,10 @@ +0123456789 +abcdefghij +*......... +.*........ +..*....... +...*...... +....*..... +.....*.... +HelloWorld +Coding Fun -- cgit v1.2.3