problem with recurse pro

This is a discussion on problem with recurse pro within the C Programming forums, part of the General Programming Boards category; Code: I have some problem with a small recurse program,debug is OK,but the running result is incorrect.The function of the ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    9

    problem with recurse pro

    Code:
       I have some problem with a  small recurse program,debug is OK,but the running result is  incorrect.The function of the program is to calculate a total  addition result of the variable x, may be there are some problem with the semantic, can any friend  help me?Thanks very much!
        
                         #include<stdio.h>
                          int test(int sum)     
                         {int x;
                          scanf("%d",&x);
                          if(x==0) sum=0;
                          else{test(sum);sum+=x;}
                          return(sum);
                           }
                   
                         int main()
                        {int t,s;
                         s=test(t);
                         printf("%d",s);
                        return 0;
                          }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why aren't you keeping track of the return value of your function, inside the function?
    Code:
    else{test(sum);sum+=x;}
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    9
    Code:
    well, I first make it like this:              
                #include<stdio.h>
                          int test(int sum)     
                         {int x;
                          scanf("%d",&x);
                          if(x==0) sum=0;
                          else{test(sum);sum+=x;}
                          printf("%d",sum);
                           }
                   
                         int main()
                        {int t;
                         test(t);
                         return 0;
                          }
     
    the running result is totally wrong.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    9
    Code:
            OK,I have make it.Change one sentence.And here it is:
             
              #include<stdio.h>
               int test(int sum)
               {int x;
               scanf("%d",&x);
                 if(x==0) sum=0;
                 else{sum=test(sum)+x;}
                return(sum);
                 }
    
                 int main()
                {int t,s;
                 s=test(t);
                 printf("%d",s);
                 return 0;
                  }

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I think your main problem is you're passing 't' to test function and you haven't initialized it in main. So, for the first time when t gets passed it'll contain garbage value.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  3. Possible Windows MDAC problem
    By BobS0327 in forum Tech Board
    Replies: 3
    Last Post: 06-28-2006, 05:57 AM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21