我遇到矩阵问题。
当我尝试将最后一个矩阵(可逆矩阵)打印到文件(matrixrez.txt)时,程序仅打印第一列。
为什么?
Matrix.txt:
1 0 1 1 2 0
1 0 2 0 3 0
2 1 1 1 2 3
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file, *outf;
int matrixA[3][3], matrixB[3][3], a[3][3];
int trash[3];
int i, j, k, sum;
float determinant = 0;
i = j = k = sum = 0;
file = fopen("matrix.txt", "rt");
outf = fopen("matrixrez.txt", "w+");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
fscanf(file, "%d", &matrixA[i][j]);
}
for (k = 0; k < 3; k++) fscanf(file, "%d", &trash[k]);
}
fseek(file, 0, SEEK_SET);
for (i = 0; i < 3; i++) {
for (k = 0; k < 3; k++) fscanf(file, "%d", &trash[k]);
for (j = 0; j < 3; j++) {
fscanf(file, "%d", &matrixB[i][j]);
}
}
/* Matrix multiplication */
for ( i = 0 ; i < 3 ; i++ )
{
for ( j = 0 ; j < 3 ; j++ )
{
for ( k = 0 ; k < 3 ; k++ )
{
sum = sum + matrixA[i][k]*matrixB[k][j];
}
a[i][j] = sum;
sum = 0;
}
}
for(i=0;i<3;i++)
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++) {
printf("%d ", matrixA[i][j]);
}
}
printf("\n");
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++) {
printf("%d ", matrixB[i][j]);
}
}
printf("\n");
printf("The resultant matrix is:: \n");
fprintf(outf,"The resultant matrix is:: \n");
for (i = 0; i < 3; i++) {
printf("\n");
fprintf(outf,"\n");
for (j = 0; j < 3; j++) {
printf("%d ", a[i][j]);
fprintf(outf,"%d ", a[i][j]);
}
}
printf("\n\n Inversion of Matrix: \n");
fprintf(outf,"\n\n Inversion of Matrix: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3] [(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"\n");
printf("\n");
}
printf("\n");
system("pause");
return 0;
}
我得到了matrixrez.txt:
The resultant matrix is::
2 4 3
3 6 6
3 9 3
Inversion of Matrix:
4.00
-1.67
-0.67
我认为错误的是文件末尾的行(如果我没记错的话):
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
答案 0 :(得分:1)
你需要在你的意图周围添加大括号作为你的for循环块:
printf("\n\n Inversion of Matrix: \n");
fprintf(outf,"\n\n Inversion of Matrix: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
{ /* YOU NEED THIS BRACE */
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3] [(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
} /* AND YOU NEED THIS BRACE */
fprintf(outf,"\n");
printf("\n");
}
答案 1 :(得分:0)
内部for
循环周围没有括号。所以,它遍历i
就好了,但每次循环j
而不调用fprintf
只是printf
代码应为
printf("\n\n Inversion of Matrix: \n");
fprintf(outf,"\n\n Inversion of Matrix: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3] [(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"\n");
printf("\n");
}
}