+ 2

Want help in Halloween candy

Hi guys actually I'm stuck in solving the Halloween candy problem. Can anybody help me out? In c++. #include <iostream> using namespace std; int main() { int houses; cin>>houses; int percentage; //your code goes here percentage = (houses/2)*100; if(houses>=3){ cout<<percentage; } return 0; } So guys that's my code.

7th Sep 2020, 6:21 PM
Shravan Mathapati
Shravan Mathapati - avatar
13 odpowiedzi
+ 5
There are some problems with your above code: 1. The formula you used to calculate the percentage of dollar bills is wrong (reversed). 2. By default, if you divide an integer by another integer in C++, the result will be an integer as well, e.g. 1 / 2 = 0 7 / 2 = 3 where the decimal part is cut off. If you expect a floating point number as result of a division, at least one of the operands must be a floating point value itself, e.g. 1 / 2.0 = 0.5 7.0 / 2 = 3.5 3. The task states to round the result up to the nearest integer. This can easily be achieved by including the <cmath> library and using std::ceil: https://en.cppreference.com/w/cpp/numeric/math/ceil And just so by the way: The description promises that the value of "houses" will always be at least equal to three, therefore the if-statement is unnecessary. Try to incorporate all of these things into your solution.
8th Sep 2020, 10:27 AM
Shadow
Shadow - avatar
+ 4
Shravan Mathapati , before we can help you, you should have done a try by yourself first. Please put your code in playground, save it there and then post the link to it here. Thanks!
7th Sep 2020, 6:24 PM
Lothar
Lothar - avatar
+ 4
Shravan Mathapati Linking to the code coach problem itself will only show us our own codes, since you can not see the solutions of others. That is why Lothar instructed you to copy your code to a playground project and share that instead.
7th Sep 2020, 7:44 PM
Shadow
Shadow - avatar
+ 3
It is better to see us your attempt first.
7th Sep 2020, 6:25 PM
Yahya Bey
Yahya Bey - avatar
+ 3
std::round() rounds a decimal number either up or down, depending on its value, but the task states to always "round up to the nearest whole number". That is the reason I advised you to use std::ceil() earlier, as it will always round up. Also, "percentage" will never be smaller or equal to zero, so the check is redundant, but it won't affect any of the test cases.
8th Sep 2020, 2:36 PM
Shadow
Shadow - avatar
+ 2
Shadow thanks for your help
8th Sep 2020, 10:30 AM
Shravan Mathapati
Shravan Mathapati - avatar
+ 2
Here's a simple python version. Not sure if it helps. https://code.sololearn.com/cT112kPuS73X/?ref=app
8th Sep 2020, 10:15 PM
Sonic
Sonic - avatar
+ 1
Lothar Yahya Bey Shadow I have uploaded my code in the description. Please help me.
8th Sep 2020, 2:32 AM
Shravan Mathapati
Shravan Mathapati - avatar
+ 1
Shadow this is my updated code ,but still it fails one case out of 5. #include <cmath> #include <iostream> using namespace std; int main() { /*declaring all the variables*/ float houses; cin>>houses; float percentage; /*getting the number of dollr bills*/ /*finding out the percentage */ percentage = (2/houses)*100; /*rounding up to the nearest integer*/ percentage = round(percentage); /*if the percentage comes 0 then*/ if(percentage<=0){ cout << percentage +1; } else{ cout<<percentage; } return 0; } what should be done
8th Sep 2020, 1:39 PM
Shravan Mathapati
Shravan Mathapati - avatar
7th Sep 2020, 6:55 PM
Shravan Mathapati
Shravan Mathapati - avatar
0
Here is my first attempt to solve this problem. please help me out
7th Sep 2020, 6:56 PM
Shravan Mathapati
Shravan Mathapati - avatar
0
Thank you guys . I solved it with your help
9th Sep 2020, 5:58 AM
Shravan Mathapati
Shravan Mathapati - avatar
0
How to put two login forms in a page
9th Sep 2020, 6:18 AM
Ndunero Francis Nnamdi.