-2
    #include <iostream>
    using namespace std;
    int main()
    {
         system("cls");
         int m,n;
         int ar[m][n];
         cout<<"Enter the rows: "<<endl;
         cin>>m;
         cout<<"Enter the columns: "<<endl;
    cin>>n;
    cout<<m<<"x"<<n<<" Matrix"<<endl;
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            cin>>ar[i][j];
        }
        cout<<endl;
    }
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            cout<<ar[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

Output:

Enter the rows:

2

Enter the columns:

2

2x2 Matrix

1 2

3 4

MAtrix:

3 4

3 4

It is only displaying the second row two times. i typed the program from again 2-3 times but no change how to overcome this error?

Please help me?

rioV8
  • 24,506
  • 3
  • 32
  • 49
  • `int ar[m][n];` is NOT standard C++, but a compiler extention. Preferably use `std::vector`. – JHBonarius Nov 24 '20 at 08:49
  • 1
    Here `int ar[m][n];` : the values of `m` and `n` are still unknown – Damien Nov 24 '20 at 08:49
  • Hi, like the above comments said, we have to decide the size of an array before source code being compiled. Either you declare `ar` with a maximum size(e.g. `int ar[100][100];`) or using `std::vector`, which will dynamically locate memories during run time. – Ju-chien Chien Nov 24 '20 at 09:00
  • turn on warnings and take them serious. Your compiler can help you a lot if you just let it – 463035818_is_not_an_ai Nov 24 '20 at 09:30

2 Answers2

1

As already mentioned in the comments, the problem lies with the line int ar[m][n];. Even if your compiler lets you do this, m and n have not been initialised at that point. You can check How do you dynamically allocate a matrix? for a discussion on how to allocate such a matrix dynamically. Basically, if we disegard some nifty optimisations, you have two straightforward options:

  1. The old pre-standardisation C++ way of allocating a double pointer (similar to C):

    [...]
    
    int** ar = nullptr;  // Replacing your int ar[m][n];
    
    [...]
    cout<<m<<"x"<<n<<" Matrix"<<endl;
    
    ar = new int*[m];
    for (int i = 0; i < m; ++i)
        ar[i] = new int[n];
    
    [...]
    

    When you're done using ar, you have to delete the memory:

    for (int i = 0; i < m; ++i)
        delete[] ar[i];
    delete[] ar;
    
  2. In order to avoid manually handling the memory, you can use two nested std::vectors, as follows:

     [...]
    
     vector<vector<int>> ar; // Replacing your int ar[m][n];
    
     [...]
     cout<<m<<"x"<<n<<" Matrix"<<endl;
    
     ar.resize(m);
     for (auto& row : ar)
         row.resize(n, 0); // Initialising each row with n zeros
    
     [...]
    

And then you do the cin's. With this code you don't have to release the memory as the std::vector class will do that for you in its destructor.

  • the "traditional" way to use a dynamic array in C++ is and was always a `std::vector`. "C/C++" is a misnomer, there is no language called like this, C idioms are different than C++ idioms. Using idioms that are ok in C and C++ is neither idiomatic C nor idiomatic C++ – 463035818_is_not_an_ai Nov 24 '20 at 09:45
  • @idclev463035818, not before the advent of the standard library. When Bjarne Stroustrup wrote The C++ Programming Language in 1985, classes like std::vector didn't exist. They were introduced by Alexander Stepanov's standard template library in the mid 90's. Before C++98, the common idioms in C++ for dynamic memory were similar to those of C (replacing mallocs with news, and frees with deletes). – Ángel José Riesgo Nov 24 '20 at 10:37
  • ok, i would not call it "common idioms" before it was standardized. C with classes was a whole different language than the C++ that was standardized. Nevermind, I now understand why you wrote it like you did and anything beyond that is just splitting hairs and opinions. – 463035818_is_not_an_ai Nov 24 '20 at 10:40
  • 1
    I suppose what we mean by "traditional" depends on how old we are. As I approach my 50th birthday, I remember the time when every C++ library came with its own "CString" and "CList" classes. Anyway, I've edited my post to use "old/pre-standardisation" instead of "traditional". – Ángel José Riesgo Nov 24 '20 at 11:29
0

Just move the declaration of the matrix after you get the values of 'm' and 'n'. Note, this will create the matrix on the stack, so for large matrices this is not a good idea. You can allocate memory dynamically as suggested in other posts.

    int main()
   {
    system("cls");
    int m, n;
    
    cout << "Enter the rows: " << endl;
    cin >> m;
    cout << "Enter the columns: " << endl;
    cin >> n;

    int ar[m][n]; // move this here ... 
shr
  • 875
  • 10
  • 20