+ 1
You are getting ready to paint a piece of art. The canvas and brushes that you want to use will cost 40.00. Each color of paint
#include <stdio.h> int main() { int b=40, p ,sum,c; int per, total; scanf ("%d",&p); c= p*5; sum= c+b; per= (sum/10); total= sum+per; printf ("%d",total); return 0; } Is there any problem in my code? Help me with this!
9 Antworten
+ 3
If it is about paint cost code coach, sum/10 return integer, truncates fraction part. Use sum/10.0 and use in a double type variable or else directly use in calculation. And finally asked to 'round up', result so apply ceil() function from math library.
Ex: if sum = 52 then sum/10 = 5, but you need 5.2 , applying ceil(52+5.2) will result = 58 final result that is asked to display...
+ 1
I don't knw what's wrong with this program , sololearn is not accepting this
+ 1
This was actually also tricky for me( for some reasin hidden case #5 was not fulfilled and it annoyed me so much, so i’ll break the rule and post my quick unclean solution here:
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double basicCost = 40;
double numberOfColors;
cin >> numberOfColors;
double sum = (basicCost + numberOfColors*5)*1.1;
if(fmod(sum,1) > 0.01){
cout << ceil(sum);
}else{
cout << (int)sum;};
return 0;
}
The fmod function is a modulo that works with doubles. And because floating point are tricky, you actually don’t want to check if its „non-zero”, because floats always have some residual microscopic values.
+ 1
@Mikolaj There is no need of if-else, you can directly apply..
cout << (int)ceil( sum) ;
What if fmod(sum, 1) result like 0.0001 still you should apply ceil..
+ 1
Jayakrishna🇮🇳 you're right, i was fiddling around and there is certainly some residual code left
+ 1
Jayakrishna🇮🇳 ok I tested it and I remember why i left it. For some reason, when the double sum doesnt have any decimal part, after being cast to int it should be still an integer, but when using the ceil function on it, it get's increased by 1 for no apparent reason.
+ 1
Mikolaj May be that is because of your sum calculation.. precision calculations are tricky. It may have like 0.0000000001 difference with conversions..
Instead of 1.1 use straightforward 10% like sum*0.1
I checked same code with formula
double sum = numberOfColors * 5+40;
sum = sum + sum*0.1; //
It's working fine with sum does not have any decimal part...
So i think your formula result a decimal fraction with some value..
May not be, problem with the ceil value.. But Your code works fine for this challenge..
Also may be issue with some other part , I may not aware of it. Not deeply checked it. Just comparing with my approach..
edit:
Input : 12 causing different result in this 2 varients.. //110 ceil(110) = 110
just checked it that
cout<<setprecision(20)<<sum<<endl;
//110.0000000001421 , not 110
that's why floating points are tricky to handled..
0
Question is truncated..
Pls Add your problem or difficulty clearly..
0
Mikolaj 👍
Just want tell that scenario, which came to my mind........