Get minimum number of shots required so that goals over shots is the percentage given

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Get minimum number of shots required so that goals over shots is the percentage given



I am having some difficulty understanding why an extremely simple program I've coded in C++ keeps looping. I'll describe the problem at hand first just to check if maybe my solution is incorrect and then I'll write the code:



The shooting efficiency of a soccer player is the percentage of
goals scored over all the shots on goal taken in all his professional career. It is a rational number between 0 and 100,
rounded to one decimal place. For example, a player who
made 7 shots on goal and scored 3 goals has a shooting
efficiency of 42.9.
Given the shooting efficiency of a player, we want to know which
is the minimum amount of shots on goal needed to get that
number (which must be greater than 0).



What I thought of is that if p is the percentage given, then in order to get the minimum number of shots n, the relationship np <= n must be satisfied since np would be the number of goals scored over a total of n.



I've coded the following program:


int main()
float efficiency;
cin >> efficiency;
int i = 1;
float tries = i*efficiency;

while(tries > i)
i++;
tries = i*efficiency;


cout << i << endl;
return 0;



This program never terminates since it keeps looping inside the while, any suggestions on what might be wrong would be really appreciated.





Check your math. np <= n simplifies to p <= 1 when n is positive (which you say it must be). Changing the value of n has no effect.
– Raymond Chen
Aug 10 at 14:32



np <= n


p <= 1


n


n





I suppose the efficiency is expressed as the percentage and therefore has a value between 0 and 100 (not between 0 and 1). With an efficiency>1%, the product i*efficiency will always be greater than i. The algorithm therefore never exits the loop.
– Robert Kock
Aug 10 at 14:51


i*efficiency


i





You completely ignore the "rounded to one decimal place" thing. It's not going to implement itself.
– n.m.
Aug 10 at 15:19





This is actually an interesting mathematical problem. Here's the answer, but you'll have to translate it into code.
– Raymond Chen
Aug 11 at 3:00





2 Answers
2


while(tries > i)
i++;
tries = i*efficiency;



Here the only way for this to ever exit is if efficiency < 1. And you say that "It is a rational number between 0 and 100". If you want it to represent a percentage you either have to convert it to a decimal, or make i == 100 so that i and efficiency are on the same scale


efficiency < 1


i == 100


i


efficiency



You multiply efficiency after incrementing i. This means efficiency will grow much faster than i as when i increases by 1, efficiency will increase (i+1) times, ending up much larger than i.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered