Single-Dimensional Array:
A single-dimensional array, often referred to simply as an array, is a collection of elements stored in a contiguous block of memory. Each element in the array is accessed using a single index. Single-dimensional arrays are commonly used to store lists of similar data types, such as integers, floats, or characters.
Example:
# Single-dimensional array in Python
arr = [10, 20, 30, 40, 50]
In this example, arr
is a single-dimensional array containing five elements.
Address Calculation:
The memory address of each element in a single-dimensional array can be calculated using the base address of the array and the index of the element. The formula for calculating the address of the i
th element in an array is:
Where:
- Base Address: The memory address of the first element in the array.
- Size of Data Type: The number of bytes occupied by each element in the array.
- i: The index of the element.
Multidimensional Array:
A multidimensional array is an array with more than one dimension. It is essentially an array of arrays, where each element is itself an array. Multidimensional arrays are used to represent matrices, tables, or higher-dimensional data structures.
Example:
# Multidimensional array in Python
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
In this example, matrix
is a 3×3 multidimensional array containing nine elements arranged in rows and columns.
Address Calculation:
The memory address of an element in a multidimensional array can be calculated using a formula similar to that of a single-dimensional array, but adjusted for multiple dimensions. For example, in a two-dimensional array, you need to consider both row and column indices.
Where:
- Base Address: The memory address of the first element in the array.
- Size of Data Type: The number of bytes occupied by each element in the array.
- i: The row index.
- j: The column index.
- Row Size: The number of elements in each row of the array.
address calculation for arrays involves determining the location of each element in memory based on its index or indices within the array, considering the size of the data type and the array dimensions.