-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest5.c
More file actions
60 lines (49 loc) · 1.29 KB
/
Copy pathtest5.c
File metadata and controls
60 lines (49 loc) · 1.29 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
53
54
55
56
57
58
59
60
#include <stdio.h>
#define MAX_ROWS 3
#define MAX_COLS 3
int main() {
int matrix[MAX_ROWS][MAX_COLS];
int transposed[MAX_COLS][MAX_ROWS];
int rows, cols, i, j;
// Open the file for reading
FILE* fp = fopen("matrix.txt", "r");
if (fp == NULL) {
printf("Error opening file!\n");
return 1;
}
// Read the matrix dimensions from the file
fscanf(fp, "%d %d", &rows, &cols);
// Read the matrix data from the file
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
fscanf(fp, "%d", &matrix[i][j]);
printf("%d",matrix[i][j]);
fflush(stdout);
}
}
// Close the file
fclose(fp);
// Transpose the matrix
for (i = 0; i < cols; i++) {
for (j = 0; j < rows; j++) {
transposed[i][j] = matrix[j][i];
}
}
// Open the file for writing
fp = fopen("transposed_matrix.txt", "w");
if (fp == NULL) {
printf("Error opening file!\n");
return 1;
}
// Write the transposed matrix data to the file
fprintf(fp, "%d %d\n", cols, rows);
for (i = 0; i < cols; i++) {
for (j = 0; j < rows; j++) {
fprintf(fp, "%d ", transposed[i][j]);
}
fprintf(fp, "\n");
}
// Close the file
fclose(fp);
return 0;
}