0

Error in Code coach task (c++)

In paint cost this code passes all first 4 cases but not last one. What is the error in it I tried using ceil and round function but of no luck. https://code.sololearn.com/cIj8uBtyZkO9/?ref=app

22nd Apr 2021, 5:39 PM
Sanjyot21
Sanjyot21 - avatar
8 ответов
+ 4
#include <iostream> #include <math.h> using namespace std; int nearest(float f){ return ((f-(int)f)>=0.5) ? ceil(f) : floor(f); } int main() { float p,cb,t; cb=40; cin>>p; p*=5; t=(p+cb)*0.1; cout<<nearest(p+t+cb); return 0; }
22nd Apr 2021, 5:44 PM
minirkk
minirkk - avatar
+ 2
Your line 11 is wrong(i mean rounding is wrong)
22nd Apr 2021, 5:44 PM
minirkk
minirkk - avatar
+ 2
Sanjyot21 This is what I meant int main() { ...redacted }
23rd Apr 2021, 5:01 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Just make p and cb an int. t should be a float. Then calculate t, round up and convert t to an int and add p and cb.
22nd Apr 2021, 6:41 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Rohit thanks bro
23rd Apr 2021, 4:57 AM
Sanjyot21
Sanjyot21 - avatar
+ 1
ChaoticDawg Ya ok sorry my mistake. Thanks
23rd Apr 2021, 5:13 AM
Sanjyot21
Sanjyot21 - avatar
0
You don't have to always go for the ceil value, because in question it is written round off value
22nd Apr 2021, 5:49 PM
Atul [Inactive]
0
ChaoticDawg I tried your suggestion but gave error. Anyways thanks for your response.
23rd Apr 2021, 4:57 AM
Sanjyot21
Sanjyot21 - avatar