Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basketball program

Status
Not open for further replies.

noyd

Programmer
Joined
Oct 31, 1999
Messages
1
Location
US
I recently had an extra credit problem for my c++ class. Although no one got it and its too late to hand it in, I would like to know how this program could be coded. Here it is:<br>
Have a user input the number of points they scored, then tell them how they could have scored those points. <br>
(using cout&lt;&lt;&quot;How many points: &quot;;<br>
cin&gt;&gt;score;<br>
<br>
Example:<br>
Enter the amount of points: 5<br>
You could have made a 3 point and a 2 point shot<br>
Or you could have made a 3 point shot and two free throws<br>
Or a two point shot and 3 free throws<br>
Or you could have made 5 free throws.<br>
There are three possible combinations<br>
:::::::::::::::::::::::::::::::::::::::::::::::::<br>
Please help if you can<br>
thanks
 
<br>
<br>
I haven't come upon a solution quite yet, but I have a feeling that a recursive function or two is going to be the key. The heart of the problem is a mathematical one. For starters, there are 5 possible combinations to the example you gave...<br>
<br>
1) 1 3ptr. + 1 2ptr. = 5pts.<br>
2) 1 3ptr. + 2 1ptrs. = 5pts.<br>
3) 2 2ptrs. + 1 1ptr. = 5pts.<br>
4) 1 2ptr. + 3 1ptrs. = 5pts.<br>
5) 5 1ptrs. = 5pts.<br>
<br>
Once the mathematics are straight, the coding itself shouldn't be too bad. I'll see what algorithms I can dig up.
 
This little box may make this hard to read but the code works. I used a form instead of cout...<br>
Let me know how it goes.<br>
<br>
D<br>
<br>
{<br>
int s;<br>
s=StrToInt(Form1-&gt;txtScore-&gt;Text);<br>
int a,b,c;<br>
a= s;<br>
b= s; //should be b=s div 2;<br>
c= s; //should be c=s div 3;<br>
int num=0;<br>
Form1-&gt;Memo1-&gt;Text=&quot;&quot;;<br>
for(int i=0;i&lt;=a;i++){<br>
for(int j=0;j&lt;=b;j++){<br>
for(int k=0;k&lt;=c;k++){<br>
if (i*1 + j * 2 + k * 3 == s){<br>
Form1-&gt;Memo1-&gt;Text= Form1-&gt;Memo1-&gt;Text + &quot;\r&quot; +<br>
i +&quot; Freethrows, &quot; + j + &quot; Twos, &quot; + k + &quot; Threes&quot;;<br>
num++;<br>
}<br>
<br>
}<br>
}<br>
}<br>
Form1-&gt;Memo1-&gt;Text= Form1-&gt;Memo1-&gt;Text + &quot;\r&quot; +<br>
&quot;There are &quot; + num +&quot; ways&quot;;<br>
}
 
This is a probability application.<br><br>Siddhartha Singh<br><A HREF="mailto:ssingh@aztecsoft.com">ssingh@aztecsoft.com</A>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top