-1

I am trying to find the 1D peak through Divide and Conquer technique in this particular question, my program even though it runs, but at the time of giving the final output it says that there has been some problem with the execution, I have got the answer from a different method, but I would like to know where am I at fault here.

#include<iostream>

using namespace std;
int a[8];

class pg1
{
public:
    int func(int n)
    {
        if(a[n] <= a[n+1])
        {
            func(n++);
        }
        else if(a[n] <=a [n-1])
        {
            func(n--);
        }
        else
        {
            return n;
        }
    }
};

int main()
{   
    pg1 ob;
    for(int i=0;i<8;i++)
    {
        cin >> a[i];
    }
    int x = ob.func(4);
    cout << endl << x;
    return 0;
}

Input- 5 6 8 5 4 3 6 4

Errors are- 1D Peak.exe has stopped working. A problem caused the program to stop working correctly.Windows will close the program and notify you aif a solution is available.

End Result- Process Exited with return value 3221225725

Dhruv K
  • 55
  • 4
  • 1
    It may also be helpful if you include the error message that you are receiving at run time. – user700390 Jun 03 '15 at 16:09
  • 1
    In `func` you have statements such as `if(a[n] <= a[n+1])` but no check is made if any of these are actually valid array index values. But without seeing your error message it's hard to say if this is the problem. Please include any error output you are getting by editing your question to include it. – shuttle87 Jun 03 '15 at 16:11

1 Answers1

2

Don't use postincrement and similar in function calls.

Here's the problem condensed down to a really simple piece of code

#include <iostream>

int test(int n){
    if(n == 1){
        std::cout << "Function called!";
        return test(n++);
    }else{
        return 0;
    }
}

int main() {
    test(1);
    return 0;
}

Before you run this, ask yourself what you expect to happen here. Did it do what you thought?

When you run this you'll see that the code doesn't terminate properly. The output shows the function gets called infinitely many times, eventually the stack runs out of space and the program crashes.

You can see this code in action here: http://ideone.com/QL0jCP

In your program you have the same problem:

int func(int n)// say n = 4
{
    if(a[n] <= a[n+1])//say this is true
    {
        func(n++); //this calls func(4) THEN increments n afterwards
    }

This calls func with the same value over and over.

The solution is to not use postincrement or postdecrement in your function calls. These create hard to diagnose bugs as you have seen in this question. Just a simple func(n+1) is all you need. If you needed to use the variable later then just create an explicit variable to do that, it's much cleaner coding style (as this problem you ran into here shows).

After you fix this you'll need to fix your array bounds checking.

if(a[n] <= a[n+1])

If n is the last spot in the array you suddenly are trying to access one place past the end of the array, if you are lucky you get a segfault and a crash, if you are unlucky you get some bug that messes up your system that is hard to find. You want to check the values are valid.

shuttle87
  • 15,466
  • 11
  • 77
  • 106