Thread: Recursion problem

  1. #1
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82

    Recursion problem

    I´m having a problem with a recursive function. When the stop condition verifies:
    Code:
    if(j>nodes && source==in)
    return maxpeso;
    i want the function to stop and return to main,where there is printf("%d\n",maxpeso);, which is the result of the recursive function.The problem is,when the condition holds , i want the function to return to main,but the function continues and enter an infinite cycle...How can i stop the execution of the function and return to main just for the printf?

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    You could try the catch to throw you back to main! like do...
    Code:
    #include <stdio.h>
    
    void RecurseFunc()
    {
        static int Beem_me_up_Scotty = 1;
    
        printf("RecurseFunc = %d\n", Beem_me_up_Scotty);
    
        if( Beem_me_up_Scotty++ >=10 )
        {
             throw "Captain, warp core breach in recursion!";
        }
    
        RecurseFunc();
    } 
    
    int main()
    {
        try
        {
             RecurseFunc(); 
        }
        catch(char *rc)
        {
              printf("ERROR: %s\n", rc);
        }
         return 0;
    }
    Last edited by Scarlet7; 04-19-2003 at 01:57 PM.

  3. #3
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    here´s the function:
    Code:
    int maxpeso=0;
    void max_peso(int source, int sink, int nodes,int j)
    {
    	int num;
    	int u=0,n=0;
    	
    	if(j>nodes && source==in)
    	{
    		;
    		
    
    	}
    	else
    	{	
    
    	if(source == sink)
    		poe_na_fila(source);
    	else
    		{
    			if(j> nodes)
    			{
    				percurso[coroa]=0;
    				coroa -=1;
    				max_peso(percurso[coroa],sink,nodes,source+1);
    			}
    			else
    			{
    	
    			if(adj[source][j]==oo)
    				max_peso(source, sink, nodes,j+1);
    			else
    				{
    					poe_na_fila(source);
    					max_peso(j, sink, nodes,0);
    				}
    			}
    			
    		}
    	n=somador(percurso,nodes);
    	if(n>maxpeso)
    		maxpeso=n;
    	coroa=coroa-1;
    	percurso[coroa]=0;
    	coroa=coroa-1;
    	max_peso(percurso[coroa],sink, nodes, source+1);
    	}
    
    }
    main (int argc, char *argv[])
    {
    	int noi,nof,peso,i,u,j,arcos,nos,inicio,fim;
    	inicio=fim=noi=nof=i=j=peso=arcos=nos=u=0;
    	FILE *fp;
    	fp=fopen(argv[1],"r");
    	if (fp==NULL)
    		printf("Impossível abrir o ficheiro %s", argv[1]);
    	else
    		{
    			fscanf(fp,"%d",&nos);
    			fscanf(fp,"%d",&arcos);
    			for(u=0;u<=nos;u++)
    			{
    				percurso[u]=0;
    			}
    			for(i=0; i<=nos;i++)
    			{
    				for(j=0;j<=nos;j++)
    					adj[i][j]=oo;
    			}
    			for(i=0;i<arcos;i++)
    			{
    				
    				fscanf(fp,"%d %d %d", &noi,&nof,&peso);
    				adj[noi][nof]=peso;
    			}
    			fscanf(fp,"%d %d", &inicio, &fim);
    			in=inicio;
    			max_peso(inicio,fim,nos,0);
    		printf("%d\n",maxpeso);
    		}
    }
    I want it to stop at the if(j>nodes && source==in) condition and return to main to do the printf

  4. #4
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    This is probably the best method as it unwinds the stack.
    Code:
    void max_peso(int source, int sink, int nodes,int j)
    {
        if(j>nodes && source==in)
        {
             throw 123;      // error code
        }
    }
    
    main (int argc, char *argv[])
    {
        try
        {
             max_peso(inicio,fim,nos,0);
        }
        catch(int rc)
        {
              printf("max_peso Error code = %d\n",rc);
        }
    }

  5. #5
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    Huuhhh guys...what the heck is that try,catch,throw thing...?How do i change my code to use that?

  6. #6
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    Right...So...How can i solve the problem?Is there any way of stopping the execution of the function..?

  7. #7
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    it worked,thnks!

  8. #8
    scarle7
    Guest
    You could do a returns after max_peso call to stop following lines of code being run before the function ended.
    Code:
    int max_peso(int source, int sink, int nodes,int j)
    {
    	int num;
    	int u=0,n=0;
    	
    	if(j>nodes && source==in)
    	{
    		return 1;
    	}
    	else
    	{	
    
    	if(source == sink)
    		poe_na_fila(source);
    	else
    		{
    			if(j> nodes)
    			{
    				percurso[coroa]=0;
    				coroa -=1;
     				if(max_peso(percurso[coroa],sink,nodes,source+1)) return 1;
    
    			}
    			else
    			{
    	
    			if(adj[source][j]==oo)
    				if(max_peso(source, sink, nodes,j+1)) return 1;
    			else
    				{
    					poe_na_fila(source);
    					if(max_peso(j, sink, nodes,0)) return 1;
    				}
    			}
    			
    		}
    	n=somador(percurso,nodes);
    	if(n>maxpeso)
    		maxpeso=n;
    	coroa=coroa-1;
    	percurso[coroa]=0;
    	coroa=coroa-1;
    	if(max_peso(percurso[coroa],sink, nodes, source+1)) return 1;
    	}
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Recursion problem
    By trnd in forum C Programming
    Replies: 2
    Last Post: 02-01-2009, 03:06 PM
  3. Problem of understanding recursion
    By ixing in forum C Programming
    Replies: 2
    Last Post: 05-02-2004, 03:52 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. recursion problem
    By dustinc in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2001, 04:29 AM