Variable Sized Arrays

Sort by

recency

|

1426 Discussions

|

  • + 0 comments

    `int main(){

    int n,q,k,i,j,b,pb;
    cin>>n>>q;
    vector<vector<int>> num(n);
    
    for(b = 0;b<n;b++) {
     cin>>k;
     num[b].resize(k);
    
     for(int a = 0; a<k;a++){
         cin>>num[b][a];
    
    }
    }
    
    
    for(int c = 0;c<q;c++){
    cin>>i>>j;`` 
    
    int TO = (i<n && j< num[i].size())?num[i][j]:0;
    
    cout<<TO<<endl;
    
    }
    
    return 0;
    

    }

  • + 1 comment

    Please help me understand what the problem is

  • + 0 comments

    int main() {

    int n, q, size, value, which, index;
    
    cin >> n >> q;
    vector<vector<int>> vec1; //initializing a 2d vector
    for (int i = 0; i < n; ++i) {
        cin >> size;
        vector<int> vec2;
        for (int j = 0; j < size; ++j) {
            cin >> value;
            vec2.push_back(value);
        }
        vec1.push_back(vec2);
    }
    for (int k = 0; k < q; ++k) {
        cin >> which >> index;
        cout << vec1[which][index] << endl;
    }  
    return 0;
    

    }

  • + 0 comments

    The question was tricky and a bit hard to understand it was difficult to understand the k maybe bcz i am just a beginner int N, Q; cin >> N >> Q;

    int* arr[N];
    
    for(int i = 0; i < N; i++ ){
        int k;
        cin >> k;
        arr[i] = new int[k];
        for(int j = 0; j < k; j++){
            cin >> arr[i][j];
        }
    }
    for(int i = 0; i < Q; i++){
        int a, b;
        cin >> a >> b;
        cout << arr[a][b] << endl;
    }
    
  • + 0 comments

    Can someone tell me what's wrong with my code please?

    include

    include

    include

    using namespace std;

    int main() { string str1; int N, q, k_size;

    getline(cin, str1);
    N = str1[0] - '0';
    q = str1[2]-'0';
    vector<vector<int>> a(N);
    getline(cin, str1);
    
    cout << N << " " << q << endl;
    for (int i = 0; i < N; i++) {
        getline(cin, str1);
        k_size = str1[0] - '0';
        vector<int> k(k_size);
    
        for (int j = 0; j < k_size; j++)
            k[j] = str1[j + 2];
    
    }
    for (int i = 0; i < 2; i++) {
        getline(cin, str1);
        cout << a[str1[0] - '0'][str1[2] - '0'] << endl;
    }
    
    
    
    
    return 0;
    

    }