samedi 4 mars 2017

Writing a recursive function in C

I am studying how to program and have recently been working on a problem that calculates the total of 2 entered numbers from min to max. For example, if someone entered the numbers 4, 7. The calculation would be 4+5+6+7=22.

I've attempted what i think would be the definition of recSum but obviously it is wrong as I get a segmentation fault. What is wrong with my definition?

int recSum(int x, int max)
{
    int incrementalSum = 0;
    recCall: if (x < max)
    return incrementalSum;
    incrementalSum = x + x++;
    printf("\n\nThe 2 values are %d & %d \nThe recursive sum is:
    %d\n\n", x, max, incrementalSum);
    goto recCall;
}

Aucun commentaire:

Enregistrer un commentaire