-
c++
I'm suppose to write a program to compute the value of the infinite series 1+1/2+1/3+1/4+1/5.......
here is what I have so far, but of course it doesn't work
#include<stdio.h>
#include<math.h>
double sum_up(epsilon)
double epsilon;
{
int i,sum;
double oldvalue;
sum=0.0;
oldvalue=epsilon+1;
for (i=0; sum_oldvalue> epsilon; i++)
{
oldvalue = sum;
sum += 1/i;
return 0;
}
}
-
Let's analyze this code line by line shall we? :D
Code:
double sum_up(epsilon)
Epsilon is not a data type, and you must supply a data type for any function declarations.
An integer is a whole number. It has no decimal places. With this said, you would define it "sum=0;".
Code:
oldvalue=epsilon+1;
You never assigned a value to the "epsilon" variable.
Another thing, you are missing the main() function. I hope that all this was helpful. I think I've got all the problems solved, if there isn't please post the problem stated by the compiler. :D
-
Hi,
I find some problems in your code
1.Division by Zero (sum+=1/i) i will be Zero at first run in for loop.
2.Without proceeding further returning from for loop which ofcourse eliminates the purpose of loop.
3.sum is declared as int but you are assinging 0.0 (float) to sum.
The below is the simplified version
/*** Code to Calculate the value of 1+1/2+1/3+1/4+1/5....... *****/
#include<stdio.h>
#include<math.h>
double sum_up(int no_of_terms)
{
double sum;
for (int i=1; i <= no_of_terms; i++)
{
sum += 1/i;
}
return sum;
}
by
A.Sebasti....