Variable Sized Arrays

Sort by

recency

|

1432 Discussions

|

  • + 0 comments

    int main() {

    int n,q;
    cin >> n >> q;
    vector<vector<int>>v(n);
    for(int i = 0; i < n;i++){
        int nums;
        cin >> nums;
        v[i].resize(nums);
        for(int j = 0; j < nums;j++){
            int x;
            cin >> x;
            v[i][j] = x;
        }     
    }
    while(q--){
        int a,b;
        cin >> a >> b;
        cout << v[a][b]<<endl;
    } 
    return 0;
    

    }

  • + 1 comment

    For anyone with doubts about the problem I will try to explain it (then I will attach a video to youtube if it's not enough).

    So it ask us for a 2 dimensional array, twice, in other words you will need to declare one 2d array and call it twice, "a" means how many arrays will be in the 2d array, and "q" is basically asking "Hey I will ask (q) number of times about the array, different question each time".

    The first line of input (2 2) means ; it will be a 2d array that contains “2” different arrays, and the other “2” means how many questions it's going to ask.

    The next line of inputs (3 1 5 4) and (5 1 2 8 9 3) is a bit more complicated. In the first line (3 1 5 4) Means two things, the first number (3 …) means how large will the array be, meaning that the next numbers are going to be the content of the array (... 1 5 4), in other words “3” is the size of the array, “1 5 4” is what is inside. It's the same with the second line, (5 1 2 8 9 3), “5” is the size of the array, and “... 1 2 8 9 3” is the content.

    For the last two lines of input (0 1) and (1 3) are coordinates, basically, in c++ a 2d array looks like “ int array [x] [y] “, the input (0 1) replaces “x” and “y” inside the array, so, the query is “ int array [0] [1] “, and in programming, the computer counts starting at 0, so “0” directs you to the 1st array of the 2d array (1 5 4), and “1” is the 2nd value of that array, so “ int array [0] [1] “ outputs “5”, the 2nd number of the 1st array. For the last two lines of input (0 1) and (1 3) are coordinates, basically, in c++ a 2d array looks like “ int array [x] [y] “, the input (0 1) replaces “x” and “y” inside the array, so, the query is “ int array [0] [1] “, and in programming, the computer counts starting at 0, so “0” directs you to the 1st array of the 2d array (1 5 4), and “1” is the 2nd value of that array, so “ int array [0] [1] “ outputs “5”, the 2nd number of the 1st array.

    Input (1 3) means “ array [1] [3] “, it outputs the 4th (3) number of the 2nd (1) array; if you are asking ‘What happened to the 3 and the 5 in the second and third line of input’ those just say how big the mentioned subsidiary array is.

    The youtube video: https://www.youtube.com/watch?v=IMsDWK0twOY

  • + 1 comment

    i dont understand the problem, even i spend a lot of time but didnt understand the problem

  • + 0 comments

    My C++ code without vector

    #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 rows(0),q(0);
        cin >>rows>>q;
        
        int** array = new int*[rows];
        for(int i = 0;i < rows;i++){
            int k(0);
            cin>>k;
            array[i] = new int[k];
            for(int j = 0;j < k;j++){
                cin >> array[i][j];
            }
        }
        
        int rowA = 0,colA = 0;
        
        for(int i = 0;i < q;i++){
            cin >> rowA>>colA;
            cout << array[rowA][colA]<<endl;
        }
        
        delete[]array;
        
        return 0;
    }
    
  • + 0 comments

    int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n, q , k , l , pl , pt; cin >> n >> q ; vector> num(n);

    for (int i = 0 ; i <= n - 1 ; i++ ){
        cin >> k ;
        num[i].resize(k);
        for (int j = 0 ; j < k ; j++ )
        {
            cin >> num[i][j] ;
        }
    }
    
    for (int i = 0 ; i < q ; i++){
        cin >> pl >> pt ;
        cout << num[pl][pt] << "\n";
    }
    
    return 0;
    

    }