float f = 0.0;
constint 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
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
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.
#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;
}
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.
#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;
}
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.