Matrix multiplication not calculating correctly?

I Have the code written to input 2 matrices and multiply both together. These are 5 x 5 matrices.
The code runs fine, however when i input in my 2 matrices, using numbers from 1-9 consectuively, I get rather crazy digits and '-' figures. I think my calculation has gone weird somewhere. I was asked to initialize all the integer values, im not quite sure if this is right?

Could someone please correct me here?
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
72
73
74
75
 #include <stdio.h>

void matrix_multiply(int firstarray[5][5], int secondarray[5][5], int firstbysecond[5][5], int i, int j, int k, int sum); 
void display_matrix(int firstbysecond[5][5], int i, int j);

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

	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]));
		}
	}

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

	display_matrix(firstbysecond, i, j);

	system("pause");

	return;
}

void matrix_multiply(int firstarray[5][5], int secondarray[5][5],int firstbysecond[5][5], int i,int j,int k,int sum)

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

	void display_matrix(int firstbysecond[5][5], int i, int j)
	
	{
		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;
	}
It is really hard to follow a problem when you make a new topic instead of just replying to existing topics:
http://www.cplusplus.com/forum/beginner/157845/
http://www.cplusplus.com/forum/general/157861/

In programming stuff, you've got to pay really close attention to every little detail. Which is part of what is so obnoxiously difficult about it, but there are no parts you can skim.

You've made a number of perplexing changes to your code, such as changing int main(void) to void main(). The first was correct. The second is not. Change it back.

In both your first and second topics about this code, you had a correct and functional matrix multiplication. But again, you have made some confusing changes to it. Why did you do that?

If you are unsure how matrix multiplication actually works, you might want to review the answer I already gave you about it here (http://www.cplusplus.com/forum/beginner/157845/#msg808401) and maybe some online help -- the mathisfun.com site has a very good explanation (http://www.mathsisfun.com/algebra/matrix-multiplying.html).

I'm glad to see you took my advice about making two functions, but you seem to have cherry-picked it a little -- you added arguments. I understand why you did this:

    "Hmm... I need int i, j, k and sum in my functions, but they're in main(). How do I use them there?"

It's okay. Fix it thus:

1
2
3
4
5
void matrix_multiply(int firstarray[5][5], int secondarray[5][5], int firstbysecond[5][5])
{
	int i, j, k, sum;  // local variables

	for (i=0; ...

The reason we don't pass i, j, etc to the function is because they are passed by value -- they cannot be modified by the function. (You should have read the tutorial on functions like I asked you: http://www.cplusplus.com/doc/tutorial/functions/.)

The firstarray, secondarray, and firstbysecond arguments are references (pointers), so they can be modified in the function. This is exactly what you do with the firstbysecond array -- when you call the function, the function changes it. The first two are not changed only because the function doesn't actually change them.

The fact that the things have the same name don't matter. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>

void foo( int* a )
{
  *a = 12;
}

int main()
{
  int x = 9;
  printf("x = %d\n", x);

  foo( &x );  /* the function is going to change x */
  printf("x = %d\n", x);
}

It doesn't matter that the function's argument is named 'a'. The 'a' is just a pointer (a placeholder, if you will) to 'x'.

The same is true of your arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>

void foo( int a[1] )
{
  a[0] = 12;
}

int main()
{
  int x[1] = { 9 };
  printf("x = %d\n", x[0]);

  foo( x );
  printf("x = %d\n", x[0]);
}

I know this is a lot of crap to put up with. Learning to program is about learning how to think about a problem plus how to tell the computer about what to do. It's overwhelming.

You're doing okay, but I think you are getting overwhelmed and stressed. Stop, okay?

Take the time to re-read everything through, and eventually it will make better sense.

Hope this helps.
You don't accept PMs. Did you get it working correctly?
Topic archived. No new replies allowed.