0

我的矩阵有问题。

当我尝试将最后一个矩阵(可逆矩阵)打印到文件(matrixrez.txt)时,程序只打印第一列。

为什么?

矩阵.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);
4

2 回答 2

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");

 }
于 2013-02-19T23:07:31.777 回答
0

for内循环周围没有括号。所以,它循环通过i就好了,但每次它循环通过j而不调用fprintfjustprintf

代码应该是

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");
 }
}
于 2013-02-19T23:07:06.457 回答