【问题标题】:Reversing positive sequences in array反转数组中的正序列
【发布时间】:2018-10-09 18:35:52
【问题描述】:

所以,我有一个遍历数组的循环,应该反转连续正数的序列,但它似乎将多余的负数计为序列的一部分,从而改变了它的位置。我自己无法弄清楚错误,很高兴听到任何提示!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int Arr[100];
int Arr2[100];
int main()
{
srand(time(NULL));
int n, x;
bool seq = false;
int ib = 0;
printf("Starting array\n");
for (n = 0; n < 100; Arr[n++] = (rand() % 101) - 50);
for (n = 0; n < 100; printf("%3d ", Arr[n++]));
putchar('\n');
for (n = 0; n < 100; n++) //sorting
{
    if (Arr[n] > 0) //check if the number is positive
    {
        if (seq == false) //if it isn't the part of a sequence
        {
            seq = true; ib = n; //declare it now is, and remember index     of sequence's beginning
        }
        else
            seq = true; //do nothing if it isn't first
    }
    else //if the number is negative
    {   
        if (seq==true) //if sequence isn't null
        for (x = n; ib <= n; ib++, x--) //new variable so that n will stay unchanged, 
          number of iterations = length of sequence
        {
            Arr2[x] = Arr[ib]; //assigning array's value to a new one,
            reversing it in the process
        }
        seq = false; //declaring sequence's end 
        Arr2[n + 1] = Arr[n + 1]; //assigning negative numbers at the same place of a new array
    }
}
printf("Modified array\n");
for (n = 0; n < 100; printf("%3d ", Arr2[n++]));
putchar('\n');
system('pause');
return 0;
}

【问题讨论】:

  • 你能包含声明n , arr[], arr2[], seq and x的代码吗?
  • if (seq = false) --> if(seq == false)
  • 是的。看起来像错字。不过,如果没有minimal reproducible example,则无法确认。
  • 我也不确定ib &lt;= n;,但如果没有看到数组是如何初始化的,就无法判断。
  • 旁注:出现警告级别的现代编译器会将条件中的赋值标记为潜在错误。如果您没有收到警告,请打开警告并将其调高 LOUD

标签: c++ arrays sorting sequence


【解决方案1】:

根据我们在 cmets 中讨论的内容,我在这里列出了几条规则来围绕它制定我的答案。

规则:

  • 元素的顺序可以改变。因此,如果数组中的一行中有 5 个正数,我们将反转 5 个元素。例如

array[5] = {1,2,3,4,5} 将变为 array[5]{5,4,3,2,1}

  • 如果单个正数与负数相邻,则不会发生反转 array[4] = {-1,0,-2,1} 会产生相同的数组

  • 发现负数时不进行任何处理。

基于这些规则。 这是我认为你的代码出了问题的地方。

问题:

1- 考虑这个array = {1,2,-1}。请注意,最后一个值为负数。因为这。以下代码将在处理数组的第三个索引时运行;

` Arr2[n + 1] = Arr[n + 1]; //assigning negative numbers at the same place of a new array`

这是一个禁忌。因为您已经在 Arr2 n+1 的末尾,所以表示数组中有第 4 个元素。 (在你的情况下是数组的 101h 元素)这会导致未定义的行为。

2 - 考虑上面提到的相同数组。当该数组循环时,结果将是 array = {-1,2,1} 。交换 -1 和 1 而不是 1 和 2。

3 - 每当找到负数时,您都在分配 ib = n。因为每当遇到负值时,都会强制使用 seq=false。但是ib,直到找到下一个负数才投入使用。这是一个例子;

array = {...2, 6}

在这种情况下,2 和 6 永远不会颠倒,因为没有负值遵循这个正序列。

4 - 考虑这种情况arr = {-10,-1,....} 这将导致arr = {0,-1,....}。发生这种情况是因为导致上述未定义行为问题的相同代码。

 `Arr2[n + 1] = Arr[n + 1];`

建议
上面提到的大多数问题的发生是因为您试图在找到负数时找出正数的顺序。

 else //if the number is negative
    {   
        if (seq==true) //if sequence isn't null
        for (x = n; ib <= n; ib++, x--) //new variable so that n will stay unchanged, 
          number of iterations = length of sequence
        {
            Arr2[x] = Arr[ib]; //assigning array's value to a new one,
            reversing it in the process
        }

您应该完全摆脱它并完全忽略负数,除非您忘记在问题中提及一些关键细节。而是只关注正数。我不会将整个代码发送给您,但这是我解决问题的方法。如果您需要帮助,请随时告诉我,我会更乐意详细介绍。

  • 像往常一样开始你的 for 循环。

    for (n = 0; n &lt; 100; n++) //sorting {

  • 当数组中的元素为负值时,不要尝试执行任何操作。
    if (Arr[n] &gt; 0) //check if the number is positive

  • 如果数字是正数。创建记录序列索引。一方面,我们知道一旦 `if (Arr[n] > 0) 为真,序列将从n 开始。所以我们可以做这样的事情;

    int sequenceStart = n;

  • 我们还需要知道正数序列何时结束。

    int sequenceEnd = sequenceStart;

  • int sequenceEnd = sequenceStart; 的原因是因为我们将开始使用相同的 n 值开始。我们现在可以遍历数组并递增sequenceEnd,直到达到负数或数组末尾。

        while (currentElement > 0)
        {
            n++;//increment n
            if(n < arraySiz) //make sure we still in the range
            {
                currentElement = Arr[n]; // get the new elemnet
                if (currentElement > 0)
                {
                    sequenceEnd++;
                }
    
            }
            else
                break;     // we hit to a negative value so stop the while loop.
    
        }
    
  • 注意 n++;//increment n 这将增加 n++ 直到我们达到负数。这很棒,因为在序列结束时,for 循环将从更新后的n

  • 继续
  • 在 while 循环之后,您可以创建一个大小与您迭代的序列数相同的数组。然后,您可以存储从 arr[sequenceStart]arr[sequenceEnd] 开始的元素,这样可以更轻松地反转数组中的序列。

【讨论】:

    猜你喜欢
    • 2015-01-09
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    相关资源
    最近更新 更多