Practical No 1 Array Operations_ Implement Programs for 1-d Arrays, Implement Programs for 2-d Arrays.
Practical No 1 Array Operations_ Implement Programs for 1-d Arrays, Implement Programs for 2-d Arrays.
1
Array Operations: Implement programs for 1-d arrays, Implement programs for 2-d
arrays.
What is an Array?
An array is a collection of elements, typically of the same data type, stored in a contiguous
memory block. Arrays allow for efficient storage and access to elements using indices
(positions within the array). They provide a way to manage large datasets, especially when
performing repetitive tasks like sorting, searching, or arithmetic operations.
Types of Arrays
Arrays can be categorized based on their dimensions and structure. Below are the common
types of arrays:
● Definition: A 1-D array is a simple, linear structure that holds a collection of elements
in a single row or column.
● Example: A list of integers representing scores: [10, 20, 30, 40, 50]
● Accessing Elements: Using a single index: array[0] (accesses the first element).
● Definition: A 2-D array is essentially a collection of 1-D arrays, arranged in rows and
columns, forming a matrix-like structure.
● Accessing Elements: Using two indices: array[row][column]. For example,
array[1][2] accesses the element in the second row and third column.
3. Multi-dimensional Array
● Definition: Arrays with more than two dimensions. These can be thought of as arrays
of 2-D arrays, 3-D arrays, and so on.
● Accessing Elements: You use multiple indices, such as array[x][y][z] where x, y,
and z refer to the first, second, and third dimensions respectively.
● Definition: A jagged array is an array whose elements are arrays, and these inner
arrays can have different lengths (unlike multi-dimensional arrays where all rows or
columns have the same length).
Use Cases: When dealing with data structures that do not require uniform sizes across rows,
like ragged matrices or nested data collections.
Use Cases: Storing key-value pairs such as user information, configurations, or even
mapping one dataset to another.
# 2. Accessing elements
print("\nAccessing elements:")
print(f"Element at index 0: {array_1d[0]}") # Output: 1
print(f"Element at index 2: {array_1d[2]}") # Output: 3
# 3. Modifying an element
array_1d[2] = 10 # Change the value at index 2 to 10
print("\nModified 1-D Array:", array_1d)
● Creating the Array: The array is simply a Python list, and we initialize it with values.
● Accessing Elements: We access elements using their indices, which start from 0 in
Python.
● Modifying Elements: You can change values by directly referencing their index.
● Traversing: A for loop is used to print each element.
● Mathematical Operations: We calculate the sum and average of the elements. We also
find the minimum and maximum values of the array.
Output:-
2-D Array Operations
# 5. Row-wise sum
print("\nRow-wise sum of the 2-D Array:")
for i, row in enumerate(array_2d):
row_sum = sum(row)
print(f"Sum of row {i}: {row_sum}")