tags:

views:

549

answers:

2

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.

+3  A: 

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?

Gabe
You might also find it nice to init C to zeros *within* matMult. There's no point forcing another set up step onto the caller (the setting of the target array to all zeros).
paxdiablo
Well, since the function printMat_5x5 only has to print 5x5 matrices and the question states that matMult has to have counter values of 1,2,3,4, and 5.
Rick
@Rick, that phrase calls for counter values of 0,1,2,3,4. What that means is "for (i=0;i<5;i++) ...". In other words *each* counter has to run from 0 through 4 inclusive, not (as you seem to think), one running to 1, another to 2, another to 3 and so on.
paxdiablo
"counter values 0, 1, 2, 3, 4" means your counter goes from 0 to 4, not that you have to have your loop termination conditions set to those numbers.
Alok
If I put all the counters to five my product matrix is still a bunch of random numbers. What else could the problem be?
Rick
I found the problem. Since I was printing the last matrix with a character spacing of 2, I thought the output was gibberish. On further inspection I just needed to space them out father.Thanks for all the help.
Rick
+2  A: 

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().

Jonathan Leffler