### Still can't get the function right with a matrix

So I read the function 1 and 2 on the forum and kinda understand it. But I cannot apply it to when it comes to matrices. The homework problem wants me to write functions on multiplying and adding functions. I got the math part right as you can see on line 36 and 63.

So now I am trying to write function where I can add the two first, but I keep coming up with errors.

My function is line 7 and my recall of that function is line 35. I've tried different ways of writing it and keep getting errors. Please help with this one function and so I can write the other function for multiplying them.

Thank you!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384`` ``````#include #include using namespace std; int addMatrix(int MA[3][3], int MB[3][3]){ int MC[3][3]; for (int i=0;i<=2;i++){ for (int j=0;j<=2;j++) { MC[i][j]=MA[i][j]+MB[i][j]; } } return MC[3][3]; } int main(int argc, char *argv[]) { int matrixA[3][3], matrixB[3][3], matrixC[3][3], multMatrix[3][3]; //Enter info for the first matrix cout<<"\tEnter the values for Matrix A in 3 by 3: "<>matrixA[i][j]; }} //Enter the info for the second matrix cout<<"\n\nEnter the values for Matrix B in 3 by 3: "<>matrixB[i][j]; }} matrixC[3][3]= addMatrix(matrixA[3][3], matrixB[3][3]); //This will add the two matrices together // for (int i=0;i<=2;i++){ // for (int j=0;j<=2;j++) { // matrixC[i][j]=matrixA[i][j]+matrixB[i][j]; // } // } //Displays the matrices cout<<"\n\nMatrix A and Matrix B\n\n"; cout<
Arrays in C++ are basically pointers. In your function:
Line 7: allocating memory for array;
Line 14: Returning pointer to MC[0][0]
Line 15: MC goes out of scope and is deallocated.

Posiible solutions:
b) Pass array you, want write result to, to your addMatrix function.
MiinIPaa is correct. You need to pass the result array to AddMatrix Function.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283`` ``````#include #include #include using namespace std; void addMatrix(int MC[3][3],int MA[3][3], int MB[3][3]) { for (int i=0;i<=2;i++){ for (int j=0;j<=2;j++) { MC[i][j]=MA[i][j]+MB[i][j]; } } } int main(int argc, char *argv[]) { int matrixA[3][3], matrixB[3][3], matrixC[3][3], multMatrix[3][3]; //Enter info for the first matrix cout<<"\tEnter the values for Matrix A in 3 by 3: "<>matrixA[i][j]; }} //Enter the info for the second matrix cout<<"\n\nEnter the values for Matrix B in 3 by 3: "<>matrixB[i][j]; }} addMatrix(matrixC,matrixA,matrixB); //This will add the two matrices together // for (int i=0;i<=2;i++){ // for (int j=0;j<=2;j++) { // matrixC[i][j]=matrixA[i][j]+matrixB[i][j]; // } // } //Displays the matrices cout<<"\n\nMatrix A and Matrix B\n\n"; cout<

Compiled and tested on Code Blocks.
Thank you very much!

So when I am calling the function for a matrix operation, I don't need to add the [3][3] on line 34. Is that because it was already defined?

But when I am writing a function, I need to specify the [3][3] like on line 7?

Thank you again.
I have a very similar problem i'm encountering myself with a solution. I was confused as to if you could just utilize a nested for loop such as

int i, j;
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{

Rather than just as you did with a third for loop with i j k going to less 2.
Topic archived. No new replies allowed.