Array traversal and manipulation in C language
In the C programming language, accessing, changing, and manipulating items of an array is referred to as array traversal and manipulation. An array’s elements can be accessed using their respective index values, which range from 0 for the first element to n-1 for the nth element, where n is the array’s size.
Accessing each element of the array and performing an operation on it is array traversal. For, while, and do-while loops can be used for this.
Example:
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]); // calculating the size of the array
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
Modifying an array’s element values constitutes array manipulation. The array items’ index values can be used for this.
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]); // calculating the size of the array
// changing the value of the third element
arr[2] = 35;
// printing the modified array
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
In the example above, we altered the third array element’s value from 30 to 35 before printing the amended array.