summaryrefslogtreecommitdiff
path: root/27_matrix_input/rotateMatrix.c~
blob: 3aa97bc73ae8a72d0900f250cfd3b60bd7f80030 (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
#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;
}