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.

Jump to PostThe 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?

Jump to PostUse floating point, otherwise the answer is 1.

1 + 1/2.0 + 1/3.0 + 1/4.0

Jump to Postu 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

=1/y+1/y+1;

// to calculate total

for(i=0;i<20;i++)

total=x+x[i+1];

if (total>5) num=x// declare num first somewhere in the program

// how many terms neededof course this is not …

Dani
3,226
The Queen of DaniWeb
Administrator
Featured Poster
Premium Member

Dave Sinkula
2,398
long time no c
Team Colleague

Use floating point, otherwise the answer is 1.

1 + 1/2.0 + 1/3.0 + 1/4.0

anastacia
0
Junior Poster

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*=1/y+1/y+1;*

// to calculate total

for(i=0;i<20;i++)

total=x*+x[i+1];
if (total>5) num=x // 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:

Dave Sinkula
2,398
long time no c
Team Colleague

>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*=1/y+1/y+1;*

Reread my previous post.

anastacia
0
Junior Poster

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

jwenting
1,839
duckman
Team Colleague

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.

Dave Sinkula
2,398
long time no c
Team Colleague

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.

anastacia
0
Junior Poster

thnx in advance

Dave Sinkula
2,398
long time no c
Team Colleague

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?

anastacia
0
Junior Poster

else i guess i only have to run the bulldozer somewhere out there!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.