数据结构中有关的逆置问题,vs2022代码运行问题?

#include<iostream>
#define maxsize 100
void seqlistR1(int seqlist[], int length)
{
    int left = 0;
    int right = length - 1;
    for (left, right; left < right; left++, right--)
    {
        int temp = seqlist[left];
        seqlist[left] = seqlist[right];
        seqlist[right] = temp;
    }
    for (int a=0,int b=length-4; a <b; a++, b--)
    {
        int temp = seqlist[a];
        seqlist[a] = seqlist[b];
        seqlist[b] = temp;
    }
}
void Printf(int seqlist[],int length)
{
    for (int i = 0; i < length; i++)
    {
        std::cout << seqlist[i]<<" ";
    }
}
int main()
{
    int seqlist[maxsize] = { 1,2,3,4,5,6,7,8 };
    int length = 8;
    seqlistR1(seqlist, length);
    Printf(seqlist,length);
    return 0;
}

我想用上面这个代码去实现:将1 2 3 4 5 6 7 8,改为4 5 6 7 8 3 2 1
我在seqlistR1函数里面用了两个for循环,可是代码运行有问题。
报的错为image.png
而当我把函数中的第二个循环单独写成一个函数时,代码且可以运行了。

另一个代码如下:

#include<iostream>
#define maxsize 100
void seqlistR1(int seqlist[], int length)
{
    int left = 0;
    int right = length - 1;
    for (left, right; left < right; left++, right--)
    {
        int temp = seqlist[left];
        seqlist[left] = seqlist[right];
        seqlist[right] = temp;
    }
}
void seqlistR2(int seqlist[], int length)
{
    int left = 0;
    int right = length - 4;
    for (left, right; left < right; left++, right--)
    {
        int temp = seqlist[left];
        seqlist[left] = seqlist[right];
        seqlist[right] = temp;
    }
}
void Printf(int seqlist[], int length)
{
    for (int i = 0; i < length; i++)
    {
        std::cout << seqlist[i] << " ";
    }
}
int main()
{
    int seqlist[maxsize] = { 1,2,3,4,5,6,7,8 };
    int length = 8;
    seqlistR1(seqlist, length);
    seqlistR2(seqlist, length);
    Printf(seqlist, length);
    return 0;
}

我想知道为什么会出现这种情况?

阅读 1.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Microsoft
子站问答
访问
宣传栏