Paint costs please help test 5 won't go through | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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
23 Answers
+ 3
(-_-) See. In the code above, you have given round the exact value as total so if statement would never be executed. Since each paint costs 5, value of amount (without tax) would be an integral multiple of 5. So when calculating the tax (dividing by 10), decimal value would also be a multiple of 5, that is, it would either be .0 or .5. Now that we have to approximate the result, .0 value would be approximated to that same number and .5 value would be incremented by 1 (the next integer). So you need to check for the decimal values rather than comparing values (which are equal in your code) and then increment it or leave it unchanged.
20th Jun 2020, 5:00 AM
Prabhanshu Chaturvedi
+ 3
Since you are not getting anything I am saying. I'll explain what I did. Given the cost of paint is 5. User purchased 'n' paints so the total cost of paint is 5n. Now, given the cost of canvas is 40. So the amount would be 5n+40 which is a multiple of 5. Calculating tax on the amount, Tax=(5n+40)/10 So the final amount will be Amount=Amount+Tax Now since amount was a multiple of 5, dividing it by 10 would give a decimal point value either 0 or 5. If the amount is an even multiple of 5, the decimal value would be 0. Therefore, there is no need for any approximation of amount. Example: n=2 Tax=50/10 Amount=50+(50/10)=55 Now if the amount is an odd multiple of 5, the decimal value would be 5. Therefore, you need to increment the calculated integer amount by one. Example: n=3 Tax=55/10 Amount=55+(55/10)=60 (since amount is an integer) There I have solved the question for you. I used the same approach.
20th Jun 2020, 4:00 PM
Prabhanshu Chaturvedi
+ 3
Now the second approach. Keep whatever data types you want. Use the ceil() function and print the value. cout<<ceil(amount); It uses cmath.h header file which you would have to include.
20th Jun 2020, 4:02 PM
Prabhanshu Chaturvedi
+ 3
I told you to increment the amount when n is odd. So it would be 61. You mean to say ceil() is not working?
20th Jun 2020, 4:22 PM
Prabhanshu Chaturvedi
+ 3
Here is the code using ceil() function. See it and I'll delete it. https://code.sololearn.com/c50N813l1V8X/?ref=app
20th Jun 2020, 4:35 PM
Prabhanshu Chaturvedi
+ 3
As for the hidden test cases I really can't do anything about it. For the test cases are hidden. You can message me privately for it. ^ ^
20th Jun 2020, 4:54 PM
Prabhanshu Chaturvedi
+ 3
Yes. I see that. You can try running test cases by manually entering values. Try to see if the two codes are producing different outputs for the same value using iterations. You can use sleep (milliseconds) to delay the outputs for your ease. I'll compare them too.
20th Jun 2020, 5:01 PM
Prabhanshu Chaturvedi
+ 3
Ok, so here is the code which I was explaining from the start. https://code.sololearn.com/cUuMjg5nV1R9/?ref=app
20th Jun 2020, 5:11 PM
Prabhanshu Chaturvedi
+ 2
What is the problem code
15th Jun 2020, 10:09 AM
Prabhanshu Chaturvedi
+ 2
Ok I saw the question. You are doing wrong approximations. Search the rules for approximation and modify your code accordingly.
16th Jun 2020, 2:41 AM
Prabhanshu Chaturvedi
+ 2
Also do not use floating point data types. Keep it int for you have to make an approximation for every case.
16th Jun 2020, 2:43 AM
Prabhanshu Chaturvedi
+ 2
If you are using float, you will have to use another variable to print in int or by using ceil() function. As the question says to "round-off" to the nearest integer the best data type is int. So try to approximate the values. (3/2)=1.5 therefore it needs to be displayed as 2 (if the last digit of decimal is greater than or equal to 5, increase the value on the left side of decimal point by one) (8/2)=4 there is no need to approximate here. This hint is like solution to the question. Try it again. Run your own test cases to better understand the question and your code.
18th Jun 2020, 1:19 AM
Prabhanshu Chaturvedi
+ 2
The code is now public. It should be visible.
20th Jun 2020, 5:30 PM
Prabhanshu Chaturvedi
+ 2
ceil() is like smallest integer function and floor() is like greatest integer function. Try seeing their graphs for better understanding as they are opposite here. If you're recreating them make sure there is no value causing error. There are several functions you might want to recreate like sqrt(), pow(). I made a square root calculating function in C. There can be more like cube, cube root etc.
20th Jun 2020, 6:51 PM
Prabhanshu Chaturvedi
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