Variable Sized Arrays

  • + 14 comments
    int n,q;
    cin >> n >> q;
    vector< vector<int> > a(n);
    
    // input each array
    for (int i=0;i<n;i++) {
        int k;
        cin >> k;
    
        for (int j=0;j<k;j++) {
            int data;
            cin >> data;
            a[i].push_back(data);
        }
    }
    
    // do the queries
    for (int i=0;i<q;i++) {
        int x,y;
        cin >> x >> y;
        cout << a[x][y] << endl;
    }
    
    • + 0 comments

      Thanks a lot rwan7727, i was wondering how to input each array. It helped me.

    • + 1 comment

      Why you don't take the int k & int data out of the for loop?

      • + 0 comments

        Yes, you can take 'int k' and 'int data' out of the for loops and declare them outside. This is for scoping purpose (that is, to be 'defensive' in programming). This is just in case there is a 'k' somewhere else in the code too. The 'k' or 'data' inside the for loops will be different from those elsewhere.

    • + 1 comment

      why should we use "a[i].push_back(data)"?Is there anything wrong in using "a[i,j]" instead of that?I didn't get the output.

    • + 1 comment

      can u convert this in c.because i dont know c++

      • + 3 comments

        Here u can find C code

        #include<stdio.h>
        #include<stdlib.h>
        int main() {
            int n,q,m,l,k;
            scanf("%d%d",&n,&q);
            int *arr[n];
            for(int i = 0 ; i < n;i++){
                scanf("%d",&m);
                arr[i] = (int *)malloc(m * sizeof(int));
                for(int j = 0 ; j < m ;j++){
                     scanf("%d",&arr[i][j]);
                }
            }
            while(q--){
                scanf("%d%d",&l,&k);
                printf("%d\n",arr[l][k]);
            } 
            return 0;
        }
        
        • + 0 comments

          segmentation fault

        • + 0 comments

          Can you explain it please?

        • + 0 comments

          Thank you bro! it's perfect solution...

    • + 1 comment

      Can anyone explain line 3 ?

      vector< vector > a(n)

      • + 1 comment

        He is declaring a matrix of vector (in this case, vector filled with vectors), a is the var name and (n) is a resize of the "parent" vectors, which is, the one who contains other vectors.

        • + 1 comment

          which languague is it?

          • + 0 comments

            C++ it is..

    • + 1 comment

      Filling of vector also can be done in the following way:

      a[i].reserve(k); // make room for k elements
          for (int j=0;j<k;j++) {
              cin >> a[i][j]; // and reading them
          }
      
      • + 0 comments

        That reserve() was exectly what I was lacking. Thanks!

    • + 0 comments

      No one use dynamic array?

    • + 0 comments

      thankyou soo much; my frnd it s really halpfull for me :)

    • + 0 comments

      This is very close to my implementation except i loaded the data all at once into a temporary vector created in the loop and pushed the data vector into the top level vector directly.

    • + 0 comments

      less lines and simple. Good.

    • + 0 comments

      vector< vector > a(n);

      can u explain this line?
      
    • + 0 comments

      you reallly made it simple by seperating the data from the k value, this one really helped me thanks now I understand :)

    • + 0 comments

      helped alot for taking the input

    • + 0 comments

      I'm still failing 9/11 when they're doing vectors of size 10,000 and 100,000.