Variable Sized Arrays

Sort by

recency

|

1469 Discussions

|

  • + 0 comments
    #include <iostream>
    #include <vector>
    using namespace std;
    //matrix problem  :X
    int main() {
        int n, q;
        cin >> n >> q;
        vector<vector<int>> arr(n);
    
        for (int i = 0; i < n; i++) {
            int k; cin >> k;
            arr[i].resize(k);
            for (int j = 0; j < k; j++) cin >> arr[i][j];
        }
    
        while (q--) {
            int x, y; cin >> x >> y;
            cout << arr[x][y] << "\n";
        }
    }
    
  • + 0 comments

    My solution:

    include

    using namespace std;

    int main(){ ios::sync_with_stdio(0); cin.tie(0);

    int n, q;
    cin >> n >> q;
    vector<vector<int>> no(n);
    for(int i = 0; i<n; i++){
        int k;
        cin >> k;
        no[i].resize(k);
        for(int j = 0; j<k; j++){
            cin >> no[i][j];
        }
    }
    while(q-->0){
        int x, y;
        cin >> x >> y;
        cout << no[x][y] << '\n';
    }
    
    return 0;
    

    }

    I am not that familiar with pointers actually, so I used vectors instead.

    return 0;
    

    }

  • + 0 comments

    int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n, q; cin >> n >> q; // number of arrays and number of queries

    vector<vector<int>> arr(n);
    
    // input arrays
    for (int i = 0; i < n; i++) {
        int k;
        cin >> k;   // size of this array
        arr[i].resize(k);
        for (int j = 0; j < k; j++) {
            cin >> arr[i][j];
        }
    }
    
    // answer queries
    for (int i = 0; i < q; i++) {
        int M, N;
        cin >> M >> N;
        cout << arr[M][N] << endl;
    }
    return 0;
    

    }

  • + 0 comments
    
    
    cin >> n >> q;
    int** arr = new int*[n];
    for(unsigned int h = 0; h < n; ++h)
    {
        cin >> k;
        *(arr + h) = new int[k];
        for(unsigned m = 0; m < k; ++m) {cin >> *(*(arr + h)+ m);}
    }
    for(unsigned int h = 0; h < q; h++){cin >>i >>j; cout << *(*(arr + i)+ j) << endl;}
    
  • + 0 comments

    Here is my code

    include

    include

    include

    include

    include

    using namespace std;

    int main() {

    int n ;
    cin >> n;
    int q ;
    cin >> q;
    vector<vector<int>> arr(n);  
    
    for (int i = 0; i < n; i++) {
        int k ;
        cin >> k;
    
        arr[i].resize(k);
    
        for (int j = 0; j < k; j++) {
            cin >> arr[i][j];
        }
    }
    
    for (int j = 0 ; j < q ; j++) {
        int x, y ;
        cin >> x >> y;
        cout << arr[x][y] << endl;
    }
    return 0;
    

    }