Writing a function to multiply 2 matrices?

Write a function that multiplies two matrices together and then displays the result. You can assume that both matrices are 5 x 5 matrices. Write a program that initializes your matrices then calls the function with the matrices as arguments. Declare the matrices as 2 dimensional arrays

I have written a program that multiplies 2 matrices together, can someone check if this is correct? It runs but I am not sure if it is correct?

Now all I need to do is write the function / call the function - I dont quite understand this part? Any help is appreciated.

Code I have so far: -EDITED
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdlib.h>

int multiplydisplay_array(int firstarray, int secondarray, int i, int j,int k, int sum)

int main(void)
{
  int firstarray[5][5];
  int secondarray[5][5];
  int firstbysecond[5][5];
  int i;
  int j;
  int k;
  int sum;

  for (i=0; i<5; i++)
  {
    for (j=0; j<5; j++)
    {
      printf("Enter an element for the first array:\n");
      scanf("%d", &(firstarray[i][j]));
    }
  }

  for (i=0; i<5; i++)
  {
    for (j=0; j<5; j++)
    {
      printf("Enter an element for the second array:\n");
      scanf("%d", &(secondarray[i][j]));
    }
  }

multiplydisplay_array(firstarray, secondarray, firstbysecond, i, j, k, sum);

  system("pause");

  return;
}


int multiplydisplay_array(int firstarray, int secondarray,int firstbysecond, int i,int j,int k,int sum)
{

  for (i=0; i<5; i++)
  {
    for (j=0; j<5; j++)
    {
      sum=0;
      for (k=0; k<5; k++)
      {
        sum += firstarray[i][k] * secondarray[k][j];
      }
      firstbysecond[i][j]=sum;
    }
  }

  

  printf("The first matrix multiplied by the second matrix is the matrix:\n");
  
  for (i=0; i<5; i++)
  {
    for (j=0; j<5; j++)
    {
      printf("\t %d",firstbysecond[i][j]);
    }
    printf("\n");
  }
  return (firstbysecond);
}
Last edited on
Hey. Could you post your code between code tags so it is clearer to see? They are under "format" - http://www.cplusplus.com/articles/jEywvCM9/
hey thank you thats done :)
I hope you are also using some indentation when you write your code.

The multiplication is correct. You just need to print out the result properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  int firstarray[5][5];
  int secondarray[5][5];
  int firstbysecond[5][5];
  int i;
  int j;
  int k;
  int sum;

  for (i=0; i<5; i++)
  {
    for (j=0; j<5; j++)
    {
      printf("Enter an element for the first array:\n");
      scanf("%d", &(firstarray[i][j]));
    }
  }

  for (i=0; i<5; i++)
  {
    for (j=0; j<5; j++)
    {
      printf("Enter an element for the second array:\n");
      scanf("%d", &(secondarray[i][j]));
    }
  }

  for (i=0; i<5; i++)
  {
    for (j=0; j<5; j++)
    {
      sum=0;
      for (k=0; k<5; k++)
      {
        sum += firstarray[i][k] * secondarray[k][j];
      }
      firstbysecond[i][j]=sum;
    }
  }

  /* Remember, at this point i and j both == 5, which are invalid indices into any of your matrix arrays */

  printf("The first matrix multiplied by the second matrix is:\n");
  
  /* You need another nested loop here to print the array */
  for (i=0; ...)
  {
    for (j=0; ...)
    {
      printf("%d",...);
    }
    printf("\n");
  }

  system("pause");

  return 0;
}

I would also change the stuff to input the array to just say something like:

14
15
16
17
18
19
20
21
  printf("Enter the first 5 x 5 array:\n");
  for (i=0; i<5; i++)
  {
    for (j=0; j<5; j++)
    {
      scanf("%d", &(firstarray[i][j]));
    }
  }

Hope this helps.
Hi,

I did that, not sure if this is right but I inputted 1 for every element of both matrices and it says

The first matrix multiplied by the second matrix is:

55555
55555
55555
55555
55555

is this correct?

just edited the code above, now i am having difficulties converting it into a function that multiplies the 2 matrices and displays the answer, then the function is called out in main?
Last edited on
Yes, that is correct.

See you other thread for my continuing answer.

It will help if you can do a couple of matrix multiplications by hand to help you out.

Any simple matrix times 2I (the identity matrix times 2), should produce 
the same simple matrix where all the elements are doubled.

1 2 3  2 0 0       1 2 3  2   1 2 3  0   1 2 3  0       2  4  6
4 5 6  0 2 0  -->         0          2          0  -->  8  10 12
7 8 9  0 0 2              0          0          2       14 16 18
                   
                   4 5 6  2   4 5 6  0   4 5 6  0
                          0          2          0
                          0          0          2
                          
                   7 8 9  2   7 8 9  0   7 8 9  0
                          0          2          0
                          0          0          2
                          
Yeah!

1 1 1  1 1 1       1 1 1  1   1 1 1  1   1 1 1  1       3 3 3
1 1 1  1 1 1  -->         1          1          1  -->  3 3 3
1 1 1  1 1 1              1          1          1       3 3 3

                   1 1 1  1   1 1 1  1   1 1 1  1
                          1          1          1
                          1          1          1
                   
                   1 1 1  1   1 1 1  1   1 1 1  1
                          1          1          1
                          1          1          1
                          
(Remember, that multiplying two vectors, the current row and column of the matrices being multiplied, is:

  1 2 3  4  -->  (1*4)+(2*5)+(3*6)  -->  4+10+18  -->  32
         5
         6
)

(Please forgive my doing 3x3 matrices, for brevity here. You can easily do 5x5 on your own.)

Hope this helps.

[edit] BTW, for future reference, please don't edit the stuff in your posts except to fix typos or add additional information that was necessary. This makes it so that others can see your post and get the same help you are getting.

(People don't seem to believe this, but whenever you ask a question, usually many others have the same question, but were just afraid to ask!)
Last edited on
Topic archived. No new replies allowed.