Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

its due in 3 hours olve the Variable Sized Array problem in hacker rank using St

ID: 3755965 • Letter: I

Question

its due in 3 hours

olve the Variable Sized Array problem in hacker rank using Static instead of Dynamic Arrays. Of course, you should have already solved with Dynamic, but how can you get it to work with Static Arrays. Here is the code which I developed to start you out. You need to complete the input/output segment. I used a 1 Dimensional array to hold all the data and another 1 Dimensional array to tell me where each row starts. See if you can figure out how to do this.checl the code down how can you get it to work with Static Arrays

This has direct application for unequal structures written to a binary file. THE CODE FOR DYNAMIC ARRAY IS DOWN AT THE END NEED IT CHANGE FROM DYNAMIC ARRAY TO STATIC ARRAY

#include
using namespace std;

int main() {
//Declare variables and arrays
const int MAX=500000;
const int ROW=300000;
int a[MAX]={};//1-D Array
int rc[ROW]={};//1-D Array where each row will start in the 1-D array above
int n,q,cnt,k;
//Read in the data
cin>>n>>q;//#Rows and #Queries
//Read in the entire array
cnt=0;//Start the array count at 0

orginal question

Consider an -element array, , where each index  in the array contains a reference to an array of  integers (where the value of  varies from array to array). See the Explanation section below for a diagram.

Given , you must answer  queries. Each query is in the format i j, where  denotes an index in array  and  denotes an index in the array located at . For each query, find and print the value of element  in the array at location  on a new line.

Click here to know more about how to create variable sized arrays in C++.

Input Format

The first line contains two space-separated integers denoting the respective values of  (the number of variable-length arrays) and  (the number of queries).
Each line  of the  subsequent lines contains a space-separated sequence in the format k a[i]0 a[i]1 … a[i]k-1 describing the -element array located at .
Each of the  subsequent lines contains two space-separated integers describing the respective values of  (an index in array ) and  (an index in the array referenced by ) for a query.

Constraints

All indices in this challenge are zero-based.

All the given numbers are non negative and are not greater than

Output Format

For each pair of  and  values (i.e., for each query), print a single integer denoting the element located at index  of the array referenced by . There should be a total of  lines of output.

Sample Input

Sample Output

CODE

#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <cassert>

#include <algorithm>

using namespace std;

int main() {

    int *arr[100004], siz[100004];

    int n,q;

    int tot = 0;

    scanf("%d %d", &n, &q);

    assert(n >= 1 && n <= 100000 && q >= 1 && q <= 100000);

    for(int i = 0 ; i< n ; i++)

    {

        int num;

        scanf("%d", &num);

        assert(num >= 1 && num <= 300000);

        tot += num;

        siz[i] = num;

        arr[i] = (int*)malloc(num*sizeof(int));

        for(int j = 0 ; j < num; j++)

        {

            scanf("%d", &arr[i][j]);

            assert(arr[i][j] >= 0 && arr[i][j] <= 1000000);

        }

    }

    assert(tot >= 1 && tot <= 300000 && tot >= n);

    while(q--)

    {

        int a,b;

        scanf("%d %d", &a, &b);

        assert(a >= 0 && a < n && b >= 0 && b < siz[a]);

        printf("%d ", arr[a][b]);

    }

    return 0;

}

Explanation / Answer

Using STATIC ARRAY

#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <cassert>

#include <algorithm>

using namespace std;

int main() {
int n, q ,a, b, num, x;
int *arr[300000], src[500000], *ptr;
ptr = src;
scanf("%d %d", &n, &q);
for(a=0;a<n;a++){
scanf("%d", &num);
arr[a] = ptr;
ptr = arr[a]+num;
for(x=0;x<num;x++)  
scanf("%d", &arr[a][x]);
}
while(q--!=0){
scanf("%d %d", &a, &b);
printf("%d ", arr[a][b]);
}

return 0;
}











Thanks, PLEASE UPVOTE