使用动态分配的 C 中的矩阵乘法(程序崩溃)
Matrix Multiplication in C using Dynamic Allocation (Program Crashes)
所以正如标题中所述,我有一个程序可以乘以从文件中读取的矩阵,但是当我 运行 它只是崩溃了。我需要有两个函数来执行乘法和一个函数来打印结果,同时使用没有 return 值的指针。感谢任何帮助。
#include<stdio.h>
#include<stdlib.h>
void mat_mult(int ** arr1, int rows1, int cols1, int ** arr2,
int rows2, int rcols2, int ** arr3);
void mat_out(int ** arr, int rows, int cols);
int main(void){
int **mat1, **mat2, **res, rows1, cols1, rows2, cols2, i, j;
FILE* f;
f = fopen("Matrices.txt", "r");
fscanf(f, "%d", &rows1);
fscanf(f, "%d", &cols1);
mat1 = (int**) malloc(rows1*sizeof(int*));
for(i = 0;i < rows1;i++){
mat1[i] = (int*)malloc(cols1*sizeof(int));
}
for(i = 0;i < rows1;i++){
for(j = 0;j < cols1;j++){
fscanf(f, "%d", &mat1[i][j]);
}
}
fscanf(f, "%d", &rows2);
fscanf(f, "%d", &cols2);
mat2 = (int**) malloc(rows2*sizeof(int*));
for(i = 0;i < rows2;i++){
mat2[i] = (int*)malloc(cols2*sizeof(int));
}
for(i = 0;i < rows2;i++){
for(j = 0;j < cols2;j++){
fscanf(f, "%d", &mat2[i][j]);
}
}
res = (int**)calloc(rows1,sizeof(int*));
for(i = 0;i < rows1;i++){
res[i] = (int*)calloc(cols2,sizeof(int));
}
/*mat_mult(mat1, rows1, cols1, mat2, rows2, cols2, res);
mat_out(res, rows1, cols2);*/
}
void mat_mult(int ** mat1, int rows1, int cols1, int ** mat2,
int rows2, int cols2, int ** res){
int i, j, k;
for(i = 0;i < rows1;i++){
for(j = 0;j < cols2;j++){
res[i][j] = 0;
for(k = 0;k < cols1;k++){
res[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}
void mat_out(int ** res, int rows, int cols){
int i, j;
for(i = 0;i < rows;i++){
for(j = 0;j < cols;j++){
printf("%d ",res[i][j]);
}
printf("\n");
}
}
我没有看到任何错误,所以我编译并执行了你的程序,它按预期工作,也许你的问题是你添加到文件中的数据 Matrices.txt
。
我的文件 Matrices.txt
是:
2
2
3
2
1
4
2
2
3
2
1
4
此文件将生成 2 个矩阵,其单元格等于:
3 2
1 4
乘法结果为:
非常好。
所以正如标题中所述,我有一个程序可以乘以从文件中读取的矩阵,但是当我 运行 它只是崩溃了。我需要有两个函数来执行乘法和一个函数来打印结果,同时使用没有 return 值的指针。感谢任何帮助。
#include<stdio.h>
#include<stdlib.h>
void mat_mult(int ** arr1, int rows1, int cols1, int ** arr2,
int rows2, int rcols2, int ** arr3);
void mat_out(int ** arr, int rows, int cols);
int main(void){
int **mat1, **mat2, **res, rows1, cols1, rows2, cols2, i, j;
FILE* f;
f = fopen("Matrices.txt", "r");
fscanf(f, "%d", &rows1);
fscanf(f, "%d", &cols1);
mat1 = (int**) malloc(rows1*sizeof(int*));
for(i = 0;i < rows1;i++){
mat1[i] = (int*)malloc(cols1*sizeof(int));
}
for(i = 0;i < rows1;i++){
for(j = 0;j < cols1;j++){
fscanf(f, "%d", &mat1[i][j]);
}
}
fscanf(f, "%d", &rows2);
fscanf(f, "%d", &cols2);
mat2 = (int**) malloc(rows2*sizeof(int*));
for(i = 0;i < rows2;i++){
mat2[i] = (int*)malloc(cols2*sizeof(int));
}
for(i = 0;i < rows2;i++){
for(j = 0;j < cols2;j++){
fscanf(f, "%d", &mat2[i][j]);
}
}
res = (int**)calloc(rows1,sizeof(int*));
for(i = 0;i < rows1;i++){
res[i] = (int*)calloc(cols2,sizeof(int));
}
/*mat_mult(mat1, rows1, cols1, mat2, rows2, cols2, res);
mat_out(res, rows1, cols2);*/
}
void mat_mult(int ** mat1, int rows1, int cols1, int ** mat2,
int rows2, int cols2, int ** res){
int i, j, k;
for(i = 0;i < rows1;i++){
for(j = 0;j < cols2;j++){
res[i][j] = 0;
for(k = 0;k < cols1;k++){
res[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}
void mat_out(int ** res, int rows, int cols){
int i, j;
for(i = 0;i < rows;i++){
for(j = 0;j < cols;j++){
printf("%d ",res[i][j]);
}
printf("\n");
}
}
我没有看到任何错误,所以我编译并执行了你的程序,它按预期工作,也许你的问题是你添加到文件中的数据 Matrices.txt
。
我的文件 Matrices.txt
是:
2
2
3
2
1
4
2
2
3
2
1
4
此文件将生成 2 个矩阵,其单元格等于:
3 2
1 4
乘法结果为:
非常好。