0% found this document useful (0 votes)
2 views

Practical No 1 Array Operations_ Implement Programs for 1-d Arrays, Implement Programs for 2-d Arrays.

The document explains array operations, detailing definitions, types, and examples of 1-D and 2-D arrays. It includes code snippets for creating, accessing, modifying, and performing mathematical operations on these arrays. Key takeaways highlight the importance of arrays in computer science and their applications in data structures and algorithms.

Uploaded by

Soham Ghadge
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Practical No 1 Array Operations_ Implement Programs for 1-d Arrays, Implement Programs for 2-d Arrays.

The document explains array operations, detailing definitions, types, and examples of 1-D and 2-D arrays. It includes code snippets for creating, accessing, modifying, and performing mathematical operations on these arrays. Key takeaways highlight the importance of arrays in computer science and their applications in data structures and algorithms.

Uploaded by

Soham Ghadge
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Practical No.

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:

1. 1-D Array (One-dimensional Array)

● 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).

Use Cases: Storing lists of data such as numbers, names, etc.

2. 2-D Array (Two-dimensional Array)

● 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.

Use Cases: Matrices in mathematics, grids, spreadsheets, image representation.

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.

Use Cases: 3D modeling, scientific computing, image processing, and simulations.

4. Jagged Array (Array of Arrays)

● 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.

5. Associative Arrays (Hash Maps, Dictionaries in Python)

● Definition: Associative arrays (also known as hash maps, or dictionaries in Python)


store data in key-value pairs. Unlike traditional arrays, the keys are not necessarily
integers.
● Accessing Elements: You access elements using the key, e.g., person["name"]
would return "Alice".

Use Cases: Storing key-value pairs such as user information, configurations, or even
mapping one dataset to another.

1-D Array Operations

# 1. Create a 1-D Array (List)


array_1d = [1, 2, 3, 4, 5]
print("1-D Array:", array_1d)

# 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)

# 4. Traversing the array


print("\nTraversing the 1-D Array:")
for element in array_1d:
print(element, end=" ") # Output: 1 2 10 4 5
print()

# 5. Mathematical operations (Sum and Average)


array_sum = sum(array_1d)
array_average = array_sum / len(array_1d)
print("\nMathematical Operations:")
print(f"Sum of elements: {array_sum}")
print(f"Average of elements: {array_average}")

# 6. Find minimum and maximum values


array_min = min(array_1d)
array_max = max(array_1d)
print(f"\nMinimum value: {array_min}")
print(f"Maximum value: {array_max}")

//Explanation of the Programs//

1-D Array Operations:

● 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

# 1. Create a 2-D Array (List of lists)


array_2d = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print("2-D Array:")
for row in array_2d:
print(row)

# 2. Accessing elements in a 2-D array


print("\nAccessing elements in the 2-D Array:")
print(f"Element at position (0, 1): {array_2d[0][1]}") # Output: 2
print(f"Element at position (2, 2): {array_2d[2][2]}") # Output: 9
# 3. Modifying an element in the 2-D array
array_2d[1][2] = 10 # Modify the element at row 1, column 2
print("\nModified 2-D Array:")
for row in array_2d:
print(row)

# 4. Traversing the 2-D array (Nested loop)


print("\nTraversing the 2-D Array:")
for row in array_2d:
for element in row:
print(element, end=" ")
print()

# 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}")

# 6. Transposing the 2-D array (Swapping rows with columns)


transpose_2d = [[array_2d[j][i] for j in range(len(array_2d))] for i in range(len(array_2d[0]))]
print("\nTransposed 2-D Array:")
for row in transpose_2d:
print(row)

# 7. Column-wise sum (Requires iterating over columns)


print("\nColumn-wise sum of the 2-D Array:")
for col_index in range(len(array_2d[0])):
col_sum = sum(array_2d[row_index][col_index] for row_index in range(len(array_2d)))
print(f"Sum of column {col_index}: {col_sum}")

//Explanation of the Programs//

2-D Array Operations:


● Creating a 2-D Array: This is achieved using a list of lists. Each inner list represents a row
of the 2-D array.
● Accessing Elements: We access elements using two indices: one for the row and another
for the column.
● Modifying Elements: Similar to 1-D arrays, elements can be modified by indexing into the
array.
● Traversing the Array: We use nested for loops to iterate over the rows and then each
element in the row.
● Row-wise and Column-wise Operations: We calculate the sum for each row and for each
column by using enumerate() and sum() functions.
Key Takeaways:-
● 1-D arrays are simple linear collections, while 2-D arrays form matrices with rows and
columns.
● Multi-dimensional arrays extend this concept to higher dimensions, useful for complex
data like 3D models.
● Jagged arrays offer flexibility with uneven data, while associative arrays use keys for
more intuitive data access.
● Arrays are fundamental in computer science and are widely used in data structures,
algorithms, and various applications.

You might also like