Thread: Strange function output.

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    7

    Strange function output.

    I'm just doing a simple excercise today and I wrote this fuction--ALG--And I'm getting the weirdest outputs. I have taken the liberty to insert a printf right before the return, and to my confusion it prints the right awnser however one that differs with my real output.

    Code:
    #include <stdio.h>
    
        int alg( int n, int cnt )	{
       	
          if( n == 1 )	{
          	
          	//dismay
             printf("%d", cnt);
             return cnt;
             
          }
          
          else if ( n%2 == 0 )
             alg( n/2, ++cnt);
                
          else 
             alg( (3*n)+1, ++cnt);
       
       }
    
        int main()	{
       
          printf(" %d ", alg(10, 1) );
          return 0;
       
       }
    Btw, I use to have this real handy dandy visual debugger in my jgrasp/cygwin superhero combo plus but ever since I updated cygwin it went away. If anyone can point out that package to me it would be great. Thx.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Make sure you return a value from all branches of the if tree.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    7
    Brilliant!

    Code:
    #include <stdio.h>
    
        int alg( int n, int cnt )	{
       	
          if( n == 1 )
             return cnt;
             
          else if ( n%2 == 0 )
             return alg( n/2, ++cnt);
                
          else 
             return alg( (3*n)+1, ++cnt);
       
       }
    
        int main()	{
       
          printf(" %d ", alg(10, 1) );
          return 0;
       
       }
    Thank you very much. Now if I could only find that visual debugger!

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 11:20 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM