简体   繁体   English

C语言的反向数组

[英]Reverse array in C language

It is an reverse array program, but the output of program is getting wrong its not reversing all numbers I have dry run this code but am not getting where the issue is?这是一个反向数组程序,但是程序的 output 出错了,它没有反转所有数字我试运行了这段代码,但我不知道问题出在哪里?

#include <stdio.h>

void reverseArray(int ar[], int start, int end)
{
    while (start < end)
    {   ar[start] = ar[start] + ar[end];
        ar[end] = ar[start] - ar[end];
        ar[start] = ar[start] - ar[end];
        start += 1;
        end -= 1;
        reverseArray(ar, start, end);
    }
  
}

void main()
{
    int ar[5] = {1, 2, 3, 4, 5};
    reverseArray(ar, 0, 4);
    for (int i = 0; i < 5; i++)
    {
        printf("%d ", ar[i]);
    }
}

Output is: 5 2 3 4 1 Output 是:5 2 3 4 1

What I found that control is getting inside while loop for 3 times but according to condition it should run the loop only for 2 times我发现控件进入 while 循环 3 次,但根据条件它应该只运行循环 2 次

You're mixing loop with recursion.您将循环与递归混合在一起。 Recursively, you reverse the subsection of your array and reverse it again with your loop.递归地,你反转你的数组的子部分并用你的循环再次反转它。 Only the outermost elements remain swapped.只有最外层的元素保持交换状态。

By the way, swapping like this could be subject to overflows, resulting in undefined behaviour.顺便说一下,这样的交换可能会溢出,导致未定义的行为。

You don't need to add the function call at the end.您不需要在末尾添加 function 调用。 If you want to add that, then you don't end to add the while loop ,because by calling the function you are making it recursive.如果你想添加它,那么你不会结束添加while 循环,因为通过调用 function 你正在使它递归。 Recursion and loops don't go well together . go 递归和循环不能很好地结合在一起

Also, try not to pass Hard-coded values in parameters, It isn't a good practice:)另外,尽量不要在参数中传递硬编码值,这不是一个好习惯:)

Due to having both a while loop and using recursive calls, your code is doing由于同时具有while循环和使用递归调用,您的代码正在执行

reverseArray(ar, 0, 4)
    a[0] <-> a[4]  // swap
    reverseArray(ar, 1, 3)
        a[1] <-> a[3]  // swap
        reverseArray(ar, 2, 2)
    a[1] <-> a[3]  // swap
    reverseArray(ar, 2, 2)

so you swap element ar[1] and ar[3] twice, ie they end up in their original positions.所以你交换元素ar[1]ar[3]两次,即它们最终回到原来的位置。

The solution seems simple: Just delete the recursive call.解决方案似乎很简单:只需删除递归调用即可。

Further:更远:

Don't swap elements by "smart code" like:不要通过“智能代码”交换元素,例如:

    ar[start] = ar[start] + ar[end];
    ar[end] = ar[start] - ar[end];
    ar[start] = ar[start] - ar[end];

Write it simple and readable like:写得简单易读,例如:

    int tmp = ar[start];
    ar[start] = ar[end];
    ar[end] = tmp;

It's easier to read and understand, it's most likely faster and most important it avoids integer overflow (which is undefined behavior in C).它更容易阅读和理解,它很可能更快,最重要的是它避免了 integer 溢出(这是 C 中的未定义行为)。

Let me tell you the issue.让我告诉你这个问题。

Values when your program reaches the line程序到达行时的值

reverseArray(ar, start, end);
start = 1, end = 3, arr={5,2,3,4,1}

After this, your compiler goes into the function once again from where your array comes back completely reversed.在此之后,您的编译器再次进入 function,您的数组从那里完全反转回来。 But all this happen in some other while loop because you called the function once again.但是所有这一切都发生在其他 while 循环中,因为您再次调用了 function。 Hence when your compiler comes back from the function call.因此,当您的编译器从 function 调用返回时。 Remember, your values of start and end in the first while loop will still be请记住,您在第一个 while 循环中的 start 和 end 值仍然是

Start = 1, end = 3
**But**
Your array is now arr = {5,4,3,2,1}

Your while loop runs again and that is why your first and last element isn't disturbed while your rest of the array is disturbed in final output.你的 while 循环再次运行,这就是为什么你的第一个和最后一个元素没有受到干扰,而你的数组的 rest 在最后的 output 中受到干扰。

Already there are great answers provided by the experts..专家们已经提供了很好的答案..

I am trying to answer in my way.我正在尝试以我的方式回答。

Basically when reverseArray(ar, start, end);基本上当reverseArray(ar, start, end); is called start=1,end=3称为开始=1,结束=3

Recursively calling reverseArray(ar, start, end);递归调用reverseArray(ar, start, end);

reversed your array and returned control when start=2 end=2当 start=2 end=2 时反转你的数组并返回控制

But your first while loop hasn't finished yet and undoing what you wanted to achieve.但是你的第一个 while 循环还没有完成并撤消你想要实现的目标。

Function call by value, by reference is playing the game here. Function call by value, by reference 在这里玩游戏。

空运行

You are passing reference of arr but for start and end you are passing value.您正在传递 arr 的引用,但对于开始和结束,您正在传递价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM