Variable Sized Arrays

  • + 0 comments

    i tried to explain as much as i can, hope this helps.

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main() {
        /*
        defining the variables
        n : size of main array
        q : number of queries
        k : size of a given array in the main one
        */
        int n,q,k;
        cin >> n >> q;
        /*
        creating the main array of n elements, which is
        an array of pointers to the others arrays
        (can't create a 2 dimentional array since the arrays within "a" are not the same size)
        */
        int** a = new int*[n];
        //for each element in array a
        for(int i=0;i<n;i++){
            cin >> k;
            //creating a new array inside of the array a
            a[i] = new int[k];
            //filling  the array with values
            for(int j=0;j<k;j++){
                cin >> a[i][j];
            }
        }
        // executing the queries
        for(int l=0;l<q;l++){
            int i,j;
            // putting the values in the variables i and j
            cin >> i >> j;
            cout << a[i][j] << "\n";
        }
        
        
        
        return 0;
    }