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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 2
Reputation: far is an unknown quantity at this point 
Solved Threads: 0
far far is offline Offline
Newbie Poster

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

 
0
  #1
Dec 5th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,041
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

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

 
0
  #2
Dec 5th, 2004
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?
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #3
Dec 5th, 2004
Use floating point, otherwise the answer is 1.
1 + 1/2.0 + 1/3.0 + 1/4.0
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 140
Reputation: anastacia is an unknown quantity at this point 
Solved Threads: 1
anastacia's Avatar
anastacia anastacia is offline Offline
Junior Poster

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

 
0
  #4
Dec 6th, 2004
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:
:lol: I am not one of those who wait for things to happen, :p but one of those who make things happen ;)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #5
Dec 6th, 2004
>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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 140
Reputation: anastacia is an unknown quantity at this point 
Solved Threads: 1
anastacia's Avatar
anastacia anastacia is offline Offline
Junior Poster

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

 
0
  #6
Dec 7th, 2004
well dave please clarify the reason. coz i am not an expert in c++. thanks in advance
:lol: I am not one of those who wait for things to happen, :p but one of those who make things happen ;)
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

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

 
0
  #7
Dec 7th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #8
Dec 7th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 140
Reputation: anastacia is an unknown quantity at this point 
Solved Threads: 1
anastacia's Avatar
anastacia anastacia is offline Offline
Junior Poster

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

 
0
  #9
Dec 7th, 2004
yeah sort of understanding a little bit. jwenting said something about a counter can you please clarify that ... point.
thnx in advance
:lol: I am not one of those who wait for things to happen, :p but one of those who make things happen ;)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #10
Dec 7th, 2004
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC