C++ static array 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
2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3
Sample Output
5
9
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(“%dn”, arr[a][b]);
}
return 0;
}
We've got everything to become your favourite writing service
Money back guarantee
Your money is safe. Even if we fail to satisfy your expectations, you can always request a refund and get your money back.
Confidentiality
We don’t share your private information with anyone. What happens on our website stays on our website.
Our service is legit
We provide you with a sample paper on the topic you need, and this kind of academic assistance is perfectly legitimate.
Get a plagiarism-free paper
We check every paper with our plagiarism-detection software, so you get a unique paper written for your particular purposes.
We can help with urgent tasks
Need a paper tomorrow? We can write it even while you’re sleeping. Place an order now and get your paper in 8 hours.
Pay a fair price
Our prices depend on urgency. If you want a cheap essay, place your order in advance. Our prices start from $11 per page.