1

Looking to merge two different 2D arrays of different row sizes into one long array:

 int 2dArray1[][] = {
 {1, 2, 3},
 {4, 5, 6}};
 int 2dArray2[][] = {
 {10, 20},
 {30, 40}};
 int 1dLongArray[10];

This is where I need help in order to have the values of 1dLongarray be arranged as follows:

1, 2, 3, 10, 20, 4, 5, 6, 30, 40

How would I achieve this? Thanks in advance.

asked Nov 29, 2020 at 23:18
3
  • step by step, how did you do it on paper? ... the program would do it the same way Commented Nov 29, 2020 at 23:37
  • The only way that I could think to make it work on paper would be to use memcpy for a one dimensional arrays and then create a two dimensional array out of it somehow. I'm not really sure how to do that, and I'm fairly new to coding and don't have a tutor or instructor to help me. I always try to do my best to find an answer myself before asking online but this one had me stumped. Commented Nov 30, 2020 at 1:47
  • when i say on paper, i mean, how would you do it if you knew nothing about programming ... what sequence of steps would you follow? ... i also do not mean write down first three numbers ... i mean baby steps, like look at array1, row1, position1. copy content to first empty position at destination Commented Nov 30, 2020 at 1:56

1 Answer 1

0

Consider using loops of length specific to the desired number of transactions. For example, using this pseudo code create a sketch which follows this pattern:

initialize i to zero
loop 2 times for each row using the variable x
 loop 3 times for each element in first array using the variable y
 long array [i] = first array[x][y] 
 increment i
 loop 2 times for each element in a second array using the variable z
 long array [i] = second array[x][z]
 increment i
answered Nov 29, 2020 at 23:36
5
  • Being a brand new account I can't upvote your comment yet, so I'm leaving this comment to thank you! Commented Nov 30, 2020 at 1:42
  • If you can derive a working solution from this answer, you should be able to accept this answer as a solution to your question. Commented Nov 30, 2020 at 1:49
  • Thanks @Hitachii for making the answer as correct. If the answer works great! If not, please leave a comment and I'll edit the answer to improve it. Commented Nov 30, 2020 at 14:54
  • Sure. If someone were to plug the guideline for the formula that you posted, into the problem that I posted, for "long array [i] = first array [y] [x]" and "long array [i] = second array [z][x], they would have to switch the placement of x and z/y in their respective line to look like "long array [i] = first array [x][y]" to follow the original sequence of the array. Do I post my working code? This shows how green I am to this platform. Commented Nov 30, 2020 at 21:10
  • You are correct. The 1st argument is the row and the second is the column. I'll change my answer. Commented Dec 1, 2020 at 5:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.