Matrix multiplication function? Almost complete

Hi,

so this is the assignment:

Write a function that multiplies to 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.

So I have the program written to multiply two matrices and display the answer. However, I can't get a grasp of converting this into a function of the program and calling the function out? Can someone please help me with this aspect? Everytime I run it an error comes up saying 'subscript requires array or pointer type'. I know i'm going wrong somewhere around calling out the function, listing the arguments or the return values or something?

Can someone please rectify?

This is my attempt so far:

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
#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
You are on the right track, actually. :O)
There are just a few things you haven't considered properly.


(1) type (or types of things)
Everything has a type. An integer (int) is different than a floating-point (double) is different than a character (char), etc. This much you know.

The trick is to make sure the type of everything matches.

So, in your code, you are trying to pass a two-dimensional array (int firstarray[5][5]) as an integer (int firstarray). These things are different kinds of things, so it won't work. (Even though they have the same name. There might be two Kevins in your class, but they are different people. You can't just swap one for the other.)

In order to pass the array, you must make the argument to your function take an array of the same type:

void multiply_array(int firstarray[5][5], ...)

After that, your main() function can say:

multiply_array(firstarray, ...);

With me so far? (That is your main problem.)


(2) functions
The purposes of a function are several:
    1. "Modularize" -- provide a way to do the same thing over and over without having to write the same code over and over. (You write the code to do it once, and then you can "call" it as many times as you like.)
    2. Make code easier to understand.
    3. Obtain a value.

A function should do just one thing. Your big function does two things -- it multiplies the arrays and it displays the result. That's too much. Try to break it down into two functions: one to multiply the arrays and one to display the result.

Another thing to notice is that there is no single number that can represent a matrix (a whole bunch of numbers). So there is no point in trying to obtain a single number that does it.

Here are some function prototypes I can recommend to you:

1
2
3
4
void multiply_matrices(int matrix1[5][5], int matrix2[5][5], int result[5][5]);
/* Function multiplies the two matrices and puts the answer in result. */

void display_matrix(int matrix[5][5]);

Now in main() you can take your two matrices and get the answer:

1
2
3
4
5
6
7
8
9
10
11
int main()
{
  int firstarray[5][5];
  int secondarray[5][5];
  int firstbysecond[5][5];

  ...

  multiply_matrices(firstarray, secondarray, firstbysecond);

  display_matrix(firstbysecond);

I hope this makes better sense.

If you haven't, it might be worth your time to review the Tutorial on functions.
http://www.cplusplus.com/doc/tutorial/functions/

Finally, notice that you are getting the value of a matrix from the user using the same code, twice? (The only difference in the two is the target matrix.)

Why not create and use a function to do that?

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
void get_matrix_from_user(int matrix[5][5]);

int main()
{
  int firstarray[5][5];
  int secondarray[5][5];
  ...

  printf("Please enter the first 5 x 5 matrix:\n";
  get_matrix_from_user(firstarray);

  printf("Now enter the second 5 x 5 matrix:\n";
  get_matrix_from_user(secondarray);

  ...
}

void get_matrix_from_user(int matrix[5][5])
{
  int i, j;
  for (i=0; i<5; i++)
  {
    for (j=0; j<5; j++)
    {
      // Don't printf() anything here... for the same reason given in my last answer
      scanf("%d", &(matrix[i][j]));
    }
  }
}

Hope this helps.
Topic archived. No new replies allowed.