Array in C
Array in C: In C programming, an array is a collection of elements of the same type placed in contiguous memory locations. This allows for efficient indexing and manipulation of data. Here’s a detailed explanation of arrays in C along with the common operations performed on them:
Declaring an Array in C
To declare an array in C, you specify the type of its elements and the number of elements required by an array.
int myArray[10]; // Declares an array of 10 integers
Initializing an Array in C
You can initialize an array at the time of declaration.
int myArray[5] = {1, 2, 3, 4, 5}; // Initializes an array with values 1 to 5
If you don’t initialize all the elements, the remaining elements will be initialized to 0.
int myArray[5] = {1, 2}; // Remaining elements are 0
Accessing Elements of an Array in C
Array elements are accessed using their index. The index of the first element is 0.
myArray[0] = 10; // Sets the first element to 10
int value = myArray[1]; // Gets the second element
Common Operations on Array in C
Traversing an Array
Looping through all the elements of the array.
for (int i = 0; i < 5; i++) {
printf("%d ", myArray[i]);
}
Inserting an Element
To insert an element at a specific position, shift the elements and then assign the value.
int pos = 2; // Position to insert the element
int value = 99;
for (int i = 4; i >= pos; i--) {
myArray[i + 1] = myArray[i];
}
myArray[pos] = value;
Deleting an Element
To delete an element at a specific position, shift the elements to fill the gap.
int pos = 2; // Position of the element to delete
for (int i = pos; i < 4; i++) {
myArray[i] = myArray[i + 1];
}
Searching for an Element
To search for an element, you can use a loop to check each element.
int value = 99;
int found = -1; // Will hold the index of the found element
for (int i = 0; i < 5; i++) {
if (myArray[i] == value) {
found = i;
break;
}
}
Updating an Element
To update an element at a specific index, assign a new value to that index
myArray[2] = 50; // Updates the third element to 50
Multidimensional Arrays in C
C also supports multidimensional arrays, which are arrays of arrays.
int matrix[3][4]; // A 3x4 matrix (2D array)
Initializing a multidimensional array:
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
Accessing elements in a multidimensional array:
int value = matrix[1][2]; // Gets the element in the 2nd row and 3rd column
Example Program
Here’s a complete example program that demonstrates the various operations on arrays.
#include <stdio.h>
int main() {
int myArray[5] = {1, 2, 3, 4, 5};
// Traversing the array
printf("Array elements: ");
for (int i = 0; i < 5; i++) {
printf("%d ", myArray[i]);
}
printf("\n");
// Inserting an element
int pos = 2;
int value = 99;
for (int i = 4; i >= pos; i--) {
myArray[i + 1] = myArray[i];
}
myArray[pos] = value;
// Traversing the array after insertion
printf("Array elements after insertion: ");
for (int i = 0; i < 6; i++) {
printf("%d ", myArray[i]);
}
printf("\n");
// Deleting an element
pos = 3;
for (int i = pos; i < 5; i++) {
myArray[i] = myArray[i + 1];
}
// Traversing the array after deletion
printf("Array elements after deletion: ");
for (int i = 0; i < 5; i++) {
printf("%d ", myArray[i]);
}
printf("\n");
// Searching for an element
value = 4;
int found = -1;
for (int i = 0; i < 5; i++) {
if (myArray[i] == value) {
found = i;
break;
}
}
if (found != -1) {
printf("Element %d found at index %d\n", value, found);
} else {
printf("Element %d not found\n", value);
}
// Updating an element
myArray[2] = 50;
// Traversing the array after updating
printf("Array elements after updating: ");
for (int i = 0; i < 5; i++) {
printf("%d ", myArray[i]);
}
printf("\n");
return 0;
}
This program demonstrates the basic operations: traversal, insertion, deletion, searching, and updating elements in an array. Understanding these fundamental operations will help you work efficiently with arrays in C.