-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest7.c
More file actions
52 lines (44 loc) · 1.11 KB
/
Copy pathtest7.c
File metadata and controls
52 lines (44 loc) · 1.11 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
50
51
52
#include <stdio.h>
#define MAX_ROWS 3
#define MAX_COLS 3
int main() {
FILE *fp;
char line[100];
int matrix[MAX_ROWS][MAX_COLS];
int rows = 0, cols = 0;
// Open file for reading
fp = fopen("matrix.txt", 'r');
// Read matrix from file
while (fgets(line, 100, fp) != NULL) {
cols = 0;
int num;
char *token = strtok(line, " ");
while (token != NULL) {
sscanf(token, "%d", &num);
matrix[rows][cols] = num;
cols++;
token = strtok(NULL, " ");
}
rows++;
}
// Transpose matrix
int transposed[MAX_COLS][MAX_ROWS];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
transposed[i][j] = matrix[j][i];
}
}
// Write transposed matrix to file
FILE *fp_out;
fp_out = fopen("transposed.txt", "w");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
fprintf(fp_out, "%d ", transposed[i][j]);
}
fprintf(fp_out, "\n");
}
fclose(fp_out);
// Close file
fclose(fp);
return 0;
}