We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
int **array = new int*[sequences];
for (int x = 0; x < sequences; x++) {
array[x] = new int[sequence_width];
for (int y = 0; y < sequence_width; y++) {
cin >> *(array[x] + y);
}
}
You need to allocate an array of pointer to int. Then you need to allocate each array with variable width. Finally you need to read in the int into the array offset by Y ints. The memory is contingous so *(array[x] + y) gets the reference of array[x] (which is a pointer to int) and offsets the memory address y ints. Then we dereference that to insert the int.
BTW this has nothing to do with C++. But it is useful to know. If you were to do C++ you would use
std::vector<std::vector<int> >
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Variable Sized Arrays
You are viewing a single comment's thread. Return to all comments →
You need to allocate an array of pointer to int. Then you need to allocate each array with variable width. Finally you need to read in the int into the array offset by Y ints. The memory is contingous so *(array[x] + y) gets the reference of array[x] (which is a pointer to int) and offsets the memory address y ints. Then we dereference that to insert the int.
BTW this has nothing to do with C++. But it is useful to know. If you were to do C++ you would use