ARRAYS调试错误的输出,复杂的算法

时间:2014-02-04 22:51:34

标签: c++ c arrays algorithm debugging

我制作了这个算法,我正在调试它,看看为什么它不工作,但后来我开始得到奇怪的东西,在每个周期结束时打印数组,看看问题的第一次发生。 乍一看,似乎我的周期没有考虑最后一个数组值,但我不知道...... 关于算法的所有信息,一切都在源头。 我想要理解的主要是这个问题的答案: 为什么输出有时会改变?如果我运行该程序,60-70%的时间我得到答案14(这应该是错误的),但有些时候我得到奇怪的东西作为结果...为什么? 如果我不断得到不同的结果,我怎么能调试代码....另外,如果我编译发布而不是调试(在debian sid中最新的gcc下运行代码块),我得到大部分时间9作为结果。< / p>

CODE:

#include <iostream>
#include <vector>
/*void print_array
{
    std::cout<<" ( ";
    for (int i = 0; i < n; i++) { std::cout<<array[i]<<" "; }
    std::cout<<")"<<std::endl;
}*/



///this algorithm must take an array of elements and return the maximum achievable sum
///within any of the sub-arrays (or sub-segments) of the array (the sum must be composed of adjacent numbers within the array)

///it will squeeze the array ...(...positive numbers...)(...negative numbers...)(...positive numbers...)...
///into ...(positive number)(negative number)(positive number)...

///then it will 'remove' any negative numbers in case it would be convienent so that the sum between 2 positive numbers
///separated by 1 negative number would result in the highest achievable number, like this:
// -- (3,-4,4) if u do 'remove' the negative number in order to unite the positive ones, i will get 3-4+4=3. So it would
// be better not to remove the negative number, and let 4 be the highest number achievable, without any sums
// -- (3,-1,4) in this case removing -1 will result in 3-1+4=6, 6 is bigger than both 3 and 4, so it would be convienent to remove the
// negative number and sum all of the three up into one number
///so what this step does is shrink the array furthermore if it is possible to 'remove' any negatives in a smart way
///i also make it reiterate for as long as there is no more shrinking available, because if you think about it not always
///can the pc know if, after a shrinking has occured, there are more shrinkings to be done

///then, lastly, it will calculate which of the positive numbers left is highest, and it will choose that as remaining maximum sum :)

///expected result for the array of input, s[], would be (i think), 7

int main() {
const int n=4;
int s[n+1]={3,-2,4,-4,6};
int k[n+1]={0};
///PRINT ARRAY, FOR DEBUG
std::cout<<" ( ";
    for (int i = 0; i <= n; i++) { std::cout<<k[i]<<" "; }
    std::cout<<")"<<std::endl;
int i=0, j=0;
// step 1: compress negative and postive subsegments of array s[] into single numbers within array k[]
/*while (i<=n)
{
    while (s[i]>=0)
    {
        k[j]+=s[i]; ++i;
    }
    ++j;
    while (s[i]<0)
    {
        k[j]+=s[i]; ++i;
    }
    ++j;
}*/

while (i<=n)
{

    while (s[i]>=0)
    {
        if (i>n) break;
        k[j]+=s[i]; ++i;
    }
    ++j;
    while (s[i]<0)
    {
        if (i>n) break;
        k[j]+=s[i]; ++i;
    }
    ++j;
}















std::cout<<"STEP 1 : ";
///PRINT ARRAY, FOR DEBUG
std::cout<<" ( ";
    for (int i = 0; i <= n; i++) { std::cout<<k[i]<<" "; }
    std::cout<<")"<<std::endl;



j=0;
// step 2: remove negative numbers when handy
std::cout<<"checked WRONG! "<<unsigned(k[3])<<std::endl;
int p=1;
while (p!=0)
{
    p=0;
    while (j<=n)
    {
        std::cout<<"checked right! "<<unsigned(k[j+1])<<std::endl;
        if (k[j]<=0) { ++j; continue;}
        if ( k[j]>unsigned(k[j+1]) && k[j+2]>unsigned(k[j+1]) )
        {
            std::cout<<"checked right!"<<std::endl;
            k[j+2]=k[j]+k[j+1]+k[j+2];
            k[j]=0; k[j+1]=0;
            ++p;
        }
        j+=2;
    }
}
std::cout<<"STEP 2 : ";
///PRINT ARRAY, FOR DEBUG
std::cout<<" ( ";
    for (int i = 0; i <= n; i++) { std::cout<<k[i]<<" "; }
    std::cout<<")"<<std::endl;


j=0; i=0; //i will now use "i" and "p" variables for completely different purposes, as not to waste memory
// i will be final value that algorithm needed to find
// p will be a value to put within i if it is the biggest number found yet, it will keep changing as i go through the array....

// step 3: check which positive number is bigger: IT IS THE MAX ACHIEVABLE SUM!!
while (j<=n)
{
    if(k[j]<=0) { ++j; continue; }
    p=k[j]; if (p>i) { std::swap(p,i); }
    j+=2;
}

std::cout<<std::endl<<"MAX ACHIEVABLE SUM WITHIN SUBSEGMENTS OF ARRAY : "<<i<<std::endl;

return 0;
}

可能会有问题,因为我不使用矢量? 谢谢你的帮助!

编辑:我发现了我的算法错误! 一个是用户m24p提到的一个,在算法的第一步中找到,我用一个有点丑陋的周转来修复,以后可以清理...... 另一个是在step2中找到的。似乎在while表达式检查中,我检查了对数组的无符号值的东西,真正检查的是某些奇怪数字的无符号值。 我测试了它,简单的cout输出: 如果我做无符号(k [anyindexofk])并且该点中包含的值是正数,我得到无符号的正数 如果这个数字是负数,那么该值不会简单地无符号,但看起来非常不同,就像我跨过数组或其他东西......我得到这个数字“4294967292”当我反而期望-2返回为2或-4为4。 (该数字为-4,-2给出4294967294)

我用我的新东西编辑了这些来源,感谢您的帮助!

编辑2: nvm我使用c ++的csth libs解决了std :: abs() 没有使用abs会有没有其他方法吗?

1 个答案:

答案 0 :(得分:1)

在您的代码中,您有:

while (s[i]>=0)
{
    k[j]+=s[i]; ++i;
}

其中s被初始化为

int s[n+1]={3,-2,4,-4,6};

这是一个明显的错误。你的while循环将超越数组并点击可能会或可能不会被清零的垃圾数据。没有什么能阻止我大于n + 1。清理代码,以免超出数组,然后尝试调试它。此外,您的问题需要更加具体,让我能够轻松回答您的问题,但修复像我指出的那样的错误应该可以更容易地停止运行到不一致的,未定义的行为并开始关注您的算法。我很乐意回答这个问题,但我无法解析你特别提出的问题或者出了什么问题。