我用 C 语言编写了一个矩阵乘法代码。没有错误,但是没有出现想要的输出,我的代码的哪一部分是错误的或者我遗漏了什么。
#include <stdio.h>
#include <stdlib.h>
int main() {
int r1, c1, r2, c2;
int a[r1][c1];
int b[r2][c2];
int c[r1][c2];
int i, j;
printf("enter row1 and col1:\n");
scanf("%d%d", &r1, &c1);
printf("enter row2 and col2:\n");
scanf("%d%d", &r2, &c2);
if (c1 == r2) {
printf("enter element of 1st matrix:");
for (i = 0; i < r1; i++) {
for (j = 0; j < c1; j++) {
scanf("%d", &a[i][j]);
}
}
printf("\n-----------------");
printf("enter element of 2nd matrix:");
for (i = 0; i < r2; i++) {
for (j = 0; j < c2; j++) {
scanf("%d", &b[i][j]);
}
}
printf("the resultant matrix is:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
c[i][j] += a[i][j] * b[j][i];
}
}
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
printf("%d\t", c[i][j]);
}
printf("\n\n");
}
}
return 0;
}
答案 0 :(得分:2)
代码中存在多个问题:
数组int a[r1][c1];
、int b[r2][c2];
、int c[r1][c2];
是在r1
和c1
被用户读取之前定义的:代码有潜力未定义行为,因为 r1
和 c1
未初始化,因此具有不确定的值:如果大小恰好为负并且使用迭代到不同的索引值访问它们,则数组的分配可能会失败或导致未定义的行为用户输入的边界值将具有未定义的行为。
您验证 a
的列数等于 b
的行数,但您还应该检查所有维度是否为正以避免潜在的未定义行为。< /p>
将矩阵定义为具有自动存储功能的可变长度数组是有风险的:大尺寸可能会导致堆栈溢出。建议从堆中分配矩阵。
乘法算法不正确:您必须实现一个三重循环并在内循环之前在 c1[i][j]
处初始化目标元素。
这是修改后的版本:
#include <stdio.h>
#include <stdlib.h>
int main() {
int r1, c1, r2, c2;
printf("enter row1 and col1:\n");
if (scanf("%d%d", &r1, &c1) != 2)
return 1;
printf("enter row2 and col2:\n");
if (scanf("%d%d", &r2, &c2) != 2)
return 1;
if (r1 <= 0 || c1 <= 0 || r2 <= 0 || c2 <= 0 || c1 != r2) {
printf("invalid matrix sizes\n");
return 1;
} else {
#if !defined ALLOCATE_MATRICES_FROM_THE_HEAP
// if the dimensions are small, you can define the matrices as
int a[r1][c1], b[r2][c2], c[r1][c2];
#else
// for large sizes, you can allocate the matrices from the heap this way:
int (*a)[c1] = calloc(sizeof(*a), r1);
int (*b)[c2] = calloc(sizeof(*b), r2);
int (*c)[c2] = calloc(sizeof(*c), r1);
if (a == NULL || b == NULL || c == NULL) {
printf("out of memory\n");
return 1;
}
#endif
printf("enter elements of 1st matrix:");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
a[i][j] = 0;
scanf("%d", &a[i][j]);
}
}
printf("\n-----------------");
printf("enter elements of 2nd matrix:");
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
b[i][j] = 0;
scanf("%d", &b[i][j]);
}
}
printf("the resultant matrix is:\n");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
int v = 0;
for (int k = 0; k < c1; k++) {
v += a[i][k] * b[k][j];
}
c[i][j] = v;
}
}
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
printf("%5d\t", c[i][j]);
}
printf("\n\n");
}
#if defined ALLOCATE_MATRICES_FROM_THE_HEAP
free(a);
free(b);
free(c);
#endif
return 0;
}
}
答案 1 :(得分:1)
问题出在这里:
int r1, c1, r2, c2;
int a[r1][c1]; // at this point r1 c1, r2, c2 etc.
int b[r2][c2]; // are not yet initialized and their
int c[r1][c2]; // content is undetermined. Therefore
// the size of the arrays a, b and C are
// undetermined leading to the problem you encounter
你想要这个:
int r1,c1,r2,c2;
int i,j;
printf("enter row1 and col1:\n");
scanf("%d%d",&r1,&c1);
printf("enter row2 and col2:\n");
scanf("%d%d",&r2,&c2);
int a[r1][c1]; // here r1, c1, r2, c2 etc.
int b[r2][c2]; // have determined values
int c[r1][c2];
您的代码中可能还有更多问题,我没有检查。
答案 2 :(得分:1)
您发布的程序使用来自未初始化变量的未定义值对三个矩阵进行标注。一旦您知道维度的值,您至少可以对这些变量进行维度,如(请阅读我在您的代码中的注释,因为它们说明了您尚未意识到的代码中的问题):
#include <stdio.h>
#include <stdlib.h>
int main()
{
int r1, c1, r2, c2;
int i, j;
printf("enter row1 and col1:\n");
scanf("%d%d", &r1, &c1);
printf("enter row2 and col2:\n");
scanf("%d%d", &r2, &c2);
int a[r1][c1]; /* declare the dimensions <<<after>>> you know what values are */
int b[r2][c2]; /* to be given. */
int c[r1][c2];
if (c1 == r2) { /* put an else statement to this if indicating the cause of */
/* not making any calculation in case r2 and c1 not being equal */
printf("enter element of 1st matrix:");
for (i = 0; i < r1; i++) {
for (j = 0; j < c1; j++) {
scanf("%d", &a[i][j]);
}
}
printf("\n-----------------");
printf("enter element of 2nd matrix:");
for (i = 0; i < r2; i++) {
for (j = 0; j < c2; j++) {
scanf("%d", &b[i][j]);
}
}
printf("the resultant matrix is:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) { /* you need three nested loops, not two */
/* ALL YOUR CODE HERE IS BAD, USE THIS INSTEAD */
int k;
c[i][j] = 0; /* initialize c[i][j] to zero or you'll get weird values */
for (k = 0; k < c1; k++) { /* YOU NEED ANOTHER LOOP */
c[i][j] += a[i][k] * b[k][j];
}
}
}
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
printf("%d\t", c[i][j]);
}
printf("\n\n");
}
}
return 0;
}
但是给你的矩阵提供最大维度并且只在由 r1
、c1
、r2
、c2
、{ {1}} 和 r3
。如:
c3
如您所见,最后一个示例不需要存储乘积矩阵 #include <stdio.h>
#include <stdlib.h>
#define MAX_DIM 10
int main()
{
int r1, c1, r2, c2;
int i, j; /* why didn't you call these r and c (for row and column resp.)? */
/* this will loop on input until you give correct values for all dimensions, */
do {
printf("enter row1 and col1:\n");
scanf("%d%d", &r1, &c1);
printf("enter row2 and col2:\n");
scanf("%d%d", &r2, &c2);
} while (r1 <= 0 || r1 > MAX_DIM || c1 <= 0 || c1 > MAX_DIM ||
r2 <= 0 || r2 > MAX_DIM || c2 <= 0 || c2 > MAX_DIM ||
c1 != r2);
int a[MAX_DIM][MAX_DIM]; /* more portable this way, as variable dimensioning */
int b[MAX_DIM][MAX_DIM]; /* is not present in all standard editions */
/* there's no need to have matrix c as we are not doing anything with it.
* (see below why)*/
printf("enter element of 1st matrix:");
for (i = 0; i < r1; i++) {
for (j = 0; j < c1; j++) {
scanf("%d", &a[i][j]);
}
}
printf("\n-----------------");
printf("enter element of 2nd matrix:");
for (i = 0; i < r2; i++) {
for (j = 0; j < c2; j++) {
scanf("%d", &b[i][j]);
}
}
printf("the resultant matrix is:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
int k;
int cell_value = 0;
for (k = 0; k < c1 /* or k < r2, both are the same */; k++) {
cell_value += a[i][k] * b[k][j];
}
/* cell_value is the value of product matrix at [i][j] */
printf("%d\t", cell_value);
}
printf("\n\n"); /* new line for each row */
}
return 0;
}
,因为您可以在计算时打印元素(按行生成,然后按列生成)。>
答案 3 :(得分:-2)
#include<stdio.h>
#include<stdlib.h>
int main(){
int r1,c1,r2,c2;
int i,j;
printf("enter row1 and col1:\n");
scanf("%d%d",&r1,&c1);
printf("enter row2 and col2:\n");
scanf("%d%d",&r2,&c2);
int a[r1][c1];
int b[r2][c2];
int c[r1][c2];
if(c1==r2){
printf("enter element of 1st matrix:");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
scanf("%d",&a[i][j]);
}
}
printf("\n-----------------");
printf("enter element of 2st matrix:");
for(i=0;i<r2;i++){
for(j=0;j<c2;j++){
scanf("%d",&b[i][j]);
}
}
printf("the resultant matrix is:\n");
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
c[i][j]=0;
for(int k=0;k<c1;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
printf("%d\t",c[i][j]);
}
printf("\n\n");
}
}
return 0;
}