C Program to Reverse an Array

Explanation

This program demonstrates how to reverse an array in C. We will use a simple algorithm that swaps elements from the beginning of the array with elements from the end of the array. The process continues until we reach the middle of the array.

Code


// This C program reverses an array.
// It demonstrates the basic concept of swapping elements in an array.

#include <stdio.h>

/**
 * This function reverses the given array in place.
 * It swaps elements from the beginning and end of the array until the middle is reached.
 * 
 * @param array The array to be reversed.
 * @param size The size of the array.
 */
void reverseArray(int array[], int size) {
    int start = 0;           // Starting index
    int end = size - 1;     // Ending index

    // Loop until start index is less than end index
    while (start < end) {
        // Swap the elements at start and end
        int temp = array[start];
        array[start] = array[end];
        array[end] = temp;

        // Move the start index forward and end index backward
        start++;
        end--;
    }
}

/**
 * This function prints the elements of the given array.
 * 
 * @param array The array whose elements are to be printed.
 * @param size The size of the array.
 */
void printArray(int array[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", array[i]);
    }
    printf("\n");
}

// Main function
int main() {
    // Initialize the array
    int array[] = {1, 2, 3, 4, 5};
    int size = sizeof(array) / sizeof(array[0]);

    // Print the original array
    printf("Original Array:\n");
    printArray(array, size);

    // Reverse the array
    reverseArray(array, size);

    // Print the reversed array
    printf("Reversed Array:\n");
    printArray(array, size);

    return 0;
}

Explanation of the Code

Function Definitions:
The reverseArray function contains the logic to reverse the array in place by swapping elements from the start and end. The printArray function is used to print the elements of the array in a readable format.

Main Function:
The main function serves as the entry point of the program. It initializes an array, prints the original array, calls the reverseArray function to reverse the array, and then prints the reversed array.

reverseArray Function:
The reverseArray function takes an array and its size as inputs and reverses the array in place. It uses two pointers: start (initialized to the beginning of the array) and end (initialized to the end of the array). The elements at these positions are swapped, and the pointers are moved towards the center. This process continues until the start index is less than the end index.

printArray Function:
The printArray function takes an array and its size as inputs and prints its elements. This is used to display the array before and after reversing it.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)