Hmm...if it is OK!
Then can you tell me...why this same function fails in this section???
Which one is correct then? I want the first one (above) but why this is behaving strangley in this ???
Code:
#include<iostream.h>
#include<math.h>
inline double round(double X, int k)
{
return ( floor( X*pow10(k) + 0.5) / pow10(k) );
}
int main()
{
int n = 0;
while(cin >> n)
{
if(n == 0)
return 0;
double trip[1000] = {0};
double sum = 0,ans = 0;
for(int i=0;i<n;++i)
{
cin >> trip[i];
sum += trip[i];
}
double avg = sum/double(n);
cout << " AVG: " << avg << endl;
avg = round(avg,2);
cout << " AVG: " << avg << endl;
for(int i=0;i<n;++i)
if(trip[i] > avg)
ans += trip[i]-avg;
cout.setf(ios::fixed);
cout.precision(2);
cout << '$' << ans << endl;
}
return 0;
}
Here is input?
And the output is:
4
15.01
15.00
3.00
3.01
AVG: 9.005
AVG: 9
$12.01