0

I have this code which find longest nondecreasing sequence in array and print length of this sequence:

int max = 0;

void sequence(int *array, int start, int end, int last, int count)
{
   int i;
   if (start >= end)
   {
       if (count > max) max = count;
   }
   else
       for (i = start; i < end; i++)
       {
            if (array[i] >= last)
                sequence(array, i + 1, end, array[i], count + 1);
            else 
                if (i == end - 1 && array[i] < last)
                    sequence(array, i + 1, end, 0, count);
        }
}

void main()
{
    int x[] = { 1, 2 };
    sequence(x, 0, 2, 0, 0);
    printf("%d", max);
}

Everything is OK in my Visual Studio, but Ideone print Runtime error time: 0 memory: 2160 signal:-1

Johny
  • 239
  • 2
  • 10
  • 4
    Possibly because you declared `main` wrong. It should return an `int`. – user253751 Nov 23 '15 at 22:16
  • 1
    The program in that ideone link does not show any runtime error. At least when I just looked at it. – kaylum Nov 23 '15 at 22:24
  • You have a recursive function. Please examine very carefully the conditions which causes it blows up the computer, and ensure you have conditions which end the recursion. – Weather Vane Nov 23 '15 at 22:29
  • @AntoineMathys I think his function will have at most N^2 levels of recursion. Since `N=2`, that shouldn't cause an overflow. – Barmar Nov 23 '15 at 22:57

1 Answers1

1

As it's already said, the reason is your main declaration.

The returned value of main indicates the exit code of program. The right declaration is

int main()

or

int main(int argc, char** argv)

The declaration like

void main()

is mistaken but it's ignored by C (such declaration is prohibited in C++). In this case the returned value is unspecified.

You can read more there:

What should main return in C and C++

Discussion on Quora

This problem is looks like yours:

Similar problem discussed on StackOverflow

Also I tested Ideone a little and it seems that any C program with void main() get Runtime Error on Ideone.

Community
  • 1
  • 1
Edgar Rokjān
  • 17,245
  • 4
  • 40
  • 67