Reverse Array in C Langauge | Reverse Array Elements without using extra memory

program-to-reverse-array-in-c-language

Program Description:

Write a Program to Reverse Array in C programming language. The program should accept an input array from the user and reverse the array elements.

We are going to look at a few methods in this article.

  • Reverse an Array without using extra memory
  • Using reverseArray function to reverse the array elements
  • Copying Original Array to new Array using Extra Memory (Not Recommended).

Here are the expected input and output of the program.

Example Input and Output:

Input:

Output:

Prerequisites:

We need to know the basics of the Arrays, and Functions in C to better understand the following program.

Reverse Array in C without using extra memory Program Explanation:

  1. Create an array named numbers with size of 100. And few other supporting variables like i, size, start, end, and temp variables
  2. Take the desired array size from the user and store it in size variable.
  3. We need to take the input values for the array. So prompt the user to provide the array elements.
    • Create a for loop and start with the element zero(index) to size-1 index.
    • Prompt the user for input and read it using scanf function and update the numbers[i] element.
  4. Once we got the array, We can display it as the Original Array. ( We need to use another for loop to display the array elements)
  5. To Reverse the array, update the variables start with zero(0) and end with size-1. So the start variable is pointing to the first element in the array and the end element is pointing to the last element in the array.
  6. The fundamental concept is to swap the first element for the final element, the second element for the second-to-last element, and so forth. In this manner, the array’s elements will be reversed when the loop is completed.
  7. So start the loop start=0 and end=size-1. At each iteration,
    • Swap the numbers[start] with numbers[end].
    • Here we are using a temp variable to do the swapping, But we can also use Swapping without using the temporary variable method. for simplicity, we are using the temporary variable to swap the elements.
    • Then increment the start variable (i.e start++) and decrement the end variable (i.e end--)
  8. Once the above loop is finished, The array elements will be Reversed.
  9. Display the reversed array on the console.

Algorithm to Reverse Array in C (without extra memory):

Here is the algorithm for reversing an array in the C language without using any extra space.

  1. Start the program
  2. Declare numbers[100], i, size, start, end, and temp variables.
  3. Take the size from the user
  4. Prompt the user and Update the numbers array
    • FOR i=0 to i=size-1
    • Update the &numbers[i] element
  5. Display the original array.
    • FOR i=0 to i=size-1
    • display the numbers[i] element
  6. Reverse the Array
    • FOR start=0; end=size-1 till (start < end)
    • Swap the numbers[start] and numbers[end]
    • Update start++ and end-- at each iteration
  7. Display the Reversed Array
    • FOR i=0 to i=size-1
    • display the numbers[i] element
  8. Stop the program

Program to Reverse Array in C language:

Let’s convert the above algorithm into the C program.

Reverse Array Program Output:

Compile and run the program using your favorite compiler.

Compile the program

gcc reverse-array.c

The above command generates an executable file ( a.out in Linux). Run the executable file.

Test 1:

reverse-array-in-c-program-output


Let’s try a few more examples.

Test 2:

Method 2: Reverse Array elements in C using a function (reverseArray):

Let’s convert the above program into small functions, where each function does a specific task. In the above program, We used only one function which is main() to perform all the tasks.

Here is the program with three user-defined functions(except main()) which are read, display, and reverseArray. Each function does a specific task.

In the above program, We have defined four functions including the main() function.

  1. The read() function – void read(int numbers[], int size)
    • This function takes two formal arguments. The numbers array and the size
    • The read() is used to take the user input and update the array elements.
  2. The display() function – void display(int numbers[], int size)
    • This function also takes two formal arguments, which are numbers[] array and its size
    • The display() function is used to display the array on the standard output.
  3. The reverseArray() function – void reverseArray(int numbers[], int size)
    • This is the function, which actually reverse the array. The reverseArray() function takes two formal arguments, they are numbers array and size and it doesn’t return anything( void)]
    • The reverseArray modifies the input array numbers and reverses the elements in place by swapping the array elements.
    • As the numbers array is passed by the address, so all the changes made in the reverseArray will be reflected in the main() function’s numbers array.

Program Output:

Let’s compile and run the program and observe the output.

reverse-array-using-function-in-c-program-output

Method 3: Reverse the Array by creating a copy of the original Array (Using Extra space):

We can reverse the array by copying all the elements of the array in the reverse order to a new array. So here is the program to copy the elements to new array in reverse order.

Here we have created a new array revArr with the same size of the numbers array (i.e 100).

And copied the numbers array in reverse order to the revArr.

We need to use the two control variables i and j. Where i goes from size-1 to zero(0) and j goes from zero(0) to size-1. At each iteration we simply copy the numbers[j] to the revArr[i] element.

Once the above loop is completed, The revArr array contains the Reversed Array.

Program Output:

Compile and Run the program

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] Array using Recursion in C programming language. In an earlier tutorial, We have looked at the Reversing array elements using the Iterative method. In today’s article, we will use Recursion to reverse the array […]

  2. […] C Program to Reverse the Array Elements […]

Leave a Reply