array ds 3 (1)
array ds 3 (1)
Features of Arrays
1. Fixed Size: The size of an array is specified at the time of declaration and
cannot be changed later. For example, an array of size 10 can hold exactly 10
elements.
2. Homogeneous Data: All elements in an array must be of the same data type.
For example, an integer array can only store integers.
3. Indexed Access: Each element in an array is identified by an index, starting
from 0. This allows easy and fast access to elements.
4. Continuous Memory Allocation: All elements are stored in adjacent memory
locations.
Declaration of Arrays in C
To declare an array in C, you specify the data type, array name, and the size of the
array.
Syntax:
data_type array_name[array_size];
Example:
In the above example, numbers can store 5 integers, and you can access the
elements using indices from 0 to 4 (e.g., numbers[0], numbers[1]).
Initialization of Arrays
You can initialize an array at the time of declaration by providing a list of values.
Syntax:
Example:
int numbers[5] = {10, 20, 30, 40, 50};
Alternatively, if the number of elements is known, you can omit the size, and the
compiler will automatically calculate it.
Example:
int numbers[] = {10, 20, 30, 40, 50}; // Size automatically set to 5
You can access the elements of an array using their index. The first element is at
index 0, the second at index 1, and so on.
Example:
You can also modify the values of array elements by assigning new values to
specific indices.
Example:
Array Traversal
Traversing an array means accessing each element of the array one by one. This is
commonly done using a for loop.
Example:
Types of Arrays
Syntax:
data_type array_name[rows][columns];
Example:
Example:
To access an element in a 2D array, you need two indices: one for the row and one
for the column.
Example:
To traverse a 2D array, you use two for loops: one for the rows and one for the
columns.
Example:
Advantages of Arrays
1. Random Access: You can access any element directly using its index.
2. Efficiency: Arrays are efficient in terms of memory and accessing elements.
3. Easy Traversal: Looping through elements is easy.
Disadvantages of Arrays
1. Fixed Size: The size of an array is fixed, which can lead to unused memory or
overflow errors.
2. Homogeneous Data: Arrays can store only one type of data.
3. Costly Insertions/Deletions: Inserting or deleting elements in an array
(except at the end) requires shifting the other elements, which can be time-
consuming.
Array Operations in C
1. Insertion:
#include <stdio.h>
int main() {
int arr[6] = {1, 2, 3, 5, 6};
int size = 5; // Current size of array
return 0;
}
2. Deletion:
#include <stdio.h>
void delete(int arr[], int n, int pos) {
for (int i = pos; i < n - 1; i++) {
arr[i] = arr[i + 1]; // Shift elements
}
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int size = 5;
return 0;
}
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int key = 3;
if (index != -1) {
printf("Element found at index %d\n", index);
} else {
printf("Element not found\n");
}
return 0;
}
int main() {
int arr[5] = {5, 3, 1, 4, 2};
int size = 5;
bubbleSort(arr, size);
return 0;
}