C中的5x5矩阵乘法

时间:2010-03-15 01:49:34

标签: c matrix

我在作业中遇到了这个问题。我已经做到了这一点,并确定问题出在我的三个for循环中。问题直接说使用3 for循环所以我知道这可能只是一个逻辑错误。

#include<stdio.h>

void matMult(int A[][5],int B[][5],int C[][5]);
int printMat_5x5(int A[5][5]);

int main() {

 int A[5][5] = {{1,2,3,4,6},
       {6,1,5,3,8},
       {2,6,4,9,9},
       {1,3,8,3,4},
       {5,7,8,2,5}};

 int B[5][5] = {{3,5,0,8,7},
       {2,2,4,8,3},
       {0,2,5,1,2},
       {1,4,0,5,1},
       {3,4,8,2,3}};

 int C[5][5] = {0};

 matMult(A,B,C);

 printMat_5x5(A);
 printf("\n");

 printMat_5x5(B);
 printf("\n");

 printMat_5x5(C);

 return 0;

}

void matMult(int A[][5], int B[][5], int C[][5])
{
 int i;
 int j;
 int k;

 for(i = 0; i <= 2; i++) {
  for(j = 0; j <= 4; j++) {
   for(k = 0; k <= 3; k++) {
    C[i][j] +=  A[i][k] * B[k][j]; 
   }
  }
 }

}

int printMat_5x5(int A[5][5]){

 int i;
 int j;

 for (i = 0;i < 5;i++) {
  for(j = 0;j < 5;j++) {

   printf("%2d",A[i][j]);
  }

  printf("\n");
 }

}

编辑: 这是一个问题,抱歉没有第一次发布。

  

写一个C函数乘以两个五乘五个矩阵。原型应该阅读

void matMult(int a[][5],int b[][5],int c[][5]);
     

生成的矩阵乘积(ab)在二维数组c(函数的第三个参数)中返回。使用三个嵌套的for循环编程你的解决方案(每个循环生成计数器值0,1,2,3,4)也就是说,不要为问题中的5乘5的情况编写特定的公式,但要使代码通用,以便它可以很容易改变以计算更大的方形矩阵的乘积。编写一个主程序来使用数组测试你的函数

a:
1 2 3 4 6
6 1 5 3 8
2 6 4 9 9
1 3 8 3 4
5 7 8 2 5
b:
3 5 0 8 7
2 2 4 8 3
0 2 5 1 2 
1 4 0 5 1
3 4 8 2 3
     

使用为打印五到五个矩阵而创建的C函数,以简洁的格式打印矩阵。打印所有三个矩阵。使用C阵列初始化功能在主程序中生成测试阵列。

2 个答案:

答案 0 :(得分:4)

您的printMat_5x5循环如何具有条件i < 5j < 5,但您的matMult循环具有条件i <= 2j <= 4和{ {1}}?

答案 1 :(得分:4)

matMult()中,所有循环限制应为5,因为您乘以5x5矩阵。另外,请使用惯用语for (i = 0; i < dimension; i++)代替for (i = 0; i <= dimension_minus_one; i++)


我从以下程序得到的输出是:

Matrix A:
  1, 2, 3, 4, 6
  6, 1, 5, 3, 8
  2, 6, 4, 9, 9
  1, 3, 8, 3, 4
  5, 7, 8, 2, 5
Matrix B:
  3, 5, 0, 8, 7
  2, 2, 4, 8, 3
  0, 2, 5, 1, 2
  1, 4, 0, 5, 1
  3, 4, 8, 2, 3
Matrix C:
   29,  55,  71,  59,  41
   47,  86,  93,  92,  82
   54, 102, 116, 131,  76
   24,  55,  84,  63,  47
   46,  83, 108, 124,  89

我使用的代码是:

#include <stdio.h>

static int matmul(size_t ax, size_t ay, int a[ax][ay],
                  size_t bx, size_t by, int b[bx][by],
                  size_t cx, size_t cy, int c[cx][cy])
{
    if (ay != bx || ax != cx || by != cy)
        return(-1);  /* Non-compatible matrices */

    size_t i, j, k;

    /* Zero result */
    for (i = 0; i < cx; i++)
    {
        for (j = 0; j < cy; j++)
            c[i][j] = 0;
    }

    /* Compute result - no care about overflows */
    for (i = 0; i < ax; i++)
    {
        for (j = 0; j < by; j++)
        {
            for (k = 0; k < ay; k++)
                c[i][j] += a[i][k] * b[k][j];
        }
    }

    return(0);
}

static void matminmax(size_t ax, size_t ay, int a[ax][ay],
                      int *pmin, int *pmax)
{
    size_t i, j;
    int max = a[0][0];
    int min = a[0][0];
    for (i = 0; i < ax; i++)
    {
        for (j = 0; j < ay; j++)
        {
            if (a[i][j] > max)
                max = a[i][j];
            else if (a[i][j] < min)
                min = a[i][j];
        }
    }
    *pmin = min;
    *pmax = max;
}

static void set_printformat(const char *pfx, const char *sfx,
                            size_t ax, size_t ay, int a[ax][ay],
                            char *buffer, size_t buflen)
{
    int min, max;
    matminmax(ax, ay, a, &min, &max);
    int len1 = snprintf(0, 0, "%d", min);
    int len2 = snprintf(0, 0, "%d", max);
    if (len2 > len1)
        len1 = len2;
    snprintf(buffer, buflen, "%s%d%s", pfx, len1, sfx);
}

static void matprt(size_t ax, size_t ay, int a[ax][ay], const char *tag)
{
    size_t i, j;
    char format[32];

    set_printformat("%s%", "d", ax, ay, a, format, sizeof(format));
    printf("%s:\n", tag);
    for (i = 0; i < ax; i++)
    {
        const char *pad = "  ";
        for (j = 0; j < ay; j++)
        {
            printf(format, pad, a[i][j]);
            pad = ", ";
        }
        putchar('\n');
    }
}

int main(void)
{
    int a[5][5] =
    {
        { 1, 2, 3, 4, 6 },
        { 6, 1, 5, 3, 8 },
        { 2, 6, 4, 9, 9 },
        { 1, 3, 8, 3, 4 },
        { 5, 7, 8, 2, 5 },
    };
    int b[5][5] =
    {
        { 3, 5, 0, 8, 7 },
        { 2, 2, 4, 8, 3 },
        { 0, 2, 5, 1, 2 },
        { 1, 4, 0, 5, 1 },
        { 3, 4, 8, 2, 3 },
    };
    int c[5][5];

    matprt(5, 5, a, "Matrix A");
    matprt(5, 5, b, "Matrix B");
    matmul(5, 5, a, 5, 5, b, 5, 5, c);
    matprt(5, 5, c, "Matrix C");
    return(0);
}

如果有人愿意解释如何指出matmul()的两个输入矩阵是常数而第三个不是,我会很感激。

请注意,我忽略了从matmul()返回错误的可能性。