943,513 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3199
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 5th, 2004
0

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

Expand Post »
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.
Similar Threads
far
Reputation Points: 10
Solved Threads: 0
Newbie Poster
far is offline Offline
2 posts
since Oct 2004
Dec 5th, 2004
0

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

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?
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is online now Online
13,645 posts
since Feb 2002
Dec 5th, 2004
0

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

Use floating point, otherwise the answer is 1.
1 + 1/2.0 + 1/3.0 + 1/4.0
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 6th, 2004
0

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

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:
Reputation Points: 11
Solved Threads: 1
Junior Poster
anastacia is offline Offline
142 posts
since Nov 2004
Dec 6th, 2004
0

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

>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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 7th, 2004
0

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

well dave please clarify the reason. coz i am not an expert in c++. thanks in advance
Reputation Points: 11
Solved Threads: 1
Junior Poster
anastacia is offline Offline
142 posts
since Nov 2004
Dec 7th, 2004
0

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

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 7th, 2004
0

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

Quote originally posted by anastacia ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 7th, 2004
0

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

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

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

Quote originally posted by anastacia ...
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?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how to read file names from a folder
Next Thread in C++ Forum Timeline: iostream in C?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC