+ 2

Paint costs please help test 5 won't go through

Edit: This is for the Code Coach exercises, for those asking what the problem is. See -->Fails test case 5, all others pass. I used a workaround instead of using #include <cmath> and ceil(), thinking all non-integers get rounded down when converted to integers due to truncation of everything after the decimal point. #include <iostream> using namespace std; int main() { int canvas = 40; int paint; double tax; double total; int round; cin >> paint; tax = 0.1 * (canvas + (paint * 5)); total = canvas + (paint * 5) + tax; round = canvas + (paint * 5) + tax; if (round<total){ round++; cout << round; } else cout << total; return 0; }

14th Jun 2020, 11:08 PM
Andrew Mehrle
Andrew Mehrle - avatar
9 Antworten
0
Shouldn't you need a float for the tax though? If the paint is an odd number, the calculation will round down when tax is computed if you use int. Remember, I'm trying to bypass <cmath>, I've already passed all the tests with the ceil() function. I still don't see how the approximation rules affect only the last test case, since the other four pass appropriately, the rule should cause two more of the tests to fail if that were the issue from how I'm understanding it.
18th Jun 2020, 12:17 AM
Andrew Mehrle
Andrew Mehrle - avatar
0
I still don't get why the total isn't printing right in the else statement, I changed it to output the int round, but if round !< total, then it should follow that round==total as all tax increments from any int paint should be a multiple of 0.5, with all odd numbers giving tax with a 0.5 and evens giving an integer number. Changing else statement to cout << round fixed it, but since round !< total and it went to the else statement, they'd == each other. Commenting out the increment step caused 3-5 to fail, meaning odd numbers, adding it in allowed 3 and 4 to pass, which should've allowed 5 to pass as well. And again, I already did it with ceil(), as stated, I was trying to bypass using that function. And, to your comment, the question says "round up," not round-off," which could go either up or down, and would go down when declaring and computing tax as an int or utilizing tax in any int, like total. I feel like you're interpreting my question differently from the point I'm trying to get at.
20th Jun 2020, 3:34 AM
Andrew Mehrle
Andrew Mehrle - avatar
0
round is not the exact value when paint is an odd number since total is a double, so round will be less than total with odd paint values, which will activate the if statement. When converting from double to int, computer language always rounds down by truncating the decimal expansion. 2.5 = 2 when a double goes to int, as does 2.9, 2.99, etc. Type it into this code to check. #include <iostream> using namespace std; int main() { double x; int y; cin>>x; cout<<x<<endl; y=x; cout<<"double to int conversion"<<endl; cout<<y; return 0; }
20th Jun 2020, 3:28 PM
Andrew Mehrle
Andrew Mehrle - avatar
0
Just because we know 2.5 should be rounded up due to our rounding rules doesn't mean that's how the code rounds. It will round down, regardless of what is after the decimal, the int classification treats anything after the decimal as non-existent, therefore all conversions from float or double to int act as if the floor function is used when using #include <cmath>, the thing I was originally bypassing. In fact, the increment with the if statement is one way of how the ceil() function is calculated in c++. Do any division code with int where the output would be >0.5 (e.g. 4/5) and see how the output is 0, rounding down on int. Here's one example of computing ceil() without #include <cmath> I found while looking at this more. It only seems to work for positive numbers, however. #include <iostream> using namespace std; int main() { int a; int b; cin>>a>>b; int val = (a / b) + ((a % b) != 0); cout<<val; return 0; } from geeksforgeeks.org
20th Jun 2020, 3:32 PM
Andrew Mehrle
Andrew Mehrle - avatar
0
Dividing by 10 is the same as multiplying by 0.1, that didn't address the point at all, your Tax (5n+40)10 is the exact same as my tax 0.1*(canvas+paint5), I've already said I used the ceil() first, and since it said ROUND UP, your answer of Amount=55+(55/10)=60 IS WRONG, IT WOULD BE 61, NOT 60. 60.5 ROUNDS UP TO 61, BUT NOT IN CODE VIA TRUNCATION. 5.5 would go to 6. You solved nothing and missed the point I was making.
20th Jun 2020, 4:16 PM
Andrew Mehrle
Andrew Mehrle - avatar
0
So, in my original code, everything works exactly right, but test case 5 in Code Coach, which is hidden, fails. It shouldn't as the code accounts for odd numbers in the if statement and increments, does nothing if even, and the # of paints bought should only allow positive integers. Taking out the increment caused certain cases to fail, which told me those ones were odd. Adding that in accounted for all possible positive numbers, but 5 still failed, so I'm trying to figure out where that occurs, because you shouldn't be able to use negative integers for paints bought, unlike in the (American) football problem in Code Coach where you can have negative integers as input. Since the ceil() would give -2.3 a -2 and the workaround doesn't work if there is a negative integer introduced, i.e. a negative would be two off(?), but you can't buy negative amounts of paints in the first place. All positive integers are accounted for.
20th Jun 2020, 4:56 PM
Andrew Mehrle
Andrew Mehrle - avatar
0
I don't need the ceil code, I know how to use it, I was trying the challenge without it. That completely precludes the use of it. As said previously said, I can and have done the code with it, this is a workaround, so NOT using the ceil function was part of my goal. My first reply said as much: "Remember, I'm trying to bypass <cmath>, I've already passed all the tests with the ceil() function."
20th Jun 2020, 5:04 PM
Andrew Mehrle
Andrew Mehrle - avatar
0
Yeah, I get that, I think I need to look more at how <cmath> floor(), ceil(), round() functions do their activities as they seem to always return floating-point values despite returning integer solutions. I'm trying to recreate various functions without specifically utilizing certain libraries, to check emulation discrepancies. I'm new to this, so I'm probably not very clear with my wording
20th Jun 2020, 5:40 PM
Andrew Mehrle
Andrew Mehrle - avatar
0
SRY for the necro This passed all the tests, I hope it helps. (Wasn't sure if you wanted to use cmath or not) #include <iostream> #include <cmath> int main() { int brusCanv=40, paintPrice=5, paintAmount; //Declares The variables double tax = 0.1; //Declares Tax Amount std::cin >> paintAmount; //Takes input to find out how many paint colors there is int finalBeforeTax = (paintAmount * paintPrice) + brusCanv; //To find out the total before tax int finalPrice = ceil((finalBeforeTax * tax) + finalBeforeTax); //Get the tax amount and + it to the original and then rounds it UP std::cout << finalPrice; //Outputs the final Price return 0; }
23rd Sep 2021, 3:46 PM
Johan Van Loggerenberg
Johan Van Loggerenberg - avatar