C 将两个文本文件中的两个矩阵相乘,输出为零
我试图从两个文本文件中读取矩阵,将其存储到两个数组中,然后尝试将两个矩阵相乘,并将结果存储到一个数组中。 乘法结果是000 下面是我的代码C 将两个文本文件中的两个矩阵相乘,输出为零,c,arrays,matrix,C,Arrays,Matrix,我试图从两个文本文件中读取矩阵,将其存储到两个数组中,然后尝试将两个矩阵相乘,并将结果存储到一个数组中。 乘法结果是000 下面是我的代码 #include<stdio.h> #include<stdlib.h> int main(int argc,char *argv[]) { FILE *fr1, *fr2, *fw; char *line = malloc(1000); int count = 0; //To read a file
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
FILE *fr1, *fr2, *fw;
char *line = malloc(1000);
int count = 0;
//To read a file use the fopen() function to open it
fr1 = fopen(argv[1], "r");
//If the file fails to open the fopen() returns a NULL
if (fr1 == NULL) {
printf("Cannot open %s. Program terminated...",argv[1]);
exit(1);
}
// Similar to the above method read the second file
fr2 = fopen(argv[2], "r");
if (fr2 == NULL) {
printf("Cannot open %s. Program terminated...",argv[2]);
exit(1);
}
double *data = (double*) malloc(1000*sizeof(double));
if(data == NULL)
{
printf("Error in allocating memory");
return EXIT_FAILURE;
}
// Read number of columns and number of rows of first matrix
getline(&line, &count, fr1);
int read = -1, cur = 0, columCount1 = 0;
while(sscanf(line+cur, "%lf%n", &data[columCount1], &read) == 1)
{cur+=read; columCount1++;}
int rowCount1 = 1;
while(getline(&line, &count, fr1) != -1) {rowCount1++;}
printf("%d\n",columCount1);
printf("%d\n",rowCount1);
// Read number of columns and number of rows of second matrix
getline(&line, &count, fr2);
read = -1,cur = 0;
int columCount2 = 0;
while(sscanf(line+cur, "%lf%n", &data[columCount2], &read) == 1)
{cur+=read; columCount2++;}
int rowCount2 = 1;
while(getline(&line, &count, fr2) != -1) {rowCount2++;}
printf("%d\n",columCount2);
printf("%d\n",rowCount2);
int i=0;
int j=0;
int **mat1 = (int **)malloc(rowCount1 * sizeof(int*));
for(i = 0; i < rowCount1; i++)
mat1[i] = (int *)malloc(columCount1 * sizeof(int));
fseek( fr1, 0, SEEK_SET );
for(i=0; i<rowCount1; i++)
{
for(j=0; j<columCount1; j++)
fscanf(fr1,"%d",&mat1[i][j]);
}
i = 0;
j = 0;
printf("\n\n");
//print matrix 1
for(i=0; i<rowCount1; i++)
{
for(j=0; j<columCount1; j++)
printf("%d",mat1[i][j]);
printf("\n");
}
i = 0;
j = 0;
int **mat2 = (int **)malloc(rowCount2 * sizeof(int*));
for(i = 0; i < rowCount2; i++)
mat2[i] = (int *)malloc(columCount2 * sizeof(int));
fseek( fr2, 0, SEEK_SET );
for(i=0; i<rowCount2; i++)
{
for(j=0; j<columCount2; j++)
fscanf(fr2,"%d",&mat2[i][j]);
}
i = 0;
j = 0;
printf("\n\n");
//print matrix 2
for(i=0; i<rowCount2; i++)
{
for(j=0; j<columCount2; j++)
printf("%d",mat2[i][j]);
printf("\n");
}
i = 0;
int **mat3 = (int **)malloc(rowCount1 * sizeof(int*));
for(i = 0; i < rowCount1; i++)
mat3[i] = (int *)malloc(columCount2 * sizeof(int));
i = 0;
j = 0;
int k = 0;
int sum = 0;
//multiplication of two matrices
for(i=0; i<rowCount1; i++)
{
for(j=0; j<columCount2; j++)
{
sum=0;
for(k=0; k<rowCount2; k++)
sum+=mat1[i][k]*mat2[k][j];
}
mat3[i][j] = sum;
}
i = 0;
j = 0;
//print multiplication result
printf("\n\nResult = \n\n");
for(i=0; i<rowCount1; i++)
{
for(j=0; j<columCount2; j++)
printf("%d",mat3[i][j]);
printf("\n");
}
return 0;
}
#包括
#包括
int main(int argc,char*argv[])
{
文件*fr1、*fr2、*fw;
char*line=malloc(1000);
整数计数=0;
//要读取文件,请使用fopen()函数将其打开
fr1=fopen(argv[1],“r”);
//如果文件无法打开,fopen()将返回空值
如果(fr1==NULL){
printf(“无法打开%s。程序已终止…”,argv[1]);
出口(1);
}
//与上述方法类似,读取第二个文件
fr2=fopen(argv[2],“r”);
如果(fr2==NULL){
printf(“无法打开%s。程序已终止…”,argv[2]);
出口(1);
}
double*数据=(double*)malloc(1000*sizeof(double));
如果(数据==NULL)
{
printf(“内存分配错误”);
返回退出失败;
}
//读取第一个矩阵的列数和行数
获取行(&行,&计数,fr1);
int read=-1,cur=0,columCount1=0;
而(sscanf(行+当前,%lf%n,&data[columCount1],&read)==1)
{cur+=read;columCount1++;}
int rowCount1=1;
while(getline(&line,&count,fr1)!=-1){rowCount1++;}
printf(“%d\n”,columCount1);
printf(“%d\n”,行计数1);
//读取第二个矩阵的列数和行数
getline(&line,&count,fr2);
read=-1,cur=0;
int columCount2=0;
而(sscanf(行+电流,%lf%n),&data[columCount2],&read)==1)
{cur+=read;columCount2++;}
int rowCount2=1;
while(getline(&line,&count,fr2)!=-1){rowCount2++;}
printf(“%d\n”,columCount2);
printf(“%d\n”,第2行);
int i=0;
int j=0;
int**mat1=(int**)malloc(rowCount1*sizeof(int*);
对于(i=0;i
//Check if number of col in mat1 is same as number of rows in mat2
if(columCount1 != rowCount2)
{
puts("The number of columns in Matrix 1 is not same as the number of rows in Matrix 2");
exit(1);
}
2) 两个矩阵相乘的代码:
for(i=0;i<rowCount1;i++)
{
for(j=0;j<columCount2;j++)
{
mat3[i][j]=0;
for(k=0;k<columCount1;k++)
{
mat3[i][j] = mat3[i][j]+mat1[i][k] * mat2[k][j];
}
}
}
当您阅读了足够多的内容以了解行数时,您可能已经到达了文件的末尾,因此while(!feof(fr1))
没什么用…请尝试fseek
返回到文件的开头。谢谢。使用fseek解决了问题,但现在我得到了将两个矩阵相乘为000的结果。我发现,现在你删除了NetVipeC的漂亮编辑,这看起来又很糟糕。也就是说,使用调试器应该可以让你找到矩阵的错误所在应用程序(提示:当您将sum
赋值给mat3
?)时,mat3[i][j]的值是多少=sum;
移动到for循环的内部。getline
的第二个参数类型是size\t*
,而不是int*
@user3648814:BLUEPIXY正确地指出了代码中的问题。在继续矩阵乘法之前,也不要忘记检查columCount1==rowCount2
。
/* After printing the results free the memory */
for(i=0; i< rowCount1; i++)
free( mat1[i]);
free(mat1);
for(i=0; i< rowCount2; i++)
free( mat2[i]);
free(mat2);
for(i=0; i< rowCount1; i++)
free( mat3[i]);
free(mat3);
free(data);