Fill 2 matrices arrays

What is wrong with That code.The Program has Stopped Working !

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
  #include <iostream>
#include<string.h>

using namespace std;



int main()
{

    int **Matrix1,**Matrix2;
    int Rows,Cols;
    cout<<"Number of Rows : ";
    cin>>Rows;
    cout<<"Number of Columns : ";
    cin>>Cols;

    Matrix1= new int*[Rows];
    Matrix2= new int*[Rows];

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

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

    for(int i=0;i<Rows;i++){
        for(int j=0;j<Cols;j++)
        cin>>Matrix1[i][j];}

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

         for(int i=0;i<Rows;i++){
        for(int j=0;j<Cols;j++)
        cin>>Matrix2[i][j];}



        for(int i=0; i<Rows;i++)
        {
            delete []Matrix1[i];

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

        }
            delete []Matrix2;



    return 0;
}
Last edited on
Matrix1 and Matrix2 are two dimensional arrays. You initialize the rows in both of them but forgot to initialize the columns in Matrix2.

1
2
3
4
5
6
7
8
9
for(int i=0;i<Rows;i++) //You added this first one
{
      Matrix1[i]= new int[Cols];
}

 for(int i=0;i<Rows;i++) //But you forgot this one
{
      Matrix2[i]= new int[Cols];
}


It helps when brackets are always used with loops even when there is only one statement being executed. Helps things be more readable.

Hope this helps!
Thank You very much...This is very helpful to me
Topic archived. No new replies allowed.