1

I'm trying to recursive function in C that calculates the sum of the numbers from x to max (inclusive). For example, sum (4, 7) would compute 4 + 5 + 6 + 7 and return the value 22. The function code a must be recursive so you are not allowed to use any conventional loop constructs.

I have this and i think it should work but i'm not entirely sure why its not

#include <stdio.h>

int main()
{
int sum (x, max);
   int total, y, x, max;
   if (x<max){
       y=x+1;
       total = x+sum(y,max);
       return total;
    return x;
   }


return 0;
}

Thanks for any help with this in advance!

10
  • 5
    What book are you using that teaches you to define functions like that? Throw it away Commented Mar 4, 2017 at 16:38
  • 1
    This will never work because the function sum is used but not defined. Commented Mar 4, 2017 at 16:39
  • 2
    @PaulOgilvie Unfortunately, successful compiling and linking is required before using a debugger. Commented Mar 4, 2017 at 16:40
  • 1
    Luke, you define functions at the global file scope, not within another function. Commented Mar 4, 2017 at 16:43
  • 1
    @AndersonGreen - The fact the GCC has an extension for nested functions is neither here nor there, since this isn't a function definition, nested or otherwise. Commented Mar 4, 2017 at 16:43

3 Answers 3

1

Here is one possible solution:

#include <stdio.h>

int sum_in_range(int a, int b){
    if(a != b){
        return sum_in_range(a+1,b)+a;
    }
    else{
        return b;
    }
}

int main(void) {
    // your code goes here
    printf("%d",sum_in_range(2,4));
    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

"The function code a must be recursive so you are not allowed to use any conventional loop constructs." - from OP's question.
1
    #include<stdio.h>
#include<stdlib.h>
#include<string.h>

int sum(int s,int max)
{
    if(s==max)
    {
        return s;
    }
    else
    {       
        return(s+sum(s+1,max));
    }
}
int main()
{
    int r,s,max;
    printf("\n enter s and max");
    scanf("%d%d",&s,&max);
    r=sum(s,max);
    printf("%d",r);
}

1 Comment

Yeah ok, that is correct, but I am not sure that OP will not benefit by just copying it without explanation and/or understanding why and how it works
0

I spotted some errors on your code. I'm not a pro yet but here's what I think

I just edit your code. removed, added and rearranged some stuff*/

/*First, let's look at your code*/
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int sum(x, max);//I think what you want to do here is declare a function but instead declaring, you define it here because you added semicolon (;) 
    int total, x, y, max;

    if(x < max)
    {
        y = x + 1;
        total = x + sum(y, max); //you don't have a function declaration for "sum"

        return total;

        return x; //this will never return since you already "return the total before this"
    }

    return 0;
}


//////////////////////////////////////////////////////////////

/*And I think this is what you want to do*/
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x = 4, max = 6; //We declare this here for the "sum" function. This way "sum" function can recognize these variables
    int total = x; //be sure to set the total to x.

    //you can make a void function for this instead of "int". but either way, it can do the job.
    void sum(int y) //no need to pass a "max" because "max" is already recognized by the "sum" function since we declare the variables at the top of "main" function
    {
        if(x < max)//don't make it x <= max. Because of the argument "total = total + (x + 1)" on this function. If you do, the total will exceed.
        {
            //You can see here why we set the value of "total" to x.
            total = total + (x + 1);//And also, you can see why we didn't make the argument in if() statement like this: if(x <= max).
            x++;//increment "x" every loop

            //call the function again and pass the total until x == max.
            sum(total);
        }
    }

    //pass the x
    sum(x);

    //check the answer
    printf("The total is %d\n\n", total);

    return 0;
}

//////////////////////////////////////////////////////////////

/*It looks messy with comments*/
/*Here's the code looks like without a comment.It's pretty short code if you look remove the comments.. LOL..*/
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x = 4, max = 6;
    int total = x;

    void sum(int y) 
    {
        if(x < max)
        {
            total = total + (x + 1);
            x++;

            sum(total);
        }
    }

    sum(x);

    //check the answer
    printf("The total is %d\n\n", total);

    return 0;
}

1 Comment

sorry. I forgot to remove the #include <stdlib.h>.... It's the default in my IDE.. =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.