Variable Sized Arrays

  • [deleted]
    + 2 comments

    Hi, I'm fairly new to coding and don't really understand the concept of allocating memory. I just have a quick question - I was wondering why we had to allocate memory for the queries? My code looks similar to yours, except I only have 1 array:

    #include <iostream> 
    using namespace std;
    
    int main() {
      int **a;         // create dynamic array
        int n, q ;     // number of rows, number of queries
        cin >> n >> q;
        
        a = new int* [n];
        for (int row = 0; row < n+1; row++) {            //go thru each row to enter variable length of col                                                       
            int k; cin >> k;          // # of columns for specific row
            a[row] = new int[k];
            
            for (int col = 0; col < k+1; col++)  {      // fill each column for specific row
                cin >> a[row] [col];
                
            }
            
        }
        
        
        for (int count = 0; count < q+1; count++ )  {   //fulfill queries
            int row; int col;                           
            cin >> row >> col;
            cout << a[row] [col];
            
        }
    
        
    
    return 0;
    }