Variable Sized Arrays

Sort by

recency

|

1468 Discussions

|

  • + 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;
    

    }

  • + 0 comments

    Here is my code!

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main() {
        int num_arrays, num_queries, num_items, current_item, array_i, item_i;
        vector<int> current_array;
        scanf("%d %d", &num_arrays, &num_queries);
        vector<vector<int>> arrays;
        for (int i = 0; i < num_arrays; i++) {
            current_array = {};
            scanf("%d", &num_items);
            for (int item = 0; item < num_items; item++) {
                scanf("%d", &current_item);
                current_array.push_back(current_item);
            }
            arrays.push_back(current_array);
        }
        for (int i = 0; i < num_queries; i++) {
            scanf("%d %d", &array_i, &item_i);
            printf("%d", arrays[array_i][item_i]);
            if (i != num_queries - 1) {
                printf("\n");
            }
        }
        return 0;
    }