开发者

5x5 matrix multiplication in C

I am stuck on this problem in my homework. I've made it this far and am sure the problem is in my three for loops. The question directly says to use 3 for loops so I know this is probably just a logic error.

#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");
 }

}

EDIT: Here is the question, sorry for not posting it the first time.

Write a C function to multiply two five by five matrices. The prototype should read

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

The resulting matrix product (a times b) is returned in the two dimensional array c (the third parameter of the function). Program your solution using three nested for loops (each generating the counter values 0, 1, 2, 3, 4) That is, DO NOT code specific formulas for the 5 by 5 case in the problem, but make your code general so it can be easily changed to compute the product of larger square matrices. Write a main program to test your function using the arrays

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

Print your matrices in a neat format using a C function created for printing five by five matrices. Print all three matrices. Generate your test arrays in your main program using the C array initialization feature.


How come your printMat_5x5 loops have conditions i < 5 and j < 5, but your matMult loops have conditions i <= 2, j <= 4, and k <= 3?


In matMult(), all the loop limits should be 5 since you are multiplying 5x5 matrices. Also, use the idiomatic for (i = 0; i < dimension; i++) instead of for (i = 0; i <= dimension_minus_one; i++).


The output I get from the program below is:

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

The code I used is this:

#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);
}

If anyone would care to explain how to indicate that the two input matrices to matmul() are constant and the third is not, I'd appreciate it.

Note that I ignored the possibility of an error return from matmul().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜