#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

  1. Include Header File:cCopy code#include <stdio.h> This line includes the standard input-output library, which allows us to use functions like printf.
  2. Function Definition:cCopy codevoid reverseArray(int arr[], int size) Here, we define a function called reverseArray. It takes two parameters: an array of integers arr and its size. This function will handle the logic for reversing the array.
  3. Initialize Indices:cCopy codeint start = 0; int end = size - 1; We set two indices: start begins at the first element (index 0), and end starts at the last element (index size - 1).
  4. While Loop:cCopy codewhile (start < end) { This loop continues as long as the start index is less than the end index. This condition ensures that we only swap elements until we reach the middle of the array.
  5. Swapping Elements:cCopy codetemp = arr[start]; arr[start] = arr[end]; arr[end] = temp; Inside the loop, we perform the following steps:
    • Store the value at the start index in a temporary variable temp.
    • Set the value at the end index to the start index.
    • Finally, assign the value in temp (originally from start) to the end index. This effectively swaps the two elements.
  6. Update Indices:cCopy codestart++; end--; After swapping, we move the start index one step to the right (increment) and the end index one step to the left (decrement) to continue the process towards the center of the array.
  7. Main Function:cCopy codeint main() { This is the starting point of our program. Here, we declare and initialize an array arr.
  8. Calculate Array Size:cCopy codeint 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.
  9. Print Original Array: We loop through the array and print each element to show its original order.
  10. Call the Reverse Function:cCopy codereverseArray(arr, size); We call our reverseArray function, passing in the array and its size.
  11. 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

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights