non recursive function

Pages: 12
infinite loop
Ok. I tried to make the program recursive. I hope it's right. :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

int sum(int n, int s)
{
    
    while (n)
    {
          s+=n/n+1;
          n--;
    }
    return s;
}
int main()
{
    int n=8;
    int s=0;
    int result;
    result=sum(n, s);
    printf("%d ", result);
    getchar();
    return 0;
}
still no. tell me what you function should do then i'll help you with that.
Im supposed to write a recursive function that takes an integer n as a parameter and returns the sum of the first n elements in the following sequence,
½ + 2/3 + ¾ …. + n/n+1

The name of the function should be sum. I am also supposed to include a call to this function from main, with n=8 and then print the result in main.
http://en.wikipedia.org/wiki/Recursion

Example:
1
2
3
int fact(int n){
    return (n==1)?1:n*fact(n-1);
}
I am really sorry but I'm still not sure why the program isn't recursive.
A recursive function calls itself.


Look at your original post:

1
2
3
4
5
float f (float n)   //  <- see the function named 'f'
{
    if (n <= 0) return (0);
    else return (f(n-1) + 1/n);  // <- see how it's called here?
}


Because 'f' calls 'f' from within itself, it makes this function recursive.

Look at helios' example:

1
2
3
int fact(int n){  // 'fact'
    return (n==1)?1:n*fact(n-1);  // calling 'fact'
}


Now look at your latest post:

1
2
3
4
5
6
7
8
9
10
int sum(int n, int s)  // sum
{
    
    while (n)  // but nowhere in here are you calling 'sum'
    {
          s+=n/n+1;
          n--;
    }
    return s;
}


Since the function is not calling itself, it is not recursive.
Last edited on
sorry late reply.. Disch already told you that a recursive function calls itself.

in your latest post, does it returns the correct value that you expect?

if you're sure about this n/n+1 that will always give you 2 since division will be done first, if it is n/(n+1) that will always give you 0

i think what you're trying to do is to increment the denominator in each iteration, is that right?
hey guys, how do you understand this example?
½ + 2/3 + ¾ …. + n/n+1


is this correct?
1
2
3
4
5
6
7
int sum(int n, int d=1) {
    if(n) {
      int s = n/d;
      cout << s << endl;
      return s+sum(s, d+1);
    } else return 0;
}
use float or double instead of int, remember you are dividing numbers unless that you only want whole numbers of result
is this a sol'n? LOL
1
2
3
4
5
6
7
float sum(float x, float n=1,float d=2) {
    if(x) {
      float s = n/d;
      cout << n <<"/"<<d<<" ="<<s<< endl;       //comment this out if you dont want to display
      return s+sum(x-1,n+1, d+1);
    } else return 0;
}
use float or double instead of int, remember you are dividing numbers unless that you only want whole numbers of result
haha. i was expecting someone would say that :p
Ok I think i made the program recursive. but im getting errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

int sum(int n, int s)
{
    while(n)
    {
    s+=sum(n/n+1);
    n--;
    }
    return s;
}
int main()
{
    int n = 8;
    int s=0, result;
    result=sum(n, s);
    printf("%d ", result);
    getchar();
    return 0;
}

          
Last edited on
The errors are gone . However, the program isn't printing out the correct numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int sum(int n, int s)
{
    if(n)
    {
    s+=sum(s,n/n+1);
    n--;
    }
}
int main()
{
    int n = 8;
    int s=0, result;
    result=sum(n, s);
    printf("%d ", result);
    getchar();
    return 0;
}


it must have a return value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

float sum(float n,float s){
    if(n) {
      s = n/(n+1);
      return s+sum(n-1,s);
    } else return 0;
}

int main()
{
    float n = 4;
    float s=0, result;
    result=sum(n, s);
    printf("%f ", result);
    getchar();
    return 0;
}

and use float or double because you are dividing numbers
Thank you so much I figured out the program.
Topic archived. No new replies allowed.
Pages: 12