C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Value 1: This is the X coordinate. This can be a number between 0 and 9 inclusive.
Value 2: This is the Y coordinate. When accessing the array, we multiply this by the array's X dimension, which is 10 here.
Value 3: This is the value we want to set in the 2D array. The integer that exists is printed to the console before it is changed.
Python program that uses 2D, flattened array
from array import array
# Create an int array.
integers = array("i")
# Add 100 elements for a 10 by 10 array.
# ... We can address each element by a single index.
for i in range(100):
integers.append(0)
while(True):
print("2D array has dimensions 10x10")
print("Type 3 values:")
print(" X, Y, and value to set")
print(" Current value will be printed")
# Get input.
values = input()
# Split on comma.
separated = values.split(",")
# Parse arguments into numbers.
value1 = int(separated[0])
value2 = int(separated[1])
value3 = int(separated[2])
# Access array with 2 coordinates.
# ... Multiply the Y by 10 to convert to 1 index.
current = integers[(value2 * 10) + value1]
print("CURRENT: " + str(current));
# Update with new value.
integers[(value2 * 10) + value1] = value3;
Output
2D array has dimensions 10x10
Type 3 values:
X, Y, and value to set
Current value will be printed
5,5,10
CURRENT: 0
2D array has dimensions 10x10
Type 3 values:
X, Y, and value to set
Current value will be printed
5,5,15
CURRENT: 10
2D array has dimensions 10x10
Type 3 values:
X, Y, and value to set
Current value will be printed
5,5,30
CURRENT: 15
2D array has dimensions 10x10
Type 3 values:
X, Y, and value to set
Current value will be printed
0,5,200
CURRENT: 0
2D array has dimensions 10x10
Type 3 values:
X, Y, and value to set
Current value will be printed
0,5,300
CURRENT: 200
2D array has dimensions 10x10
Type 3 values:
X, Y, and value to set
Current value will be printed
And: The benefits will become larger as the size of the data increases. Each integer provides a level of savings.