Как развернуть массив c
Перейти к содержимому

Как развернуть массив c

  • автор:

Reversing Array in C?

Hi i trying to implement a reverse array code but it doesnt seem to work and im really not sure why. The For loop just doesnt seem to work. I dont know why because the logic seems pretty right to me.

5 Answers 5

Avoid using gets() use fgets() instead.

Gangadhar's user avatar

for loop condition should be ‘i < n’. and prototype declaration should match.

for loop condition should be ‘i < n’. and prototype declaration should match.

and «int n» is the size of array. So «i<=n» would make the same array reversed from end to mid, and again from mid to top. So result is same as array. make «n» as half of the array size.

TDK's user avatar

I think better use macro for this task. In code below it is a macro SWAP.

Content a file main.c

Compilation as:

Result:

Notes:

  1. Just working
  2. Workint with any built-in type
  3. Poorly tested and used only the GCC compiler

4.3 The answers on this question

Testing environment

Not gonna lie, it seems as though you’re handling too much in the "reverse function". I personally like to break down my code as much as possible, so that it’s easier to find mistakes.

To start, you may want to put the swapping process (for loop) in its own function called "swap". You can do this with char pointers ‘a’ and ‘b’ as parameters.

C Program to Reverse Elements of an Array

Here, in this page we will discuss the program to reverse elements of an array in C programming language. We are given with an array and need to reverse the elements. We will discuss various algorithms to reverse the array in efficient way.

C program to reverse an array

Here, in page we will discuss various method for reversing the array. Different methods are :

Method Discussed

  • Method 1: Just print the array in reverse order
  • Method 2: Actual in-place reversing of the original array
  • Method 3: Recursive in-place reversing of the original array

Example :

  • Input : arr[5] = [10, 20, 30, 40, 50]
  • Output : Array after reversing, arr[5] = [50, 40, 30, 20, 10]

We will discuss all of these methods one by one below –

C program to reverse an array

Method 1

For an array: arr

  • Run an iterative loop from the last index of the array
  • Print each item one by one arr[i]

Let’s see how it works below –

Method 1 Code in C

Output

Method 2

For an array: arr[] with length len.

  • Call a function void reverse(int arr[], int start, int end)
    • Intial calling as reverse(arr, 0, len-1);
    Time and Space Complexity :
    • Time – Complexity : O(n)
    • Space-Complexity : O(1)

    C Program to Reverse elements of an array

    Method 2 Code in C

    Output

    Method 3

    In this method, we will use recursion. Call function reverseRecursive(int arr[], int start, int end)

    4 ways in C# to reverse an array

    In this post, we will learn different ways to reverse an Array in C#. We can iterate through the array in reverse and build a new array or we can use predefined methods. I will show you three different ways to reverse an array in this post.

    Method 1: By iterating through the array content in reverse:

    In this method, we will iterate through the content of the array in reverse and build another array with its content.

    • GivenArray is the original array of integers.
    • ReverseArray is another array. It is initialised as the equal length of the given array.
    • The for loop is iterating through the GivenArray in reverse order. It inserts the numbers to the ReverseArray.
    • The last two lines are printing the original and reverse arrays.

    If you run this program, it will print the below output:

    Method 2: By using a separate method:

    The above program works only for integer arrays. We can also modify it with generics to work for any type of array. Let’s create a separate method to work with any type of array:

    We are using generics in the Reverse method and it works with any type of array. We can pass an integer array, string array or any other type of array.

    How to reverse an array in C#

    Get Educative’s popular interview prep course for free.

    The Array class in the System namespace provides the Reverse() method used to reverse the elements of an array with multiple overloads.

    Syntax

    • This method works only for single dimensional Arrays and throws a RankException if the array passed as input is multi-dimensional.
    • It throws a ArgumentNullException if the input array is null.
    • The Reverse() method does not use an auxiliary array for reversal and overwrite the existing elements of the array.
    • The time complexity of Array.Reverse() method is O(n) , where n is the number of elements in the array.

    In the example below, we have created and initialized an Array with integers. We then print the Array elements before calling the Reverse() method.

    We then call the Reverse() method and print the Array contents again. The array elements are now reversed, so the program prints the following output.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *