954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

series of 1 + 1/2 + 1/3 ...etc

I need some help: how can I do this:

Write a program to find out how many terms of the series:

1 + 1/2 + 1/3 + 1/4 +.... are needed before the total first exceeds 5.

far
Newbie Poster
2 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

The first step to figuring out how to write the program is figuring out how to write the algorithm. How would you solve this problem with pencil and paper? Where would you start? What steps would you take?

cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

Use floating point, otherwise the answer is 1.
[indent]1 + 1/2.0 + 1/3.0 + 1/4.0[/indent]

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

u need an array
float x[20];
// to enter the codes now.use a for loop
int i,y=1;
for (i=0;i<20,i++)

x[i]=1/y+1/y+1;


// to calculate total
for(i=0;i<20;i++)
total=x[i]+x[i+1];
if (total>5) num=x[i]// declare num first somewhere in the program
// how many terms needed

of course this is not the absolute code but i guess you are intellighent enough to go ahead
before writing any program do it on paper first
get the logic first then use your computer
:lol: :cool:

anastacia
Junior Poster
142 posts since Nov 2004
Reputation Points: 11
Solved Threads: 1
 

>u need an array

No, you don't. In fact, it would be better not to.

>int i,y=1;
>for (i=0;i<20,i++)
>x[i]=1/y+1/y+1;

Reread my previous post.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

well dave please clarify the reason. coz i am not an expert in c++. thanks in advance

anastacia
Junior Poster
142 posts since Nov 2004
Reputation Points: 11
Solved Threads: 1
 

all you need is a loop, a float to hold the result, and a loopcounter :)

I wrote the code in about 3 minutes, including the time to start my editor and the time to compile it.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
well dave please clarify the reason. coz i am not an expert in c++. thanks in advance


The array? Because then you limit yourself to the array size. Without using an array I found that it took about 84 iterations to pass 5. So an array of 20 would not have been adequate.

Or the integer math? In integer math,1 / 2 = 0. But floating point math, 1.0 / 2 = 0.5. If you take the integer result 0 and (implicitly) convert to floating point, you will have 0.0. You can see how this would seriously affect the calculation.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

yeah sort of understanding a little bit. jwenting said something about a counter can you please clarify that ... point.
thnx in advance

anastacia
Junior Poster
142 posts since Nov 2004
Reputation Points: 11
Solved Threads: 1
 
yeah sort of understanding a little bit. jwenting said something about a counter can you please clarify that ... point. thnx in advance

Note the denominator: 1, 2, 3, ... Seems a perfect candidate for a counter.This is hard not to completely give away since it's only about 4 lines of code -- why not post an attempt?

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

ok i will try but not now. i nedd if some more time to figure it out
else i guess i only have to run the bulldozer somewhere out there!!!!

anastacia
Junior Poster
142 posts since Nov 2004
Reputation Points: 11
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You