Common Array Operations in C
Array operations are basic tasks performed on arrays such as searching, sorting, updating, inserting, deleting, merging, and reversing.
Understanding these operations helps in solving real-world problems and coding interviews.
1. Traversing an Array
Traversing means visiting each element of the array one by one.
#include <stdio.h>
int main() {
int a[5] = {10,20,30,40,50};
for(int i = 0; i < 5; i++){
printf("%d ", a[i]);
}
return 0;
}
2. Searching an Element (Linear Search)
Linear search checks elements one by one until the key is found.
#include <stdio.h>
int main() {
int a[6] = {12, 45, 7, 22, 9, 30};
int key = 22, found = 0;
for(int i = 0; i < 6; i++){
if(a[i] == key){
found = 1;
break;
}
}
if(found)
printf("Element found");
else
printf("Not found");
return 0;
}
3. Updating an Element
Updating means changing an existing value using its index.
#include <stdio.h>
int main() {
int a[4] = {5,10,15,20};
a[2] = 100;
for(int i = 0; i < 4; i++){
printf("%d ", a[i]);
}
return 0;
}
4. Inserting an Element (At Any Position)
Insertion shifts elements to the right to make space.
#include <stdio.h>
int main() {
int a[10] = {2,4,6,8,10};
int n = 5;
int pos = 2;
int value = 99;
for(int i = n; i > pos; i--){
a[i] = a[i - 1];
}
a[pos] = value;
n++;
for(int i = 0; i < n; i++){
printf("%d ", a[i]);
}
return 0;
}
5. Deleting an Element
#include <stdio.h>
int main() {
int a[6] = {10,20,30,40,50,60};
int pos = 2;
for(int i = pos; i < 6-1; i++){
a[i] = a[i+1];
}
for(int i = 0; i < 5; i++){
printf("%d ", a[i]);
}
return 0;
}
6. Reversing the Array
#include <stdio.h>
int main() {
int a[5] = {1,2,3,4,5};
for(int i = 4; i >= 0; i--){
printf("%d ", a[i]);
}
return 0;
}
7. Sorting (Ascending Order)
#include <stdio.h>
int main() {
int a[6] = {5,2,9,1,7,3};
int temp;
for(int i = 0; i < 6; i++){
for(int j = i+1; j < 6; j++){
if(a[j] < a[i]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(int i = 0; i < 6; i++){
printf("%d ", a[i]);
}
return 0;
}
8. Sorting (Descending Order)
#include <stdio.h>
int main() {
int arr[5] = {4,1,8,2,7};
int temp;
for(int i = 0; i < 5; i++){
for(int j = i+1; j < 5; j++){
if(arr[j] > arr[i]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(int i = 0; i < 5; i++){
printf("%d ", arr[i]);
}
return 0;
}
9. Merging Two Arrays
#include <stdio.h>
int main() {
int a[3] = {1,2,3};
int b[3] = {4,5,6};
int c[6];
for(int i = 0; i < 3; i++){
c[i] = a[i];
}
for(int i = 0; i < 3; i++){
c[i+3] = b[i];
}
for(int i = 0; i < 6; i++){
printf("%d ", c[i]);
}
return 0;
}
10. Counting Even and Odd in Array
#include <stdio.h>
int main() {
int arr[7] = {11,20,33,40,55,60,71};
int even = 0, odd = 0;
for(int i = 0; i < 7; i++){
if(arr[i] % 2 == 0)
even++;
else
odd++;
}
printf("Even = %d\nOdd = %d", even, odd);
return 0;
}
Practice Questions
- Insert a number in the array at a given position.
- Delete a number from a given index.
- Sort an array using Bubble Sort.
- Search an element using Linear Search.
- Merge two sorted arrays.
Practice Task
Create an array of 10 integers and perform:
✔ Find total, average
✔ Count even, odd, positive, negative
✔ Reverse the array
✔ Sort ascending
✔ Sort descending
Print all results clearly.