-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranspose_fromFile.c
More file actions
49 lines (40 loc) · 1.23 KB
/
Copy pathtranspose_fromFile.c
File metadata and controls
49 lines (40 loc) · 1.23 KB
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
#include <stdio.h>
#include <unistd.h>
#define MAX 100
// время: O(NM)
// память: O(NM)
int main(){
// ====== Ввод ======
/**
* Ввод:
* 1 строка: N M - кол-во стр и стб
* Далее - матрица NxM
*/
FILE *matrix_source = fopen("matrix.txt", "r+");
int matrix[MAX][MAX];
int N, M;
fscanf(matrix_source, "%d %d", &N, &M);
for (size_t i = 0; i < N; ++i){
for (size_t j = 0; j < M; ++j){
fscanf(matrix_source, "%d", &matrix[i][j]);
fprintf(stdout, "%d ", matrix[i][j]);
}
fprintf(stdout, "\n");
}
// ===================
rewind(matrix_source);
// функция очистки файла; мб сделаем рукописную
ftruncate(fileno(matrix_source), 0);
// ====== Транспонирование и запись ======
fprintf(matrix_source, "%d %d\n", N, M);
for (size_t i = 0; i < N; ++i){
for (size_t j = 0; j < M; ++j){
fprintf(matrix_source, "%d ", matrix[j][i]);
fprintf(stdout, "%d ", matrix[j][i]);
}
fprintf(matrix_source, "\n");
fprintf(stdout, "\n");
}
fclose(matrix_source);
return 0;
}