Data Structure: Array
What is an Array?
- Definition: An array is a data structure that can hold multiple values, usually of the same data type, in a single variable.
- Purpose: Arrays allow you to organize and manage data efficiently. Instead of creating a separate variable for each value, you can store all the values in a single array.
Key Features of Arrays
1. Fixed Size:
- When we create an array, we need to define its size, meaning how many elements it can hold. This size is usually fixed and cannot be changed later.
2. Indexed Elements:
- Each item in an array has a specific position, called an index. Indexing usually starts at 0, meaning the first element is at index 0, the second at index 1, and so on.
3. Homogeneous Data:
- Arrays store elements of the same type. For example, an array of integers can only hold integers.
4. Contiguous Memory Allocation:
- Elements in an array are stored next to each other in memory (contiguous memory locations). This makes accessing elements by their index very efficient.
1-Dimensional (1-D) Array
Imagine we have a row of lockers, and each locker can hold a number:
Fig. 1: Representation of 1-Dimensional (1-D) Array
- Locker Number: Represents the index of the array.
- Number Inside: Represents the value stored at that index.
Real-life Example of 1-D Array
Example:
- A piano keyboard is essentially a one-dimensional array where each key represents an element in the array. The keys are arranged in a linear sequence, and each key corresponds to a specific musical note (e.g., A, B, C, D, etc.).
- The index of the array can be thought of as the position of the key on the keyboard, and the value stored at each index is the pitch or frequency of the note produced when the key is pressed.
- For example:
Index 0: A0 (lowest note)
Index 1: B0
Index 2: C1
…
Index 87: C8 (highest note)
When you play a melody, you’re essentially traversing this 1-D array, accessing specific indices (keys) in a sequence to create music. The simplicity of the 1-D array structure beautifully mirrors the linear arrangement of the piano keys, yet it gives rise to the complexity and richness of musical expression.
- A 1-D array is a linear data structure that stores elements of the same type in a contiguous block of memory. Each element in the array can be accessed using an index, which represents its position in the sequence.
- Let’s say we have an array of integers representing the number of apples sold each day of the week:
Index: The position of each element in the array (starting from 0).
Value: The data stored at each index.
- Memory Representation: In memory, a 1-D array is stored as a contiguous block. For example, if the array is [10, 15, 12, 20, 18, 14, 16], it might look like this in memory:
Each integer takes 4 bytes of memory (for example).
The memory address increases by 4 for each subsequent element.
- Mathematical Representation: A 1-D array can be represented mathematically as:
A = [a0, a1, a2, …, an−1]
Where,
A is the array.
a0, a1, …, an−1 are the elements of the array.
n is the size of the array.
C program that demonstrates the concept of a 1-D array
Following program will:
- Declare and initialize a 1-D array.
- Access and print elements of the array.
- Traverse the array using a loop.
- Modify an element in the array.
- Print the updated array.
C Program: 1-D Array Explanation
Explanation of the Code
1) Array Declaration and Initialization:
int numbers[5] = {10, 20, 30, 40, 50};
- numbersis an array of integers with a size of 5.
- It is initialized with the values {10, 20, 30, 40, 50}.
2) Accessing and Printing Array Elements:
for (int i = 0; i < 5; i++) { printf("numbers[%d] = %d\n", i, numbers[i]); }
- A forloop is used to traverse the array.
- numbers[i]accesses the element at index i.
3) Modifying an Element:
numbers[2] = 100;
- The value at index 2(third element) is changed from 30 to 100.
4) Printing the Updated Array:
- The same loop is used to print the array again, showing the updated value
Output of the Program:
Elements of the array: numbers[0] = 10 numbers[1] = 20 numbers[2] = 30 numbers[3] = 40 numbers[4] = 50
Updated elements of the array: numbers[0] = 10 numbers[1] = 20 numbers[2] = 100 numbers[3] = 40 numbers[4] = 50
Notes:
- Arrays are zero-indexed: The first element is at index 0.
- Elements can be accessed and modified using their index.
- Loops are commonly used to traverse arrays.
- Arrays are stored in contiguous memory locations, making them efficient for random access.