-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariable Sized Arrays.cpp
47 lines (36 loc) · 989 Bytes
/
Variable Sized Arrays.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;
int main() {
// get length of 2D array and number of queries
int n, q;
cin >> n >> q;
// create 2D array
int** a = new int*[n]();
// fill 2D array with 1D subarrays
for (int i = 0; i < n; i++) {
// get the length of the 1D array at a[i]
int k;
cin >> k;
// create the 1D subarray with the given length
int* i_arr = new int[k]();
// fill the subarray with k values
for (int j = 0; j < k; j++) {
cin >> i_arr[j];
}
// store the subarray in a
a[i] = i_arr;
}
// run queries on a
for (int q_num = 0; q_num < q; q_num++) {
// get i, j as the 'query' to get a value from a
int i, j;
cin >> i >> j;
cout << a[i][j] << endl;
}
// delete 2D array (each subarray must be deleted)
for (int i = 0; i < n; i++) {
delete[] a[i];
}
delete[] a;
return 0;
}