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!



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
76
77
78
79
80
81
82
83
84
#include <cmath>
#include <iostream>

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: "<<endl;
   for (int i=0;i<=2;i++){
         for (int j=0;j<=2;j++) {
             cin>>matrixA[i][j];
             }}

   //Enter the info for the second matrix
   cout<<"\n\nEnter the values for Matrix B in 3 by 3: "<<endl;
   for (int i=0;i<=2;i++){
         for (int j=0;j<=2;j++) {
             cin>>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<<matrixA[0][0]<<"\t"<<matrixA[0][1]<<"\t"<<matrixA[0][2]<<"\n";
   cout<<matrixA[1][0]<<"\t"<<matrixA[1][1]<<"\t"<<matrixA[1][2]<<"\n";
   cout<<matrixA[2][0]<<"\t"<<matrixA[2][1]<<"\t"<<matrixA[2][2]<<"\n";
   cout<<"and\n\n";
   cout<<matrixB[0][0]<<"\t"<<matrixB[0][1]<<"\t"<<matrixB[0][2]<<"\n";
   cout<<matrixB[1][0]<<"\t"<<matrixB[1][1]<<"\t"<<matrixB[1][2]<<"\n";
   cout<<matrixB[2][0]<<"\t"<<matrixB[2][1]<<"\t"<<matrixB[2][2]<<"\n";
  
  
   //Displays the addition of the matrices
   cout<<"\n\nThe adding of the two matrice is\n\n";
   
   cout<<matrixC[0][0]<<"\t"<<matrixC[0][1]<<"\t"<<matrixC[0][2]<<"\n";
   cout<<matrixC[1][0]<<"\t"<<matrixC[1][1]<<"\t"<<matrixC[1][2]<<"\n";
   cout<<matrixC[2][0]<<"\t"<<matrixC[2][1]<<"\t"<<matrixC[2][2]<<"\n";
  
  //Mulitplying the matrices together
  for (int i=0;i<=2;i++){
         for (int j=0;j<=2;j++) {
             multMatrix[i][j]=0;
             for (int k=0; k<=2;k++){
                 multMatrix[i][j]+=matrixA[i][k]*matrixB[k][j];
                 }
                 }
                 }
                 
    //Displays the Product of the matrices
   cout<<"\n\nThe product of the two matrice is\n\n";
   
   cout<<multMatrix[0][0]<<"\t"<<multMatrix[0][1]<<"\t"<<multMatrix[0][2]<<"\n";
   cout<<multMatrix[1][0]<<"\t"<<multMatrix[1][1]<<"\t"<<multMatrix[1][2]<<"\n";
   cout<<multMatrix[2][0]<<"\t"<<multMatrix[2][1]<<"\t"<<multMatrix[2][2]<<"\n";
               
                 
  
  
   system("PAUSE");
   return EXIT_SUCCESS;
}
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:
a) Dinamically allocate your array.
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.
I have fixed your program.
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
76
77
78
79
80
81
82
83
#include <cmath>
#include <iostream>
#include <windows.h>

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: "<<endl;
   for (int i=0;i<=2;i++){
         for (int j=0;j<=2;j++) {
             cin>>matrixA[i][j];
             }}

   //Enter the info for the second matrix
   cout<<"\n\nEnter the values for Matrix B in 3 by 3: "<<endl;
   for (int i=0;i<=2;i++){
         for (int j=0;j<=2;j++) {
             cin>>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<<matrixA[0][0]<<"\t"<<matrixA[0][1]<<"\t"<<matrixA[0][2]<<"\n";
   cout<<matrixA[1][0]<<"\t"<<matrixA[1][1]<<"\t"<<matrixA[1][2]<<"\n";
   cout<<matrixA[2][0]<<"\t"<<matrixA[2][1]<<"\t"<<matrixA[2][2]<<"\n";
   cout<<"and\n\n";
   cout<<matrixB[0][0]<<"\t"<<matrixB[0][1]<<"\t"<<matrixB[0][2]<<"\n";
   cout<<matrixB[1][0]<<"\t"<<matrixB[1][1]<<"\t"<<matrixB[1][2]<<"\n";
   cout<<matrixB[2][0]<<"\t"<<matrixB[2][1]<<"\t"<<matrixB[2][2]<<"\n";


   //Displays the addition of the matrices
   cout<<"\n\nThe adding of the two matrice is\n\n";

   cout<<matrixC[0][0]<<"\t"<<matrixC[0][1]<<"\t"<<matrixC[0][2]<<"\n";
   cout<<matrixC[1][0]<<"\t"<<matrixC[1][1]<<"\t"<<matrixC[1][2]<<"\n";
   cout<<matrixC[2][0]<<"\t"<<matrixC[2][1]<<"\t"<<matrixC[2][2]<<"\n";

  //Mulitplying the matrices together
  for (int i=0;i<=2;i++){
         for (int j=0;j<=2;j++) {
             multMatrix[i][j]=0;
             for (int k=0; k<=2;k++){
                 multMatrix[i][j]+=matrixA[i][k]*matrixB[k][j];
                 }
                 }
                 }

    //Displays the Product of the matrices
   cout<<"\n\nThe product of the two matrice is\n\n";

   cout<<multMatrix[0][0]<<"\t"<<multMatrix[0][1]<<"\t"<<multMatrix[0][2]<<"\n";
   cout<<multMatrix[1][0]<<"\t"<<multMatrix[1][1]<<"\t"<<multMatrix[1][2]<<"\n";
   cout<<multMatrix[2][0]<<"\t"<<multMatrix[2][1]<<"\t"<<multMatrix[2][2]<<"\n";




   system("PAUSE");
   return EXIT_SUCCESS;
}


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.