C Program to swap the first and last elements of an array
Learn how to swap the first and last elements of a C array using a temporary variable, with validated code, examples, edge cases, and complexity.
In a C array, the first element is stored at index 0, and the last element is stored at index size - 1. We can exchange these two values using a temporary variable.
For example:
Original array: 10 20 30 40 50
Swapped array: 50 20 30 40 10
Only the first and last elements change positions. All elements between them remain unchanged.
C Program to Swap the First and Last Array Elements
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int array[MAX_SIZE];
int size;
printf("Enter the number of elements: ");
if (scanf("%d", &size) != 1 || size < 1 || size > MAX_SIZE) {
printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
return 1;
}
printf("Enter %d elements:\n", size);
for (int index = 0; index < size; index++) {
if (scanf("%d", &array[index]) != 1) {
printf("Invalid array element.\n");
return 1;
}
}
printf("Original array: ");
for (int index = 0; index < size; index++) {
printf("%d ", array[index]);
}
if (size > 1) {
int temporary = array[0];
array[0] = array[size - 1];
array[size - 1] = temporary;
}
printf("\nArray after swapping: ");
for (int index = 0; index < size; index++) {
printf("%d ", array[index]);
}
printf("\n");
return 0;
}
Sample Output
Enter the number of elements: 5
Enter 5 elements:
10 20 30 40 50
Original array: 10 20 30 40 50
Array after swapping: 50 20 30 40 10
How the Program Works
- The program validates the number of elements before reading the array.
array[0]identifies the first element.array[size - 1]identifies the last element.- The first value is saved in
temporaryso it is not lost. - The last value is copied into the first position.
- The saved first value is copied into the last position.
- The program prints the modified array.
For the sample array, the swap happens as follows:
| Step | temporary | First element | Last element |
|---|---|---|---|
| Before swapping | — | 10 | 50 |
| Save first element | 10 | 10 | 50 |
| Copy last to first | 10 | 50 | 50 |
| Copy temporary to last | 10 | 50 | 10 |
The middle elements 20, 30, and 40 are never assigned new values, so they remain in their original positions.
Why Is the Last Index size - 1?
C arrays use zero-based indexing. An array containing five elements has these indices:
Element: 10 20 30 40 50
Index: 0 1 2 3 4
When size is 5, the last valid index is 5 - 1, which is 4. Accessing array[size] would go one position beyond the valid array and cause undefined behavior.
What Happens with One Element?
In a one-element array, the first and last elements are the same element at index 0. No swap is necessary, so the program performs the assignment only when size > 1.
Original array: 42
Array after swapping: 42
What Happens with Two Elements?
For a two-element array, the two values simply exchange positions:
Original array: 7 9
Array after swapping: 9 7
Why Use a Temporary Variable?
Without a temporary variable, assigning the last element to the first position would overwrite the original first value before it could be moved.
Arithmetic or XOR-based swaps are possible, but they are less readable and can introduce overflow or maintenance problems. A temporary variable is clear, safe, and works for all integer values.
Time and Space Complexity
- Swap operation:
O(1)time because it always performs the same three assignments. - Complete program:
O(n)time because reading and printing require traversing allnelements. - Extra space complexity:
O(1)because the swap uses only one temporary variable.
Common Mistakes
- Using
array[size]for the last element instead ofarray[size - 1]. - Overwriting the first value without saving it in a temporary variable.
- Allowing an empty array and then accessing
array[0]. - Swapping every pair of elements when only the first and last elements should change.
- Forgetting that an array with one element requires no change.
Using indices 0 and size - 1 with one temporary variable provides a safe and direct way to swap the first and last elements of an array.