Addin two 2D Dynamic Arrays by functions

The program has stopped working and I don't identify the error

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
 #include <iostream>

using namespace std;

int** addMatrices(int** matrix1, int** matrix2, int rows1, int columns1, int rows2, int columns2)
{

    int ** Result ;
    Result = new int*[rows1];
    Result = new int*[rows2];
     for(int i=0;i<rows1;i++)
      {

        Result[i]= new int[columns1];
      }
      for(int i=0;i<rows2;i++)
      {

        Result[i]= new int[columns2];
      }
    for(int i=0;i<rows1;i++)
    {
        for(int j=0;j<columns1;i++)
        {

           Result[i][j] = matrix1[i][j]+matrix2[i][j];
        }

    }


   return Result;
}



int main()
{

    int **Matrix1,**Matrix2 , **matrixResult;
    int Rows1,Cols1,Rows2,Cols2;
    cout<<"Number of Rows of Matrix#1 : ";
    cin>>Rows1;
    cout<<"Number of Columns of Matrix#1 : ";
     cin>>Cols1;
    cout<<"Number of Rows of Matrix#2 : ";
    cin>>Rows2;
    cout<<"Number of Columns of Matrix#2 : ";
     cin>>Cols2;

   //Allocation of 2D Matrices Arrays

     Matrix1= new  int*[Rows1];
     Matrix2= new  int*[Rows2];

        for(int i=0;i<Rows1;i++){

        Matrix1[i]= new int[Cols1];
        }
          for(int i=0;i<Rows2;i++){

        Matrix2[i]= new int[Cols2];
        }

    //Filling 2D Matrix#1 Array

        cout<<"Matrix #1 : "<<endl;

        for(int i=0;i<Rows1;i++)
        {

            for(int j=0;j<Cols1;j++)
              {

                  cin>>Matrix1[i][j];
              }
        }
     //Filling 2D Matrix#2 Array
        cout<<"Matrix #2 : "<<endl;

    for(int i=0;i<Rows2;i++)
        {

            for(int j=0;j<Cols2;j++)
              {

                  cin>>Matrix2[i][j];
              }
        }

  //Adding 2 Matrices
  if(Rows1==Rows2 && Cols1==Cols2)
  {
      for(int i = 0;i<Rows1;i++)
      {
          for(int j=0;j<Cols1;j++)
          {
              matrixResult = addMatrices(Matrix1,Matrix2,Rows1,Cols1,Rows2,Cols2);
              cout<<matrixResult<<" ";
          }
          cout<<endl;
      }
  }
  else
  {
      cout<<"Invalid to add the two matrices"<<endl;
  }




        //Deallocation of 2D Array
        for(int i=0; i<Rows1;i++)
        {
            delete []Matrix1[i];

        }
            delete []Matrix1;
        for(int i=0; i<Rows2;i++)
        {
            delete []Matrix2[i];

        }
            delete []Matrix2;


    return 0;
}
Lines 9,10: Why are you assigning new arrays to the same variable. The first allocation will be lost.
Topic archived. No new replies allowed.