Variable Sized Arrays

Sort by

recency

|

1480 Discussions

|

  • + 0 comments

    // meo meo :))

    include

    using namespace std;

    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 i, j;
        cin >> i >> j;
        cout << arr[i][j] << endl;
    }
    
    return 0;
    

    }

  • + 1 comment

    only revise format and variable name with gpt written by CPP newbie

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main() {
        int n;  // number of arrays
        int q;  // number of queries
        cin >> n >> q;
    
        // store variable-length arrays
        vector<vector<int>> arrays(n);
    
        for (int i = 0; i < n; i++) {
            int len;  // length of the i-th array
            cin >> len;
    
            arrays[i].resize(len);
    
            for (int j = 0; j < len; j++) {
                cin >> arrays[i][j];
            }
        }
    
        // process queries
        for (int k = 0; k < q; k++) {
            int arrayIndex;
            int elementIndex;
            cin >> arrayIndex >> elementIndex;
    
            cout << arrays[arrayIndex][elementIndex] << "\n";
        }
    
        return 0;
    }
    
  • + 0 comments

    include

    include

    include

    include

    include

    using namespace std;

    int main() { int n,q; //n = lenght of main vector q = no. of queries cin >> n >> q;

    vector<vector<int>> VEC(n); //initializing the 2D vector VEC
    for(int i = 0; i < n; i++){ //accessing each elements of the vector VEC
        int lenght; //lenght of each vector
        cin >> lenght;
        vector<int> ele(lenght);
        for(int j = 0; j < lenght; j++){ //writing the elements of each elements of the VEC
            cin >> ele[j];
        }
        VEC[i] = ele;
    }
    
    vector<vector<int>> queries(q); //accomodating the i j format in one vector in the form of vector itself
    for(int s = 0; s < q; s++){
        vector<int> rank(2); //creating individual vector for each query
        for(int i = 0; i < 2; i++){
            cin >> rank[i];
        }
        queries[s] = rank;
    }
    for(int i = 0; i < q; i++){
        cout << VEC[queries[i][0]][queries[i][1]] << endl; //accessing the elements
    
    }
    return 0;
    

    }

  • + 1 comment
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <sstream> 
    
    
    using namespace std;
    
    
    int main() {
        int n{};
        int q{};
        scanf("%d %d", &n , &q);
        std::vector<std::vector<int>> allVecs{};
        
        for (int arrs = 0 ; arrs < n ; arrs++) 
        {
            int tempArrLen{};
            scanf("%d", &tempArrLen);
            std::vector<int> tempArr{};
            int tempVal{};
            
            for (int i = 0; i < tempArrLen; i++) {
                scanf("%d", &tempVal);
                tempArr.push_back(tempVal);
            }
            allVecs.push_back(tempArr);
        }
        std::vector<pair<int,int>> queryVec{};
        std::vector<int> res{};
        
        for (int query = 0 ; query < q ; query++) {
            std::pair<int,int> temp{};
            scanf("%d %d", &temp.first, &temp.second);
            // queryVec.push_back(temp);
            std::vector<int> tempOuter{allVecs[temp.first]};
                
            res.push_back(tempOuter[temp.second]);    
        }
        
        for (auto x : res) {
            std::cout<<x<<std::endl;
        }
        
        return 0;
    }
    
  • + 0 comments
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
        int n,q;
        scanf("%d %d",&n,&q);
        std::vector<std::vector<int>> combinedVector;
        for(int ii = 0;ii<n;ii++)
        {
            int tempArrLen;
            scanf("%d",&tempArrLen);
            std::vector<int> arrTemp;
            int tempVal;
            for(int jj=0;jj<tempArrLen;jj++)
            {
                scanf("%d",&tempVal);
                arrTemp.push_back(tempVal);
            }
            combinedVector.push_back(arrTemp);
        }
        for(int xx=0;xx<q;xx++)
        {
            int arrNum, arrIdx;
            scanf("%d %d",&arrNum,&arrIdx);
            std::vector<int> arrTemp1;
            arrTemp1 = combinedVector.at(arrNum);
            cout << arrTemp1[arrIdx] << std::endl;
        }
        return 0;
    }