Copying a section of a 2 dimensional array using for loops? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Copying a section of a 2 dimensional array using for loops?

Hi, I'm having trouble for nights of 'cloning' or 'copying' a specific 'section' of a 2 dimensional array. Suppose I have this 2 dimensional array: myArr = [ [ 0, 0, 0, 1, 0, 0, 1 ] [ 1, 0, 0, 1, 0, 1, 0 ] [ 0, 0, 0, 1, 0, 0, 0] [ 1, 0, 0, 1, 1, 1, 0 ] ] Now I want to 'clone' the 'section' of the myArr, for example, from item myArr[1, 1] to item myArr[3, 5] in the variable newArr. Now the newArr should be: newArr = [ [ 0, 0, 1, 0, 1 ], [ 0, 0, 1, 0, 0 ], [ 0, 0, 1, 1, 1 ] ] Can someone help me or how can I do this or what possible procedures can I do by using for loops? PS: I don't mind any programming language as far as it will use for loops. and also sorrryyyy for my bad english xD.

3rd May 2020, 5:51 AM
Nootnoot
Nootnoot - avatar
6 Answers
+ 5
SPICEAPPLE 🍎 { active: 0 } Something crazy is happening with your code, haha :)) Here is the corrected code in javascript : https://code.sololearn.com/WMht9Mr5CmPj/?ref=app
3rd May 2020, 8:45 AM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 4
SPICEAPPLE, when asking for a code you should first do a try and link us the result here. Thanks!
3rd May 2020, 7:19 AM
Lothar
Lothar - avatar
+ 3
You need two loops. One for rows(outer), other for columns(inner). myArr[0],myArr[2],myArr[1] are rows. myArr[0][0],myArr[0][1],myArr[0][2] are elements in the same row but different columns. Let me take your example: Your outer loop should go from 1 to 3 and the inner from 1 to 5. Remember that array index start is 0 so you'll probably need to substract 1. Then in the inner loop the logic should be: newArr[outer][inner] = myArr[outer][inner]. (Outer and inner are here the loops increment variables) Let me see what you have so far.
3rd May 2020, 6:09 AM
Kevin ★
+ 2
Here are the steps you may need to follow : 1) Write a function that takes the array, starting point and the end point (Let say the starting points are x1, y1 and the end points are x2, y2 2) Make an empty array to store the result (let say newArr) 3) We need two for loops. The first one for rows and the second one for columns; 4) The first for loop runs from y1 to y2 (Rows) 5) Now make another empty array to store the cloned item (let say sudo) 6) The second for loop runs from x1 to x2 (Columns) 7) Inside the second for loop push the to-be-cloned item into the sudo array 8) Now push the sudo array to the newArr 9) Return the newArr This should do your work :))
3rd May 2020, 6:24 AM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 2
Thank you everyone, here is the spaghetti code. I really think there's something missing... https://code.sololearn.com/cF17ALB1J0kr/#cs
3rd May 2020, 8:01 AM
Nootnoot
Nootnoot - avatar
+ 2
I'm so dumb I overthink everything on it. Thanks you, Arb Rahim Badsa. I can now finally sleep in peace xD
3rd May 2020, 8:55 AM
Nootnoot
Nootnoot - avatar