Jump to content

recursion not returning anything

goatedpenguin
Go to solution Solved by trag1c,

The problem is your mathematical function is not recursive or iterative, at least in this form.聽 if you want the sum of 5 consecutive terms, you would substitute n = 5 so it would be

x = n(n+1)/2

x = (5)(5+1)/2

x = 15

There is no recursion or iteration when using that formula. If you want to calculate the sum recursively for positive integers you would do the following:

#include <cstdio>

int sum(int n);

int main(){

        int n = 0;
        printf("Enter a number to calculate the sum of all the number from your input: ");
        scanf("%d", &n);
        printf("sum of %d terms is, %d\n", n, sum(n));

        return 0;
}


int sum(int n){

        if(n < 2 && n >= 0) { // sum of 1 or sum of 0 terms is 1 or 0 respectively
                return n;
        } else if (n < 0) { // positive ints only
                return 0;
        }

        return n + sum(n - 1);
}

edit:

I would also add that your original code should segfault (attempt to write an illegal memory address) due to the fact that there is no exit condition that breaks the recursion. As such your stack will just keep on growing with every stack frame of the recursive function. Linux should report this, no idea about any other platform.

so I have this simple c program(down below) but the code does not generate any errors and executes fine however the base cases nor the recursive function return anything and I can't figure out why, thanks in advance 馃檪

#include <stdio.h>

int sum(int n);

int main(){
	
	int n = 0;
	printf("Enter a number to calculate the sum of all the number from your input: ");
	scanf("%d", &n);
	sum(n);

	return 0;
}


int sum(int n){
	
	if(n == 0){
		return 0;
	}
	else if(n == 1){
		return 1;
	}
	//formula for find the sum of the given n number: (N * (N + 1) ) /2
	else{
		int formula = n * (n + 1)/2;
		return sum(formula); 
	}
}

Link to comment
Share on other sites

Link to post
Share on other sites

Are you certain the recursion ever ends? You might be stuck in infinite calculation. Also what do you expect to happen? The returned value is not stored or printed anywhere, it's just lost as soon as the program ends, so you're not going to see anything.

Link to comment
Share on other sites

Link to post
Share on other sites

The problem is your mathematical function is not recursive or iterative, at least in this form.聽 if you want the sum of 5 consecutive terms, you would substitute n = 5 so it would be

x = n(n+1)/2

x = (5)(5+1)/2

x = 15

There is no recursion or iteration when using that formula. If you want to calculate the sum recursively for positive integers you would do the following:

#include <cstdio>

int sum(int n);

int main(){

        int n = 0;
        printf("Enter a number to calculate the sum of all the number from your input: ");
        scanf("%d", &n);
        printf("sum of %d terms is, %d\n", n, sum(n));

        return 0;
}


int sum(int n){

        if(n < 2 && n >= 0) { // sum of 1 or sum of 0 terms is 1 or 0 respectively
                return n;
        } else if (n < 0) { // positive ints only
                return 0;
        }

        return n + sum(n - 1);
}

edit:

I would also add that your original code should segfault (attempt to write an illegal memory address) due to the fact that there is no exit condition that breaks the recursion. As such your stack will just keep on growing with every stack frame of the recursive function. Linux should report this, no idea about any other platform.

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95,聽Storage:聽Intel 730 Series 480GB SSD,聽WD 1.5TB Black

Display:聽BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

26 minutes ago, trag1c said:

The problem is your mathematical function is not recursive or iterative, at least in this form.聽 if you want the sum of 5 consecutive terms, you would substitute n = 5 so it would be

x = n(n+1)/2

x = (5)(5+1)/2

x = 15

There is no recursion or iteration when using that formula. If you want to calculate the sum recursively for positive integers you would do the following:

#include <cstdio>

int sum(int n);

int main(){

        int n = 0;
        printf("Enter a number to calculate the sum of all the number from your input: ");
        scanf("%d", &n);
        printf("sum of %d terms is, %d\n", n, sum(n));

        return 0;
}


int sum(int n){

        if(n < 2 && n >= 0) { // sum of 1 or sum of 0 terms is 1 or 0 respectively
                return n;
        } else if (n < 0) { // positive ints only
                return 0;
        }

        return n + sum(n - 1);
}

edit:

I would also add that your original code should segfault (attempt to write an illegal memory address) due to the fact that there is no exit condition that breaks the recursion. As such your stack will just keep on growing with every stack frame of the recursive function. Linux should report this, no idea about any other platform.

thanks for the help and yes linux does report this but right now this code was ran on a windows system 馃檪

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, trag1c said:

I would also add that your original code should segfault (attempt to write an illegal memory address) due to the fact that there is no exit condition that breaks the recursion. As such your stack will just keep on growing with every stack frame of the recursive function. Linux should report this, no idea about any other platform.

Well I'd argue that there is an exit condition; the issue becomes the conditions are never met as any positive integer will grow n.

To @goatedpenguin overall recursion, while handy in instances should be carefully thought about.

The key is that you should always be thinking what will be happening to the variables passed.聽 As an example in this case, you should ask will n be always decreasing...in this case the answer would have been no.

Actually, when you look at code like this and are trying to figure out what's going on; it's sometimes handy to manually walk through things with a simple example

Like saying sum(2); you would see formula = 2 * (2 + 1) / 2 = 3...which would then call sum(3)...and soon you would realize that it quickly escalates...to the point of never returning.聽 Ultimately ask yourself, will this ever end; and always try proving to yourself that the exit condition must eventually be satisfied (i.e. that the problem with each call it itself will be reduced)

Overall though, if there is an easy way to implement a function without recursion you should take it...recursion other than being able to easier understand it in some cases makes things very inefficient (and in cases like this can cause crashes if you aren't careful to make sure it exits the recursion correctly)

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

I understand your point I just found a project online to use recursion using a for loop may have been better in this case however I just wanted to practice recursion which I am still don鈥檛 really understand. Thanks for all the advice it really helps 馃檪

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now