Sum numbers recursively

Discussion in 'C' started by Quark, Dec 13, 2010.

  1. Quark

    Quark New Member

    Joined:
    Dec 13, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I have been brushing up on recursion. I have a typical problem here. I want to sum up all the numbers between two bounds.The end bounds are included in the sum. I have here a small program to the do the above. Except it goes into infiinite loop. Here is the listing:

    Code:
    #include <stdio.h>
    
    int sum(int a,int b)
    {
    	if(a>=b)
    		return 0;
    	result= a+sum(++a,b);
    	return result;
    }
    
    int main()
    {
    	int i,j,r=0;
    	printf("Enter the range: ");
    	scanf("%d %d",&i,&j);
    	printf("The result is :",sum(i,j));
    	getch();
    	return 0;
    }
     
    Last edited by a moderator: Dec 13, 2010
  2. k1e1g1

    k1e1g1 New Member

    Joined:
    Dec 13, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    I'm new to this, but I believe it's because you did not define 'result'. If you were to define it as an int, that may solve your problem.
    Alternately, you could leave result out all together and just
    return a+sum(++a,b);
     
  3. Quark

    Quark New Member

    Joined:
    Dec 13, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Actually I removed the result altogether ......... still the same result.
     
  4. Quark

    Quark New Member

    Joined:
    Dec 13, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    #include <stdio.h>
    #include <conio.h>

    int sum(int a,int b)
    {
    if(a>=b)
    return 0;
    return a+sum(++a,b);
    }

    int main()
    {
    int i,j,r=0;
    printf("Enter the range: ");
    scanf("%d %d",&i,&j);
    printf("The result is :",sum(i,j));
    getch();
    return 0;
    }

    ##########Still loops infinitely##############
     
  5. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    The following code:
    Code:
    return a+sum(++a,b);
    Is your problem. The ++a in this position is undefined.

    Code:
    int temp = sum(++a,b)
    return a+temp;
    See this link: Sequence points

    Jim
     
  6. Quark

    Quark New Member

    Joined:
    Dec 13, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I solved this problem this way.

    int inc(int a)
    {
    return ++a;
    }


    #include <stdio.h>

    int sum(int a,int b)
    {
    if(a>=b)
    return 0;
    result= a+sum(inc(a),b);
    return result;
    }

    int main()
    {
    int i,j,r=0;
    printf("Enter the range: ");
    scanf("%d %d",&i,&j);
    printf("The result is :%d",sum(i,j));
    getch();
    return 0;
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice