summaryrefslogtreecommitdiff
path: root/27_matrix_input/rotateMatrix.c
blob: 8697a0dbc0d27d7ebb0acec1bf0da0aebb694220 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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;
}