write a recursive function called sumover


write a recursive function called sumover that has one argument n which is an unsigned integer. the function returns double value which is the sum of reciprocals of the first n positive integers =.
for example sumover 1 returns 1.0
sumover 2 returns 1.5 like 1/1+1/2
Did you have an actual question about the assignment?
how to write this code ?? couldnt figur out
Don't you have at least some idea? You should try something yourself then ask questions about what you tried, not just ask others to do it for you.
{
if (n < 0)
{
cout << "no negative number" << endl;
}

//write recursive
if (n == 1)
{
return 1;
}
else
{
for (int i = 1/n; i <= 5; i++)
cout << i << endl;
}
return ((1/n) + (1/n));//recursive call
}
> write a recursive function called sumover that has one argument n which is an unsigned integer.
> the function returns double
double sumover( unsigned int n )


> return ((1/n) + (1/n));//recursive call
there isn't even a function call there
1
2
3
4
5
6
7
double sumOver(unsigned int n)
{
	if(n == 0)
		return 0;
	else
		return (1.0f / n) + sumOver(n - 1);
}

That should work, I think it's something like what jay7390 was going for.
return (1.0f / n) + sumOver(n - 1);

@Mike5424,

Why did you use 1.0f? Since the function you are defining returns a double, you probably want to use a double. The value 1.0 is a double. The value 1.0f is a float.

If you use 1.0f, the program will perform float division before adding it to the result of sumOver(n - 1). In a toy program like this, it probably doesn't matter, but if you want the accuracy of the double type, all of the calculations should use doubles.

Also, for clarity it would be nice for line 4 to return 0.0. That makes it obvious that a double value is being returned.
Last edited on
I used 1.0f out of habit, I don't often use doubles. The code wasn't supposed to be perfect, it's just a quick snippet to point jay in the right direction.
> That makes it obvious that a double value is being returned.
double sumOver(unsigned int n)
¿what else could it be?
¿what else could it be?


Obviously it can only be a double because that's what the compiler casts it to. I was making a point about clarity (really a style comment), not about correctness. The code return 0; can be used for ints, floats, doubles, longs, shorts, pointers, unsigned and signed, etc. Providing a constant of the proper type is a way of telling code reviewers "yes, I meant to return a double with value 0.0".

If you know you are returning a double, writing return 0.0; provides a constant of the type being returned, and the compiler does not have to cast the value to another type before returning it. At our company, a code inspection comment like this would be perfectly appropriate, and the change would most likely be made to enhance readability and maintainability.

By the way, does the following code look OK to you? It has the same clarity issues, just in reverse.

1
2
3
4
int myFunction
{
    return 0.0;
}

thank you all :))
Topic archived. No new replies allowed.