My 1-D array is importing correctly and is displayed correctly, my logic also works when I do it by hand so I'm not sure what is wrong. When I copy each value in the 1-D array into the 2-D it is doing an odd pattern of copying as well as putting the wrong values in.
This is the 1-D array:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
This is the following output:
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
row: 0 col: 0
0
In loop... [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
row: 0 col: 1
1
In loop... [[2, 0, 0, 0], [2, 0, 0, 0], [2, 0, 0, 0], [2, 0, 0, 0]]
row: 0 col: 2
2
In loop... [[3, 0, 0, 0], [3, 0, 0, 0], [3, 0, 0, 0], [3, 0, 0, 0]]
row: 0 col: 3
3
In loop... [[4, 0, 0, 0], [4, 0, 0, 0], [4, 0, 0, 0], [4, 0, 0, 0]]
row: 1 col: 0
4
In loop... [[4, 5, 0, 0], [4, 5, 0, 0], [4, 5, 0, 0], [4, 5, 0, 0]]
row: 1 col: 1
5
In loop... [[4, 6, 0, 0], [4, 6, 0, 0], [4, 6, 0, 0], [4, 6, 0, 0]]
row: 1 col: 2
6
In loop... [[4, 7, 0, 0], [4, 7, 0, 0], [4, 7, 0, 0], [4, 7, 0, 0]]
row: 1 col: 3
7
In loop... [[4, 8, 0, 0], [4, 8, 0, 0], [4, 8, 0, 0], [4, 8, 0, 0]]
row: 2 col: 0
8
In loop... [[4, 8, 9, 0], [4, 8, 9, 0], [4, 8, 9, 0], [4, 8, 9, 0]]
row: 2 col: 1
9
In loop... [[4, 8, 10, 0], [4, 8, 10, 0], [4, 8, 10, 0], [4, 8, 10, 0]]
row: 2 col: 2
10
In loop... [[4, 8, 11, 0], [4, 8, 11, 0], [4, 8, 11, 0], [4, 8, 11, 0]]
row: 2 col: 3
11
In loop... [[4, 8, 12, 0], [4, 8, 12, 0], [4, 8, 12, 0], [4, 8, 12, 0]]
row: 3 col: 0
12
In loop... [[4, 8, 12, 13], [4, 8, 12, 13], [4, 8, 12, 13], [4, 8, 12, 13]]
row: 3 col: 1
13
In loop... [[4, 8, 12, 14], [4, 8, 12, 14], [4, 8, 12, 14], [4, 8, 12, 14]]
row: 3 col: 2
14
In loop... [[4, 8, 12, 15], [4, 8, 12, 15], [4, 8, 12, 15], [4, 8, 12, 15]]
row: 3 col: 3
15
In loop... [[4, 8, 12, 16], [4, 8, 12, 16], [4, 8, 12, 16], [4, 8, 12, 16]]
Before return... [[4, 8, 12, 16], [4, 8, 12, 16], [4, 8, 12, 16], [4, 8, 12, 16]]
Here is the function code:
def makeTwoArr(array, height, width):
print(array)
newArray=[]
line=[0]*width
for i in range(height):
newArray.append(line)
location=0
print(newArray)
for row in range(height):
for col in range(width):
print("row: ",row," col: ",col);
print(location)
## #print(array[location])
newArray[col][row]=array[location]
## print(newArray)
location+=1
print("In loop...",newArray)
print("Before return...",newArray)
return newArray
Both me and my computer science professor can't figure out why the values are wrong, or why its filling certain spots at the wrong iteration.
colandrowinnewArray[col][row]=array[location]. I think this should work.