-1

I have a question about debug\ release mode in visual studio.

I wrote a recursive program that gets an array of integers (maximum size of 20 cells) and rearrange it by that rules:

The first numbers are prime numbers, after that (non prime numbers), numbers which divisible by 2 and then numbers which divisible by 3, and then by 5, by 7, by 11 and so on...

I don't know why, but when the program runs on debug mode, it finishes successfully and immediately. but on release mode it takes about 3.5 minutes. why is the big difference?

I don't know if the problem is inefficiency or something else. If the code is too long to understand, it's okay, I will be happy just for guidance.

This is the code:

int main()
{
    printf("What is the size of the array?\n");
    scanf("%d",&arrySize);
    printf("Enter %d integers\n",arrySize);
    inputArry(arry,arrySize,0);

    arrangeArry(arry,1,arrySize);

    printArry(arry,0,arrySize);

    main();
    return 0;
}

//insert numbers to array
void inputArry (int arry[], int size, int index)  
{
    if (index>size-1)
        return;

    scanf("%d",&arry[index]);

    inputArry(arry,size,index+1);
}

void arrangeArry (int arry[], int index, int size) // Arrange array
{
    if (index >=size)
        return;

    arrangeSubArry(arry,index);

    arrangeArry(arry,index+1,size);
}

// Given arranged array except the last number, this
//   function arranges the array including the last number
void arrangeSubArry (int arry[], int index) 
{
    int temp=0;

    if (index==0)
        return;

    if (dividedBy(arry[index],2)<dividedBy(arry[index-1],2))
    {
        temp=arry[index];
        arry[index]=arry[index-1];
        arry[index-1]=temp;

        arrangeSubArry(arry,index-1);
    }
}

// Gives the first prime number that int num is divided by
int dividedBy (int num, int counter) 
{
    if (num==1||num==0||isPrime(num,sqrt(num))==1)
        return 1;
    if (isPrime(counter,sqrt(counter))==1)
    {
        if (num%counter==0)
            return counter;
    }

    dividedBy(num, counter+1);
}
jww
  • 97,681
  • 90
  • 411
  • 885
user1673206
  • 1,671
  • 1
  • 23
  • 43

2 Answers2

2

I have a question about debug\ release mode in visual studio.

Its actually not related to debug or release builds.


I wrote a recursive program that gets an array of integers

There is no base case. Something needs to stop the recursion.


int main()
{
    ...

    main();
    return 0;
}

Calling main in a C++ program is undefined behavior. See Can main function call itself in C++?. Once the program is illegal, anything can happen and its all OK.

Watch out for demons flying out of your nose.

Community
  • 1
  • 1
jww
  • 97,681
  • 90
  • 411
  • 885
0

I can't see any way the recursive execution of main will ever end without, for example, a segfault.

Recursive algorithm always should define exit condition:

int main(void) {
  /* code */
  if(/* condition */) {
    main();
  }

  /* So, we have our chances to return from main */
  return 0;
}

But do not call main that way. Wrap main body into another function and call it instead.

And forgive me, just as you noticed, the "code is too long to understand", but the answer for your question: deal with infinite calls to main.