An Array Reversal Program in C is a common task that involves reversing the order of elements in an array. This task helps in understanding array manipulation, looping structures, and basic input/output in C.

Example: Array Reversal Program

In this example, we will write a program that allows the user to input a specific number of integers into an array and then reverses the order of those integers.

C Program to Reverse an Array

#include <stdio.h> int main() { int n, i; int arr[100]; // Declare an array of size 100 // Input the number of elements in the array printf("Enter the number of elements in the array: "); scanf("%d", &n); // Input elements into the array printf("Enter %d integers:\n", n); for (i = 0; i < n; i++) { printf("Element %d: ", i + 1); scanf("%d", &arr[i]); // Store each element in the array } // Reverse the array int start = 0; // Starting index int end = n - 1; // Ending index int temp; // Temporary variable for swapping while (start < end) { // Swap the elements at the start and end indices temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; // Move the indices towards the center start++; end--; } // Output the reversed array printf("The reversed array is:\n"); for (i = 0; i < n; i++) { printf("%d ", arr[i]); // Print each element } printf("\n"); return 0; }

Explanation:

  1. Variable Declarations:

    • int n: This variable holds the number of elements the user wants to input.
    • int i: Loop counter for iterating through the array.
    • int arr[100]: Declares an integer array of size 100 to store the elements.
  2. Input for Array Size:

    • The program prompts the user to enter the number of elements they want to reverse and stores this value in n.
  3. Input for Array Elements:

    • A for loop is used to iterate from 0 to n-1, prompting the user to input each integer, which is stored in the array arr.
  4. Reversing the Array:

    • Two indices, start and end, are initialized to point to the first and last elements of the array, respectively.
    • A while loop continues to execute as long as start is less than end.
    • Inside the loop:
      • The elements at start and end are swapped using a temporary variable temp.
      • The start index is incremented, and the end index is decremented to move toward the center of the array.
  5. Output the Reversed Array:

    • Finally, a for loop iterates over the array to print the elements in their new reversed order.

Sample Output:

Example 1:

Enter the number of elements in the array: 5 Enter 5 integers: Element 1: 10 Element 2: 20 Element 3: 30 Element 4: 40 Element 5: 50 The reversed array is: 50 40 30 20 10

Example 2:

Enter the number of elements in the array: 3 Enter 3 integers: Element 1: 5 Element 2: 15 Element 3: 25 The reversed array is: 25 15 5

Key Points:

  • Array Manipulation: The program demonstrates how to manipulate arrays through indexing and swapping.
  • Looping: The use of both for and while loops illustrates different ways to traverse an array.
  • Input/Output: Standard input/output functions are used for user interaction.

Variations:

You can modify the program to:

  • Handle larger arrays dynamically using dynamic memory allocation (with malloc).
  • Reverse only a portion of the array based on user input.
  • Implement a function for array reversal to modularize the code.

This program serves as a foundational exercise for understanding array manipulation and control structures in C programming.