non recursive function

Pages: 12
Jan 25, 2010 at 6:53pm
Hi I have to write a non recursive function named 'fracts' which is identical in operation to the recursive function showed below:

1
2
3
4
5
float f (float n);
{
if (n <= 0) return (0);
else return (f(n-1) + 1/n);
}


What I do not understand is how would I make non recursive function. All I need is some tips on how I would do this. Thank you.
Jan 25, 2010 at 7:03pm
1
2
3
4
5
6
7
8
float s = 0;
float n = 5;

while(n)
{
s+= 1/n;
n--;
}


i think it's correct
Last edited on Jan 25, 2010 at 7:45pm
Jan 25, 2010 at 7:15pm
1
2
3
4
5
6
7
8
float f = 0.0;
const int MAXLOOP = 10;  // initial value

for (int i = 0, i < MAXLOOP, i++)
{
    //The magic happens here.   Apply the algoritm above 
    f += .......;
}


Jan 26, 2010 at 2:58am
Okay. Thanks for helping. However, I have a new question about a different problem. I have to write a recursive function, pow, that gets two arguments, a floating point variable b and an
integer p and returns bp.
here is what i have

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include "simpio.h"

double pow(float b, float p);
int main()
{
    float b=2;
    int p=2;
    double power;
    power=pow(b, p);
    printf("%d ", power);
    getchar();
    return 0;
}
double pow(float b, int p)
{
      if (b==0 && p<=0)
      {
               printf("undefined\n");
      }
      else if (p==0 && b!=0)
      {
           printf("1\n");
      }
      else if (p>0)
      {
           printf("%g\n",(pow(b,p-1)));
      }
      else 
      {
          printf("%g\n",(1/pow(b,-p)));
      }
}      
 
Last edited on Jan 26, 2010 at 3:18am
Jan 26, 2010 at 3:52am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
double pow(float b,int p){
       if(p==0)
               return 1;
       else 
              return (b*pow(b,p-1));  
}
main(){
       float num=5,power;
       int exp=3;
       power=pow(num,exp);
       cout<<power;
       system("pause");
}


i change b and p to num and exp in main for more readable code and i used c++ codes anyway you will just covert cout to printf and iostream to stdio.. gudluck
Last edited on Jan 26, 2010 at 3:54am
Jan 26, 2010 at 3:56am
Thank you soooooo much.
Jan 26, 2010 at 4:10am
I am really sorry but how would I make 'p' a negative in the pow program? And with the fracts program I asked about earlier I am having some problems with. The problem is that the program is only printing out '0' im not sure if thats correct.

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

float fracts(float s, float n)
{
    while(n)
    {
    s+= 1/n;
    n--;
    }
}
int main()
{
    
    float s = 0;
    float n = 5;
    fracts(s, n);
    printf("%f ", s);
    getchar();
    return 0;
}


Last edited on Jan 26, 2010 at 4:32am
Jan 26, 2010 at 5:38am
the problem is on line 16, you only pass the values of s and n but never return the result.
here the correct code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>

float fracts(float s, float n)
{
    while(n)
    {
    s+= 1/n;
    n--;
    }
    return s; // return the result
}
int main()
{
    
    float s = 0;
    float n = 5,result;
    result=fracts(s, n);  // put the result in a variable or display it directly
    printf("%f ", result);
    getchar();
    return 0;
}
Jan 26, 2010 at 5:53am
1
2
3
4
5
6
7
8
9
10
double power(double b, int p) {
    if(b < 0) {
        b = 1.0/b;
        b = -b;
    }
    if(p > 0)
        return b*power(b, p-1);
    else
        return 1.0;
}
Jan 26, 2010 at 6:12am
Thank you so much pyschoder and you too blackcoder41. When you run the code is your compiler taking a while to compile the code?
Last edited on Jan 26, 2010 at 6:26am
Jan 26, 2010 at 8:48am
a couple seconds...
Jan 26, 2010 at 9:37am
no.. it runs smooth on my old p4..
Jan 26, 2010 at 10:59am
it didn't
Jan 26, 2010 at 4:55pm
Oh ok. For some reason my compiler takes like 3 minutes to compile the code
Last edited on Jan 26, 2010 at 5:25pm
Jan 26, 2010 at 5:24pm
Either something's very wrong with your compiler, or your computer. I can normally compile over a million lines in a minute or two (counting repeatedly compiled headers from inclusion). Even my old K6-2 took no more than ten seconds to compile a few hundred lines.
Jan 26, 2010 at 5:27pm
Ok.
Last edited on Jan 26, 2010 at 5:41pm
Jan 26, 2010 at 6:17pm
I have one quick question. Is this program recursive?

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;
    }
    s*=2;
    return s;
}
int main()
{
    int n=8;
    int s=0;
    int result;
    result=sum(s, n);
    printf("%d ", result);
    getchar();
    return 0;
}
Jan 26, 2010 at 6:49pm
No, there are no recursive functions in that program.

There is, however, an infinite loop. That while(n) loop will deadlock your program if n is nonzero because you never change n inside the loop.
Jan 26, 2010 at 6:58pm
What should I change in order to make the function recursive? And if i change the
while (n) to a for loop would that be better?
Jan 26, 2010 at 8:44pm
What should I change in order to make the function recursive?


A recursive function calls itself. Your first post in this thread is an example of a recursive function because f() calls f() in order to get output.

In your latest post, sum() does not call sum() and is therefore not recursive.

And if i change the
while (n) to a for loop would that be better?


Probably. I haven't really been paying attention to this thread so I don't exactly know what you're trying to do (I'm kind of lazy right now). But your while(n) loop is definately ill formed.
Pages: 12