Algorithm is correct according to me… tried both with float and double… I am using the relation:
e(i)= 0.45*i + e(i-1)/10
Please help…

using namespace std;
float e[100005];

int tst,lim=1;
int n,i;
if(e[n]) printf("%.7f\n", e[n]);
    for(i=lim;i<=n;i++) e[i]=0.45*(float)i + e[i-1]/10;
printf("%.7f\n", e[n]); 

The problem is, as n grows larger, due to floating point storage issues, the answer starts to get varied.

For example, the answer for n = 2000 is 999.944444444... but your program is giving 999.9444580. Which is clearly not correct in the first 6 decimal places. (Ref: http://ideone.com/GcdpMG)

When n is as big as 105, then the answers will be even different.

But I used double also… still WA… should i change my approach?

Oh! No!

Your code throws wrong answer, because of a silly mistake. You have misplaced your printf statement. Now, what happens is, when the if statement is true, both printfstatements will work. Put the second one inside the body ofelse` statement. And it will give you AC.

And of course, use double to take care of precision issues.

thnx a lot sir… didnt notice it at all…

You are welcome.

1 Like

And for the record, I am not a “sir”. :slight_smile:

1 Like

Just a fellow programmer like you. :slight_smile:

1 Like