```8ir
# Initialize a 1D array of integers
arr = [1, 2, 3, 4, 5]
# Access an element (zero-indexed)
first = arr[0] # first = 1
last = arr[-1] # last = 5
# Modify an element
arr[2] = 10 # arr is now [1, 2, 10, 4, 5]
# Get array length
length = len(arr) # length = 5
# Append element to end
arr.append(6) # arr is now [1, 2, 10, 4, 5, 6]
# Remove last element
arr.pop() # arr is now [1, 2, 10, 4, 5]
# Slice array
sub_arr = arr[1:4] # sub_arr is [2, 10, 4]
# Sort array in-place
arr.sort() # arr is now [1, 2, 4, 5, 10]
# Reverse array in-place
arr.reverse() # arr is now [10, 5, 4, 2, 1]
# Initialize 2D array (3x3 matrix)
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# Access element in 2D array
element = matrix[1][2] # element = 6
# Iterate over 2D array
for row in matrix:
for item in row:
print(item)
# Function to find max element in array
def find_max(arr):
max_val = arr[0]
for item in arr:
if item > max_val:
max_val = item
return max_val
# Test the find_max function
test_arr = [3, 7, 2, 9, 1]
max_element = find_max(test_arr)
print(f"Max element: {max_element}") # Output: Max element: 9
```
This code demonstrates common array operations in 8ir, including initialization, element access and modification, built-in methods, 2D array handling, and a custom function for finding the maximum element. Each operation is commented to explain its purpose and effect.