简体   繁体   中英

Matrix Multiplication C

I have checked and checked the code over and over everything should be fine , so i don't get it. I have saw a video on practically the same thing and the code was the same but i keep getting a BUS error.

I tried a 2x2 Matrix and than worked but when i did a 2x3 it didn't

#include<stdio.h>

void main(void)

 {
   int i,j,k,x,y; //i denotes rows, j denotes columns which depends on the matrix 
   int C[x][y]; //denotes the multiplied matrix

//declaring matrix A
   int A[2][3]={ {1,2,3},{4,5,6} };

   printf("MATRIX A\n");

   for(i=0; i<2; i++) //selecting the row
    {
        for(j=0; j<3; j++) // selecting the column
        {
            printf("%d|",A[i][j]);
        }
        printf("\n\n"); //to declare the spacing 
    }


//declaring matrix B
   int B[3][2]={ {7,8},{9,10},{11,12} };

   printf("MATRIX B\n");

    for(i=0; i<3; i++) //selecting the row
        {
            for(j=0; j<2; j++) // selecting the column
            {
             printf("%3d|",B[i][j]);
            }
            printf("\n\n");
         }


//multiplying the A & B matrix
   printf("MULTIPLICATION OF MATRIX A & B\n");

     for(x=0; x<2; x++)
        {
            for(y=0; y<2; y++)
            {
                for(k=0;k<3;k++)
                {
                    C[x][y] = C[x][y] + A[x][k]*B[k][y];
                }
            }          
        }
    for(x=0; x<2; x++)
        {
            for(y=0; y<2; y++)
            {
                printf("%3d|", C[x][y]);
            }
            printf("\n");
        }
}

It should simply multiply the 2 matrices

As others mentioned in the comments, your bug was that you tried to declare a variable-length array using uninitialized variables as the dimensions.

I'd advise putting your matrix operations into library functions, instead of repeating yourself. You can make these work with variable array shapes by passing in the dimensions as parameters, like so:

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

#define M 2
#define N 3
#define P 2

void int2d_print( ptrdiff_t m, ptrdiff_t n, const int a[m][n] )
/* A bare-bones routine to print matrices containing small integers
 * to stdout.
 */
{
  // Sanity-checking the parameters:
  assert(m > 0);
  assert(n > 0);
  assert(a);

  for ( ptrdiff_t i = 0; i < m; ++i ) {
    fputc( '[', stdout );

    for ( ptrdiff_t j = 0; j < n; ++j ) {
      printf( " %4d", a[i][j] );
    }

    fputs( " ]\n", stdout );
  }
  fputc( '\n', stdout );
}

int* int2d_mul( ptrdiff_t m, ptrdiff_t n, ptrdiff_t p,
                const int a[m][n],
                const int b[n][p],
                int c[m][p] )
/* Sets the array c = ab.  Returns (int*)c.
 */
{
  // Sanity-checking the parameters:
  assert(m > 0);
  assert(n > 0);
  assert(p > 0);
  assert(a);
  assert(b);
  assert(c);

 /* There are better algorithms than this, and it is a good candidate for
  * parallelization.
  */
  for( ptrdiff_t i = 0; i < m; ++i )
    for ( ptrdiff_t j = 0; j < p; ++j ) {
      int x = 0;
      for ( ptrdiff_t k = 0; k < n; ++k ) {
        x += a[i][k] * b[k][j];
      }
      c[i][j] = x;
    }

  return (int*)c;
}

// Test driver for the previous functions:
int main(void)
{
  // Declaring these static is redundant in this context.  
  static const int a[M][N]={ {1,2,3},{4,5,6} };
  static const int b[N][P]={ {7,8},{9,10},{11,12} };
  static int c[M][P];

  printf("MATRIX B\n");
  int2d_print( M, N, a );

  printf("MATRIX B\n");
  int2d_print( N, P, b );

  printf("PRODUCT OF A & B\n");
  int2d_mul( M, N, P, a, b, c );
  int2d_print( M, P, c );

  return EXIT_SUCCESS;
}

I personally prefer to use ptrdiff_t for array subscripts, because it's the correct width, makes it easier to detect overflow and underflow, and avoids conversion bugs like the infamous -3 > 1U . You can easily change this to match your own coding style.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM