0

so i have this function which use recursion to find the longest sequence of numbers. but every time i run it, i get this error message:

Unhandled exception at 0x002871D9 in increasingSeq.exe: 0xC0000005: Access violation writing location 0x01060EC0.

int seqeter(int grid, int startPos,  int seq)
{
    Sequence maxseq;
    maxseq.size = 0;
    for (int rows=0;rows<MAXROWS;rows++)
    {
        for(int cols=0;cols<MAXCOLS;cols++)
        {

            if (grid[startPos.x][startPos.y] < grid[startPos.x+rows][startPos.y+cols])

            {
                return seq;
            }

            else 
            {
                maxseq.list[maxseq.size] = grid[startPos.x][startPos.y];
                maxseq.size ++;
             return generateSeq(grid, startPos, seq);
            }

        }
    }

    return maxseq;
}
3
  • This little code will run to infinity and beyond... the same start position is passed over and over Commented Mar 27, 2013 at 7:02
  • Why is this a recursive function? Commented Mar 27, 2013 at 7:39
  • No, this is not a recursive function. Commented Aug 19, 2015 at 11:54

1 Answer 1

4

You're not advancing from your startPos - the recursion never stops and you quickly crash the stack.

Sign up to request clarification or add additional context in comments.

5 Comments

SO isn't a homework/whatever solving mechanism. Your specific problem has been pointed out. If you need help with algorithm, ask an appropriate question and describe mattgemmell.com/2008/12/08/what-have-you-tried
the above code is what i came up with. but im getting an error. which you say its because im not advancing from my startpos. but can you explain how to advance from my startpos?
You need to change the startPos to reflect your next step before you invoke the recursive call (generateSeq(grid, startPos, seq)).
Add 1 to startPos.x or startPos.y (I'm not sure which -- I don't really understand what your code is supposed to be doing).
One way to learn what is actually going on, is to run this code under a debugger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.