#include <stdio.h>
void reverseArray(int arr[], int size) {
int start = 0; // Initialize start index
int end = size - 1; // Initialize end index
int temp; // Temporary variable for swapping
// Loop until the start index is less than the end index
while (start < end) {
// Swap the elements at start and end indices
temp = arr[start]; // Store the start element in temp
arr[start] = arr[end]; // Assign the end element to start position
arr[end] = temp; // Assign temp (original start) to end position
// Move indices towards the center
start++; // Increment start index
end--; // Decrement end index
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5}; // An array to be reversed
int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array
printf("Original array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]); // Print original array
}
printf("\n");
reverseArray(arr, size); // Call the function to reverse the array
printf("Reversed array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]); // Print the reversed array
}
printf("\n");
return 0; // End of program
}
Explanation
- Include Header File:cCopy code
#include <stdio.h>This line includes the standard input-output library, which allows us to use functions likeprintf. - Function Definition:cCopy code
void reverseArray(int arr[], int size)Here, we define a function calledreverseArray. It takes two parameters: an array of integersarrand itssize. This function will handle the logic for reversing the array. - Initialize Indices:cCopy code
int start = 0; int end = size - 1;We set two indices:startbegins at the first element (index 0), andendstarts at the last element (indexsize - 1). - While Loop:cCopy code
while (start < end) {This loop continues as long as thestartindex is less than theendindex. This condition ensures that we only swap elements until we reach the middle of the array. - Swapping Elements:cCopy code
temp = arr[start]; arr[start] = arr[end]; arr[end] = temp;Inside the loop, we perform the following steps:- Store the value at the
startindex in a temporary variabletemp. - Set the value at the
endindex to thestartindex. - Finally, assign the value in
temp(originally fromstart) to theendindex. This effectively swaps the two elements.
- Store the value at the
- Update Indices:cCopy code
start++; end--;After swapping, we move thestartindex one step to the right (increment) and theendindex one step to the left (decrement) to continue the process towards the center of the array. - Main Function:cCopy code
int main() {This is the starting point of our program. Here, we declare and initialize an arrayarr. - Calculate Array Size:cCopy code
int size = sizeof(arr) / sizeof(arr[0]);We calculate the number of elements in the array by dividing the total size of the array by the size of one element. - Print Original Array: We loop through the array and print each element to show its original order.
- Call the Reverse Function:cCopy code
reverseArray(arr, size);We call ourreverseArrayfunction, passing in the array and its size. - Print Reversed Array: Finally, we print the elements of the array again, but this time in their reversed order.
Output of the Given Code
Original array: 1 2 3 4 5
Reversed array: 5 4 3 2 1