0

“Halloween Candy” (C++)

//Is it possible to solve this challenge with only knowledge from the first module (Basic Concepts)? “You go trick or treating with a friend and all but three of the houses that you visit are giving out candy. One house that you visit is giving out toothbrushes and two houses are giving out dollar bills.” Task Given the input of the total number of houses that you visited, what is the percentage chance that one random item pulled from your bag is a dollar bill?

21st Feb 2021, 12:21 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
28 Answers
+ 5
#include <iostream> using namespace std; int main() { float houses; float dollar; cin >> houses; //your code goes here { dollar = (((houses - (houses - 2))) / houses) * 100; if (dollar == (int)dollar) { cout << dollar; } else cout << (int)dollar + 1; } return 0; } Try this now.👍
28th Feb 2021, 12:03 AM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
+ 2
Tahiti oh .. go through this link, to know how to share code link.. https://www.sololearn.com/post/75089/?ref=app Wait pls, for code check.. hope it helps.
21st Feb 2021, 12:57 PM
Jayakrishna 🇮🇳
+ 1
post your try.. edit : oh am going delete it soon..
21st Feb 2021, 12:25 PM
Jayakrishna 🇮🇳
+ 1
cout<<( .67 * (3/houses) * 100) ; You are simply doing this but i dont understand your logic here.. need to inverse it for finding chances .. There are only 2 dollor you getting. And we need to find percentage of chances for that dollors from total input houses.. And just apply ceil function on result to round up value... Edit : Tahiti explain your logic or rethink in above way...
21st Feb 2021, 1:08 PM
Jayakrishna 🇮🇳
+ 1
Tahiti I think, There it need two of total houses (not only for non-candy..) percentage. And ceil function does , round up values like ex: 1.2 to 2 1.7 to 2 1.0 to 1 Applying int only here makes all to 1 only.. You can use ceil by import math.h header and use like cout<< ceil( result ) ; Hope it helps.. let me know..
21st Feb 2021, 2:54 PM
Jayakrishna 🇮🇳
+ 1
ceil function does the job what you need there. and You can write your own function to do that job of rounding up values also.... edit : Tahiti Also (int/int) result int only.. so take dollar as double value.
21st Feb 2021, 3:08 PM
Jayakrishna 🇮🇳
+ 1
🎞Tahiti🍷 you need to find out chances of percentage of dollars from total houses.. so you need dollar/houses and round up result. If it is unsolved for you, then post updated code..
23rd Feb 2021, 7:24 PM
Jayakrishna 🇮🇳
+ 1
🎞Tahiti🍷 Am not fully checked the code but it gives always per+1 as output. 1st if is always false.. 2nd if true only for per==20 Else cases, last else part gets 3executed. I think that code not work for all cases...
26th Feb 2021, 11:01 AM
Jayakrishna 🇮🇳
+ 1
Tahiti🍷 how can it possible? I had posted my code.
27th Feb 2021, 8:55 AM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
+ 1
Most welcome 👍 Happy coding 👍👍
28th Feb 2021, 12:15 AM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
0
#include <iostream> using namespace std; int main() { int houses; cin>>houses; int dollar; dollar = .67 * (3/houses) * 100; houses>=3; cout << dollar; return 0; } // Why does this answer only pass one test and not the others?
21st Feb 2021, 12:25 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
0
Jayakrishna🇮🇳 I was posting it while you were writing. I’m still trying to figure out how to insert my code or a link to it, without leaving the discussion to go back and copy my answer from the lesson.
21st Feb 2021, 12:28 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
0
Jayakrishna🇮🇳 To get the output as the percentage of houses that will give dollars (cout >> dollar), I asked the program to output the int “dollar”. I defined “dollar” as 3 out of the number of houses, multiplied by .67 (two-thirds, or two of the three non-candy houses), then multiplied by 100 to get the percentage. I haven’t learned the “ceil” function yet, so I could only use concepts I’ve already learned.
21st Feb 2021, 1:52 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
0
Jayakrishna🇮🇳 So it’s only possible to solve it using the ceil function?
21st Feb 2021, 3:05 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
0
Hello this is my code. Try it. https://www.sololearn.com/coach/4?ref=app
22nd Feb 2021, 10:28 PM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
0
Sathe Prerana Satish That link takes me to my own saved exercise.
23rd Feb 2021, 4:44 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
0
Sandeep Kushwaha can you explain why you coded that way? Like, can you explain each line, beginning with “int high = (2*100)/houses; ? Thank you.
23rd Feb 2021, 4:48 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
0
Jayakrishna🇮🇳 So “high” is code for rounding up? I’m still confused what “per” and “.5” represent.
24th Feb 2021, 4:56 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
0
Ok. I think I have to get to a higher level before this will all make sense. Thanks.
26th Feb 2021, 8:17 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
0
Sathe Prerana Satish It’s really strange. When I tap “Hello this”, it takes me to my own profile page. When I tap “code” where you typed “Hello this is my code”, it takes me to your profile page. When I tap the URL/link, it takes me to the Halloween Candy instructions page. But I appreciate your offer of help anyway!
28th Feb 2021, 12:01 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar