summaryrefslogtreecommitdiff
path: root/27_matrix_input/rotateMatrix.c~
diff options
context:
space:
mode:
Diffstat (limited to '27_matrix_input/rotateMatrix.c~')
-rw-r--r--27_matrix_input/rotateMatrix.c~72
1 files changed, 72 insertions, 0 deletions
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;
+}