Thread: bugs and recursion

  1. #1
    Unregistered
    Guest

    bugs and recursion

    Im having trouble figuring this problem out. I need to be able to make this function recursive and there is also another bug in here can anybody try it and help me fix it. Any help would be would work thanx. Heres my source code.
    #include <stdio.h>

    int main (void)
    {
    int x;
    int y;
    int n;

    printf("enter your two numbers:\n");
    scanf("%d%d",&x,&y);

    if(y==0)
    printf("your number is:%d",x);
    else if (x<y)
    {n=x;
    x=y;
    y=n;}
    printf("your number arge:%d%d",x,y);
    else
    {n=x;
    x=y;
    y=n%y;}
    printf("your numbers are %d%d",x,y);

    return 0;
    }

  2. #2
    Registered User lliero's Avatar
    Join Date
    Oct 2001
    Posts
    59

    wher?

    im sorry i did not get you right...
    where is that function?

    maybe prelude can comment on that
    " programming is 1% syntax and 99% logic "

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    I couldn't even get it to compile.

    : : edit : :
    I think Prelude might be busy. It's a slim possibility.
    Last edited by Shadow; 04-25-2002 at 11:36 AM.
    The world is waiting. I must leave you now.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >and there is also another bug in here
    You have a little problem matching your else with the rest of the conditional statements. Here:
    Code:
      y=n;
    } 
    printf("your number arge:%d%d",x,y); 
    else
    The printf call ends the first series of tests, so the else after that is illegal. This may not be what you wanted, your intentions are a bit confusing in this program:
    Code:
    #include <stdio.h> 
    
    int main (void) 
    { 
      int x; 
      int y; 
      int n; 
    
      printf("enter your two numbers: "); 
      scanf("%d %d",&x,&y); 
    
      if(y==0) 
        printf("your number is:%d\n",x); 
      else if (x<y) {
        n=x; 
        x=y; 
        y=n;
      }  
      else {
        n=x; 
        x=y; 
        y=n%y;
      } 
      printf("your numbers are %d %d\n",x,y); 
      return 0; 
    }
    >I need to be able to make this function recursive
    You don't want to call main recursively, is there another function you were talking about? Because as it is this program really wouldn't benefit from recursion.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    What exactly do you mean with 'recursive'?
    Do you mean I want to repeat entering numbers?
    Code:
    int main()
    {
        while(scanf(%d%d))
        {
            /* do your stuff */
        }
        return 0;
    }

  6. #6
    Registered User lliero's Avatar
    Join Date
    Oct 2001
    Posts
    59

    oh no

    you gotta read real books i think


    recursion,

    it is a function calling itself. recursion can be use to get a factorial of a number... i cant go into detail right now... please do read your book...
    " programming is 1% syntax and 99% logic "

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    I know what recursion means (have some experience with Miranda and Prolog). I was just wondering if he knows. Couldn't find any recursion in his code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM