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.