Understanding how traversal works will make your programming logic clearer and more efficient. In this article, you will learn how to traverse arrays in C with simple explanations and practical examples.
Table of Contents
What is Array Traversal in C?
Array traversal means visiting each element of an array exactly once to perform some operation, such as printing, updating, or calculating values. In C, traversal is usually done using loops like for, while, or do-while.Syntax:
Here is the general structure using a for loop:
Here,for(int i = 0; i size; i++) {
// Access array element
array[i];
}
- i represents the index.
- array[i] accesses each element.
- The loop runs from index 0 to size - 1.
Traversing and Printing an Array
Below is the C program to traverse and print an array:
// C program to traverse and
// print an array
#include stdio.h
int main()
{
int arr[5] = {10, 20, 30, 40, 50};
printf("Array elements are:\n");
for(int i = 0; i 5; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Output:
Explanation:Array elements are:
10 20 30 40 50
The array arr contains 5 elements. The loop starts from index 0 and continues until index 4. During each iteration, arr[i] is printed, which gives all elements in sequence.
Traversing Array Using While Loop
Below is the C program to traverse an array using while loop:
// C program to traverse an array
// using while loop
#include stdio.h
int main()
{
int arr[4] = {5, 15, 25, 35};
int i = 0;
printf("Array elements are:\n");
while(i 4)
{
printf("%d ", arr[i]);
i++;
}
return 0;
}
Output:
Explanation:Array elements are:
5 15 25 35
The loop runs as long as i is less than 4. Each iteration prints the current element and increments i. This method works the same as a for loop but gives more control over conditions.
Traversing Array in Reverse Order
Below is the C program to traverse an array in reverse order:
// C program to traverse an array
// in reverse order
#include stdio.h
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
printf("Array elements in reverse:\n");
for(int i = 4; i = 0; i--)
{
printf("%d ", arr[i]);
}
return 0;
}
Output:
Explanation:Array elements in reverse:
5 4 3 2 1
Instead of starting from 0, the loop starts from the last index (4) and moves backward. This prints the array in reverse order.
Traversing and Calculating Sum
Below is the C prorgam to traverse an array and calculate the sum of the elements of the array:
// C program to traverse an array
// and calculate the sum
#include stdio.h
int main()
{
int arr[5] = {2, 4, 6, 8, 10};
int sum = 0;
for(int i = 0; i 5; i++)
{
sum += arr[i];
}
printf("Sum of elements = %d", sum);
return 0;
}
Output:
Explanation:Sum of elements = 30
Each element is added to the variable sum during traversal. By the end of the loop, sum holds the total of all elements.
Key Points About Array Traversal
- Elements Are Accessed Sequentially: Array traversal accesses elements one by one from the beginning to the end of the array.
- Indexing Starts from Zero: In C, array indexing always starts from 0, so the first element is accessed using array[0].
- Loops Are Used for Traversal: Traversal is generally performed using for, while, or do-while loops to move through all elements.
- Array Size Must Be Considered: The loop condition should always depend on the array size to avoid invalid access.
- Traversal Can Be Forward or Reverse: Elements can be accessed from the first element to the last element or in reverse order depending on the requirement.
- Supports Multiple Operations: Array traversal is useful for printing values, calculating sums, finding maximum values, searching, and updating elements.
Common Mistakes in Array Traversal
1. Accessing Elements Outside Array Range: Using indices greater than the array size can produce unexpected results.Example:
Valid indices are 0 to 4.int arr[5];
arr[5]; // Incorrect
2. Using Incorrect Loop Conditions: A wrong loop condition may skip elements or run beyond the array limit.
Incorrect Example:
The correct condition is:for(int i = 0; i = 5; i++)
3. Forgetting to Initialize the Loop Variable: If the loop variable is not initialized, traversal may not work correctly.for(int i = 0; i 5; i++)
Incorrect Example:
Always initialize the variable:int i;
for(i; i 5; i++)
4. Confusing Array Size with Last Index: Many beginners assume the last index is equal to array size.int i = 0;
For an array of size 5:
5. Missing Increment or Decrement Statement: Forgetting i++ or i-- may create an infinite loop.Size = 5
Last index = 4
Incorrect Example:
The loop never ends because i is not updated.for(int i = 0; i 5; )
{
printf("%d", arr[i]);
}
6. Using Wrong Starting Index: Starting traversal from 1 instead of 0 skips the first element.
Incorrect Example:
This ignores arr[0].for(int i = 1; i 5; i++)
7. Modifying Elements Accidentally: Sometimes values change unintentionally during traversal.
Example:
This updates original data, so use it only when modification is required.arr[i] = arr[i] + 10;
8. Hardcoding Array Size Repeatedly: Writing fixed numbers everywhere reduces flexibility.
Less Efficient:
Better Approach:for(int i = 0; i 5; i++)
This makes the code easier to update later.int size = 5;
for(int i = 0; i size; i++)
Conclusion
Array traversal in C is a simple yet powerful concept that you will use repeatedly in programming. Once you understand how to access each element using loops, you can build more complex logic like searching and sorting. The key is to keep your loop conditions correct and avoid out-of-bound errors. With practice, traversal becomes second nature.Frequently Asked Questions
1. What is array traversal in C?2. Which loop is best for array traversal?Array traversal is the process of accessing each element of an array one by one using loops.
3. Can we traverse an array in reverse order?The for loop is most commonly used because it is simple and compact, but while and do-while can also be used.
4. What happens if we go beyond array size?Yes, by starting from the last index and decreasing the loop variable.
5. Why is array traversal important?It leads to undefined behavior and may cause errors or crashes.
It is essential for performing operations like searching, sorting, and modifying array data.
0 Comments