non recursive function

Pages: 12
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.
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
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 += .......;
}


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
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
Thank you soooooo much.
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
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;
}
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;
}
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
a couple seconds...
no.. it runs smooth on my old p4..
it didn't
Oh ok. For some reason my compiler takes like 3 minutes to compile the code
Last edited on
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.
Ok.
Last edited on
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;
}
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.
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?
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