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?
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.👍
+ 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.
+ 1
post your try..
edit : oh
am going delete it soon..
+ 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...
+ 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..
+ 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.
+ 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..
+ 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...
+ 1
Tahiti🍷 how can it possible?
I had posted my code.
+ 1
Most welcome 👍
Happy coding 👍👍
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?
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.
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.
0
Jayakrishna🇮🇳
So it’s only possible to solve it using the ceil function?
0
Hello this is my code.
Try it.
https://www.sololearn.com/coach/4?ref=app
0
Sathe Prerana Satish
That link takes me to my own saved exercise.
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.
0
Jayakrishna🇮🇳 So “high” is code for rounding up? I’m still confused what “per” and “.5” represent.
0
Ok. I think I have to get to a higher level before this will all make sense. Thanks.
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!