This C code consists of two utility functions and a...

September 2, 2025 at 06:46 AM

#include <stdio.h> // Function to reverse an array in place void reverseArray(int *arr, int size) { int start = 0; int end = size - 1; while(start < end) { int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } // Function to print an array void printArray(int *arr, int size) { for(int i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("\n"); } int main() { int numbers[] = {1, 2, 3, 4, 5, 6}; int size = sizeof(numbers) / sizeof(numbers[0]); printf("Original array: "); printArray(numbers, size); reverseArray(numbers, size); printf("Reversed array: "); printArray(numbers, size); return 0; }

This C code consists of two utility functions and a main() function to demonstrate reversing the elements of an array in place.

Here is a breakdown of what the code does:

1. Function: reverseArray

  • Purpose: Reverses the elements of an array arr in place.
  • Parameters:
    • int *arr: A pointer to the start of the array to be reversed.
    • int size: The number of elements in the array.
  • Process:
    • Uses two pointers (start and end) initialized to the beginning (0) and end (size - 1) of the array.
    • Swaps the elements at the start and end indices.
    • Moves start forward and end backward iteratively until the array has been reversed.

2. Function: printArray

  • Purpose: Prints the elements of an integer array to the console.
  • Parameters:
    • int *arr: A pointer to the start of the array to be printed.
    • int size: The number of elements in the array.
  • Process:
    • Loops through each element in the array.
    • Prints the elements one by one, separated by a space.
    • Adds a newline at the end.

3. Function: main

  • Purpose: Main entry point that demonstrates reversing an array.
  • Steps:
    1. Declares and initializes an integer array numbers with the elements {1, 2, 3, 4, 5, 6}.
    2. Calculates the size of numbers using the expression sizeof(numbers) / sizeof(numbers[0]).
      • This divides the total memory size of the array by the size of a single element to get the number of elements.
    3. Prints the original array using printArray.
    4. Calls reverseArray to reverse the array in place.
    5. Prints the reversed array using printArray.

Output

The output of the program is:

Original array: 1 2 3 4 5 6 
Reversed array: 6 5 4 3 2 1
  • Before reversal, the array elements are printed in their original order: 1, 2, 3, 4, 5, 6.
  • After reversal, the array is printed again, but the order has been reversed: 6, 5, 4, 3, 2, 1.

Core Functionality

The key feature is that the reverseArray function operates in place without requiring any additional arrays, making it memory efficient. It swaps elements until the array is reversed.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node