Johny
Johny

Reputation: 239

C Recursion runtime error

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

Upvotes: 0

Views: 426

Answers (1)

Edgar Rokjān
Edgar Rokjān

Reputation: 17483

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.

Upvotes: 1

Related Questions