Search code examples
c++recursionstack-overflowunhandled-exception

Why do I get "unhandled exception" error and how can I fix it?


I am trying to write a code that uses 2 recursive functions; 'I' and 'U' and one non-recursive function 'f'. What I am tring to achive is to run the recursive function I "steps1" many times and then stop at this level to then run the recursive funtion U "steps2" many times. After that, finally run the non-recursive function f at the level where function U's iteration ends.

For example:

Let steps1=1 and steps2=1 then,

I will iterate function 'I', 1-time(steps1) and get:

   I(n)= 3*I(n/2)+7*n-3  

then, I will iterate funtion U, 1-time(steps2) for n/2 value. And then, insert it instead of I(n/2), thus I will calculate:

    I(n)= 3*[U(n/2)]+7*n-3= 3*[2*U(n/6)+2*(n/2)-9] = 3*2*U(n/6)+3*2*(n/2)-3*9 

now insert last function f(n/6), into this equation:

        3*2*U(n/6)+3*2*(n/2)-3*9=3*2*f(n/6)+3*2*(n/2)-3*9 

since f is non-recursive, this will give me the result.

When I run my code I get, "unhandeled exception" error. Can someone help me find the reason for this error? Is my code wrong at somewhere? Can someone help me fix this please? I am not sure if my code does exactly what I want to do, as well?

#include<stdlib.h>
#include<bits/stdc++.h> 
using namespace std; 


int f(int n) 
{ 
   return (n-1)*(n-1);

} 

 /* step2 many iteration of the function U and then function f */
int U(int n , int steps2, int counter2=0) 
{ 

   if(counter2==steps2){
        return f(n);
      } 

    return 2*U(n/3, steps2, counter2+1)+2*n-9; 

} 

/* step1 many iteration of  the function I and then function U*/
int I(int n , int steps1,int steps2, int counter1=0, int counter2=0) 
{ 

   if(counter1==steps1){
        return U(n,steps2,counter2);
      } 

    return 3*I(n/2, steps1, counter1+1)+7*n-3; 

} 


int main(){

    int n, steps1,steps2;
   cout<< " Enter 'n' value which is divisable by both 2 and 3"<<"\n";
   cin>>n;
    cout<< " Enter iteration count for I"<<"\n";
   cin>>steps1;
    cout<< " Enter iteration count for U"<<"\n";
   cin>>steps2;
   cout<< " result:" << I(n,steps1,steps2)<<"\n";

     getchar(); 
     return 0; 

 }

Solution

  • I compiled and ran your program and it looks like you're getting a stack overflow. The recursion for function I isn't correct. Namely your base case will never be reached. In each location that I is called you only pass 3 parameters, thus counter1 is always going to have a value of 0, the default value. Also, I is always called such that steps1 is always going to have the same value (from the user's input). So if(counter1==steps1){ will never be true.

    Some advice for future problems, when troubleshooting a problem like this, one of the easiest things you can do is to add a cout to the beginning of each function. Print the function name and the parameter values. Another option is to hook up a debugger and set some break points. Learning how to use a debugger with C++ will come in very, very handy.