Is it possible to convert 2d vector into 1d? If it is, how should i do that? [solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is it possible to convert 2d vector into 1d? If it is, how should i do that? [solved]

Currently doing my summer uni practice, can't seem to figure that one out. My task is to 1. create a 2d array, size n*n (n determined by user, that's why i resorted to using vectors). 2. users should be able to input array elements themselves, or let it happen randomly. 3. form a 1d array from a 2d one (that's where I'm currently stuck) Is it even achievable with the code i have currently? Or should I start all over again? https://code.sololearn.com/cYk5aAk8XE6V/?ref=app

20th Jul 2022, 12:21 AM
Rostyslav Shamekhin
4 Answers
+ 4
Rostislav The example shows a possible way to convert 2d into 1d and vice versa 1d into 2d: https://code.sololearn.com/cOb7EIB0CIm9/?ref=app
20th Jul 2022, 9:16 AM
JaScript
JaScript - avatar
+ 3
How should this conversion be? What's the logic to go from the 2d to the 1d array?
20th Jul 2022, 3:32 AM
Emerson Prado
Emerson Prado - avatar
0
The only other piece of info i have says that the 1st element of 1d array should be the one, that's on the bottom left in a 2d array. Then it goes straight up, then right. Basically forming a spiral like the one in the center here: https://images.app.goo.gl/QTKHnx8cVy69obWz8
20th Jul 2022, 4:22 AM
Rostyslav Shamekhin
0
Rostislav So it seems somewhat easy. Just sweep the 2d array in the way you describe, then append each item to the 1d array. It takes some thinking to sweep like a spiral, but it's not that hard. For the brave, maybe you can append each side of the 2d array at once. But let's walk one step at a time.
20th Jul 2022, 5:54 PM
Emerson Prado
Emerson Prado - avatar