+ 1
Where is the bug?
#include <iostream> #include<cmath> using namespace std; int main() { int color; float fixed = 40.0; cin >> color; float cost = (5.0 * color) + fixed; cost += cost * 0.10; cout << ceil(cost); return 0; }
2 Antworten
+ 3
Md Mehedi Hasan
I added explicit int conversion in the cout statement as:
cout << (int)ceil(cost);
And it worked. But I can't really understand what the issue is. The result normally returns an integer anyway(and so it passes 4 of the 5 test cases).
The error is, to my understanding, due to the cost being a certain floating number, that when converted with ceil, rounds up to a number it shouldn't. Maybe because of floating point precision, but I'm not sure either. I did check by using double instead of float, but that didn't fix the issue like (int) conversion did.
+ 1
If you're using an online compiler or testing, the program does not prompt the user, so it may appear like it does nothing.
ceil() returns a double. So, cout << ceil(cost); prints a number with a decimal (like 121 instead of 121.0), unless your environment suppresses it.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int color;
float fixed = 40.0;
cout << "Enter number of colors: ";
cin >> color;
float cost = (5.0 * color) + fixed;
cost += cost * 0.10;
cout << "Total cost: " << ceil(cost) << endl;
return 0;
}
Try it