Understanding Arrays in C Programming
What is Array
An array is a collection of the same type of data items, which are stored in consecutive memory locations under a common name.
Features of Array
- An array is a linear data structure.
- An array can store many values using a single variable name.
- Arrays store elements of the same data type.
- Every element has an index number. Array elements can be accessed quickly using index numbers.
- Array elements are stored in continuous memory locations.
Types of Array
There are two types of Arrays :
1. One-Dimensional Array
A One-dimensional array is an array in which only one index is used to access or specify an element of the array.
Declaration of One-Dimensional Array
In C, One-dimensional array can be declared as follows:
data_type array_name [array_size];
Here, array_name is the name of the array, and array_size represents how many elements the array can store. The data_type specifies the type of data stored in the array, such as int, float, or char. The size of the array must be a whole number.
For example int array[5];
Initialization of One-Dimensional Array
In C, One-dimensional array can be initialized in two ways.
- Initialization with Declaration
In this method, values are assigned to the array at the time of declaration as follows:
data_type array_name[array_size] = {value1, value2, value3};
For example: int array[] = {1, 2, 3, 4, 5};. Here, it is not necessary to define size of array.
- Initialization After Declaration
In this method, the array is declared first, and values are assigned later using indexes as follows:
array_name[index_number] = value1;
For example: if int array[5]; then array[0] = 1;. Now, first element of array is 1.
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
Operations Perform on Arrays
1. Traversing
Traversing means accessing and displaying each element of an array one by one.
C Program for Traversing Array:
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
2. Insertion
Insertion means adding a new element into an array at a specific position.
C Program for Insertion
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
3. Deletion
Deletion means removing an element from an array. After deletion, remaining elements are shifted left.
C Program for Deletion
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
4. Merging
Merging means combining two arrays into a single array.
C Program for Merging Arrays
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
5. Searching
Searching means finding whether an element exists in an array or not.
C Program for Searching
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
6. Sorting
Sorting means arranging array elements in a particular order such as ascending or descending order.
C Program for Sorting Array
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |