Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 2x2矩阵乘法_C_Visual Studio_Matrix_Matrix Multiplication - Fatal编程技术网

C 2x2矩阵乘法

C 2x2矩阵乘法,c,visual-studio,matrix,matrix-multiplication,C,Visual Studio,Matrix,Matrix Multiplication,我正试图写一个程序来计算两个矩阵的和和和积,但我不能让它的积工作。总数很好。我正在使用VisualStudio 以下是我的节目: #include <stdio.h> #include <stdlib.h> int main() { float a[2][2], b[2][2], c[2][2], d[2][2], sum; int i,j,k; for(i = 0; i < 2; i++) { for(j = 0; j

我正试图写一个程序来计算两个矩阵的和和和积,但我不能让它的积工作。总数很好。我正在使用VisualStudio

以下是我的节目:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float a[2][2], b[2][2], c[2][2], d[2][2], sum;
    int i,j,k;

    for(i = 0; i < 2; i++) {  
      for(j = 0; j < 2; j++) {  
        d[i][j] = 0;
      }
    }

    printf("Enter the elements of 1st matrix\n");
    /*
     * Reading two dimensional Array with the help of two for loop. If there
     * was an array of 'n' dimension, 'n' numbers of loops are needed for
     * inserting data to array.
     */   
    for (i = 0; i < 2; ++i)      
      for (j = 0; j < 2; ++j) {
        printf("Enter a%d%d: ", i + 1, j + 1);
        scanf("%f", &a[i][j]);
      }

    printf("\nEnter the elements of 2nd matrix\n");
    for (i = 0; i < 2; ++i)
      for (j = 0; j < 2; ++j) {
        printf("Enter b%d%d: ", i + 1, j + 1);
        scanf("%f", &b[i][j]);
      }

    printf("\nMatrix 1:\n");
    for (i = 0; i < 2; ++i)
      for(j = 0; j < 2; ++j) {
        printf("%.1f\t", a[i][j]);  
        if (j == 1)             /* To display matrix sum in order. */
          printf("\n");
      }
    printf("\nMatrix 2:\n");

    for(i = 0; i < 2; ++i)
      for(j = 0; j < 2; ++j) {
        printf("%.1f\t", b[i][j]);  
        if (j == 1)             /* To display matrix sum in order. */
           printf("\n");
      }

    for (i = 0; i < 2; ++i)
      for(j = 0; j < 2; ++j) {
        /* Writing the elements of multidimensional array using loop. */
        c[i][j]=a[i][j]+b[i][j];  /* Sum of corresponding elements of two arrays. */
      }
    printf("\nSum Of Matrix:\n");
    for (i = 0; i < 2; ++i)
      for(j = 0; j < 2; ++j) {
        printf("%.1f\t",c[i][j]);  
        if (j == 1)             /* To display matrix sum in order. */
          printf("\n");
      }

    for(i = 0 ; i < 2 ; i++) {  
      for(j = 0 ; j < 2 ; j++) {  
        sum = 0 ;  
        for(k = 0 ; k < 2 ; k++) {  
          sum = sum + a[i][k] * b[k][j];
          printf("a: %d; b: %d\n", a[i][k], b[k][j]);
          printf("%d", sum);
        }
        d[i][j]=sum;
      }  
    }  
    printf("\nThe multiplication matrix is : \n\n") ;  
    for(i = 0; i < 2; i++) {  
      for(j = 0; j < 2; j++) {  
        printf("%d \t", d[i][j]) ;  
      }  
      printf("\n") ;  
    } 

    system("PAUSE");
    return 0;
}

我看不出矩阵A和B的问题在哪里。

首先,我不会将for循环的终止硬编码为常量,而是将其编码为
sizeof(A)/sizeof(A[0])
。其次,问题是您试图打印浮点,因为ints-第68行显示:

printf(“a:%d;b:%d\n”,a[i][k],b[k][j])

但应该是这样

printf(“a:%.1f;b:%.1f\n”,a[i][k],b[k][j])


这些问题存在于第68、69和79行。如果您将%d更改为%f,您应该不会有问题。

是的,或者甚至是
%.1f
来匹配前面的语句。感谢@WeatherVane,将您的建议合并到编辑中。优秀的编译器会警告有关格式说明符和参数不匹配的问题,如
float sum。。。printf(“%d”,总和)。节省时间,确保编译器警告完全启用或考虑一个新编译器。
Matrix 1:
1.0     2.0
3.0     4.0

Matrix 2:
5.0     6.0
7.0     8.0

Sum Of Matrix:
6.0     8.0
10.0    12.0
a: 0; b: 1072693248
0a: 0; b: 1073741824
0a: 0; b: 1072693248
0a: 0; b: 1073741824
0a: 0; b: 1074266112
0a: 0; b: 1074790400
0a: 0; b: 1074266112
0a: 0; b: 1074790400
0
The multiplication matrix is :

0       0
0       0