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.