开发者

Program to multiply matrix

开发者 https://www.devze.com 2023-04-10 14:12 出处:网络
Trying to make 3 x 3 matrix multiplier but it gives out wrong output. I don\'t know what I am doing wrong. Two problems that I am facing are:

Trying to make 3 x 3 matrix multiplier but it gives out wrong output. I don't know what I am doing wrong. Two problems that I am facing are:

(1) Some variables store wrong input. For example a[1][1] shows 7 although I entered 1

(2) The matrix multiplication is wrong

#include <stdio.h>
#include <conio.h>

void matrix_format(int m[2][2])
{
 int i,j;
 printf("\n\n");
 for(i=0;i<=2;i++)
 {
  for(j=0;j<=2;j++)
   {
    if(j==0)
    printf("[ %d |",m[i][j]);
    else if(j==1)
    printf(" %d |",m[i][j]);
    else if(j==2)
    printf(" %d ] \n",m[i][j]);
   }
 }
}


int main(void)
{
 void matrix_format(int [2][2]);
 int a[2][2], b[2][2], r[2][2],m,i,j;

 clrscr();

 for(m=1;m<=2;m++)
 {

  if(m==1)
   {
    printf("Enter values for the matrix A \n");
   }
  else
   {
    printf("\n\nEnter values for the matrix B \n");
   }

  for(i=0;i<=2;i++)
  {
   for(j=0;j<=2;j++)
    开发者_如何转开发{
     if(m==1)
      {
       printf("A[%d][%d] : ",i+1,j+1);
       scanf("%d",&a[i][j]);
      }
     else if(m==2)
      {
       printf("B[%d][%d] : ",i+1,j+1);
       scanf("%d",&b[i][j]);
      }
    }
  }
 }

 printf("\n Matrix A : \n");
 matrix_format(a);

 printf("\n Matrix B : \n");
 matrix_format(b);

 for(i=0;i<=2;i++)
 {
  for(j=0;j<=2;j++)
   {
    r[i][j]= a[i][j] * b[j][i];
   }
 }

 printf("\n Matrix Multiplication Result : \n");
 matrix_format(r);

 getch();
 return 0;
}

output:

Program to multiply matrix

Program to multiply matrix

Please guide me.


The first problem that jumps out is that all your arrays are 2x2, while they should be 3x3:

m[2][2]

should read

m[3][3]

and so on. The number in brackets is the size of the array, not the index of the last element.

This will explain some of the weirdness, in particular why some elements get mysteriously overwritten.

As to the actual matrix multiplication, your algorithm isn't quite right (assuming what you're trying to implement is the standard linear algebra matrix product). Think about what steps are involved in multiplying two matrices, and what your code is actually doing. Since this is homework, I'll only give you a hint:

Matrix product involves summations of element products.


There are two major problems:

First, a 3*3 matrix is represented by int matrix[3][3] not int matrix[2][2]. The reason you see strange results is that you are writing over array boundaries, effectively writing over the other matrix because their memory locations are adjacent.

Note: An array such as int a[10] can only be indexed from 0 to 9.

Another problem is your multiplication. From math, we know that if we have:

C = A x B

Then we have:

C[i][j] = sum(A[i][k]*A[k][j]) over k

That is in your case:

C[i][j] = A[i][0]*A[0][j]+A[i][1]*A[1][j]+A[i][2]*A[2][j]

So you have to have:

for over i
    for over j
        C[i][j] = 0
        for over k
            C[i][j] += A[i][k]*B[k][j]


I have written a simple matrix multiplication program without using pointers. Hopefully this would work for you. I can see that you know how to use functions, so try using them more often. Also your multiplication logic was wrong. Read up on that and then see the code. (If you want to do the matrix multiplication for let's say a 5 x 5 matrix, then you should just change #define SIZE 3 to #define SIZE 5).

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

#define SIZE 3

void CreateMatrix(char name, int m[SIZE][SIZE]) {
  int row, col;
  printf("Enter values for the matrix %c:\n", name);
  for(row = 0; row < SIZE; row++) {
    for(col = 0; col < SIZE; col++) {
      printf("%c[%d][%d] : ", name, row + 1, col + 1);
      scanf("%d", &m[row][col]);
    }
  }
  printf("\n");
}

void PrintMatrix(char name, int m[SIZE][SIZE]) {
  int row, col;
  printf("Matrix %c:\n", name);
  for (row = 0; row < SIZE; row++) {
    printf("[ ");
    for (col = 0; col < SIZE; col++) {
      printf("%d ", m[row][col]);
    }
    printf("]\n");
  }
  printf("\n");
}

void MatrixMultiply(int a[SIZE][SIZE], int b[SIZE][SIZE], int mul[SIZE][SIZE]) {
  int row, col, k;
  for (row = 0; row < SIZE; row++) {
    for (col = 0; col < SIZE; col++) {
      mul[row][col] = 0;
      for (k = 0; k < SIZE; k++) {
        mul[row][col] += a[row][k] * b[k][col];
      }
    }
  }
}

int main() {
  int a[SIZE][SIZE];
  int b[SIZE][SIZE];
  int mul[SIZE][SIZE];

  // Create Matrices
  CreateMatrix('A', a);
  CreateMatrix('B', b);

  // Matrix Multiplication
  MatrixMultiply(a, b, mul);

  // Print Matrices
  PrintMatrix('A', a);
  PrintMatrix('B', b);
  PrintMatrix('M', mul); 
}

The output:

Enter values for the matrix A:
A[1][1] : 1
A[1][2] : 2
A[1][3] : 3
A[2][1] : 4
A[2][2] : 5
A[2][3] : 6
A[3][1] : 7
A[3][2] : 8
A[3][3] : 9

Enter values for the matrix B:
B[1][1] : 1
B[1][2] : 2
B[1][3] : 3
B[2][1] : 4
B[2][2] : 5
B[2][3] : 6
B[3][1] : 7
B[3][2] : 8
B[3][3] : 9

Matrix A:
[ 1 2 3 ]
[ 4 5 6 ]
[ 7 8 9 ]

Matrix B:
[ 1 2 3 ]
[ 4 5 6 ]
[ 7 8 9 ]

Matrix M:
[ 30 36 42 ]
[ 66 81 96 ]
[ 102 126 150 ]


First, see @aix' answer regarding the array sizes. Then, the reason the multiplication doesn't work is that you are using the wrong formula. The element at i,j in the result matrix is not simply the product of i,j and j,i from the two matrices being multiplied - instead, every element in row i from the left matrix must be multiplied by the corresponding element from column j from the right matrix, and all the products must be added together. See this illustration in the Wikipedia article.


you have define array of 2*2 i.e. it has index of 0,1.but in your FOR loop you are trying to accept 3 i.e.{0,1,2 }elements in one row. so remove = sign from all FOR loops. or just change the declaration of your Array to [3][3].Also then apply right formula for Matrix multiplication i.e r[0][0]=(a[0][0]*b[0][0])+(a[0][1]*b[1][0])+(a[0][2]*b[2][0]). for first cell so on for other cells,in your case for 3*3 matrix.

0

精彩评论

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

关注公众号