# HELP with coding.....

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 11-08-2007
shaheel
HELP with coding.....
hello everyone...I'm still learning c...I had to create a porgram to calculate the number of possible selections of choosing R players from N. I have use the facotorial function..I have done the coding..there is till one error in it..If someone could help..plzzzz

Code:

```void main() {         int n,r;         long int selection;         int factorial();         printf("\n Enter the N-number of players:");         scanf("%",&n);         printf("\n Enter the R-number of players you wish to select from %d players:",n);         scanf("%d",&r);         selection= (factorial(n))/ ((factorial(r))*(factorial(n-r)));         printf("\n The number of possible selections of %d players from %d players is %d",r,n,selection); } int factorial(int x) {         if (n<1)                 return(1);         else                 return(n*factorial(n-1)); }```
• 11-08-2007
matsp
Describe the problem a bit better, please...

Is it compiling but not running, running but not giving correct result, or not even compiling? If it's running with the wrong result, what are the data you enter?

--
Mats
• 11-08-2007
Salem
> scanf("&#37;",&n);
Are you forgetting something here?

Also main returns int, not void.
• 11-08-2007
shaheel
thanks for ur quick replies..ooops for the small errorss...

Well I needed to kno if the way I treatred the question was well...

If this way i would get the answer correctly...

when I run it for the first time..after entering data for n..the compiler doesnt ask for entering R but goes directly to the answer..????
• 11-08-2007
shaheel
n in
Code:

```} int factorial(int x) {         if (n<1)                 return(1);         else                 return(n*factorial(n-1)); }```
is said to be undefined....
• 11-08-2007
Salem
Until you fix the first scanf, I can't see it doing anything but skip reading n.

> is said to be undefined....
Your parameter is x, yet you use n.
• 11-08-2007
shaheel
thanks..i spoted my mistake....

Oh GOD............Its working.....................

I managed to run it successfully....

in int factorial[int x] should have been int factorial[int n]

Thanks guys...sorrry for disturbing ya........ Thanks agai
• 11-08-2007
shaheel
If u guys dont mind... can I ask another question plzzzzzzzzzzzzz???? Its concerning another program... I just really cant understand how perform this condition...

It says ask a question.. if answer is right pass to next...if it is wrong repeat question..if again wrong then give right answer.....

what condition loop may i use??? If else, While, for or do..??? Plzz helpp
• 11-08-2007
matsp
You can, realisticly, perform that as a for, while or do-while loop. The most natural choice is perhaps a for-loop, because you have a set number of "iterations" (two) that you should perform the task. But any of the loops can do this.

You could also do it using if/else, but that would repeat the same bit of code, which for the purpose of this excercise seems "wrong".

--
Mats
• 11-08-2007
Happy_Reaper
You don't need a loop for that at all. You just needed an if-else statement with another if-else statement nest in its else clause.
• 11-08-2007
matsp
Quote:

Originally Posted by Happy_Reaper
You don't need a loop for that at all. You just needed an if-else statement with another if-else statement nest in its else clause.

Yes, but that's not "teaching how to use loops" - which I have a feeling is what the excercise is about - I could be wrong, it could be about "nested if-statements", in which case your solution is right - but this solution is very inflexible - if you want to ask 3 times, 4 times or 5 times, you need to (noticably) change the code. A loop solution is very easy to change the number of iterations that it does.

--
Mats
• 11-08-2007
shaheel
hmmmmmmmmmmmmmmmmmmmmmmmm.......well can any one write up abit of the code..I'll still cant get that in my head...
P.s Do I need a counter here????
• 11-08-2007
shaheel
hmmm Guys...I'll give it a try...If Im still messed up..I'll get the coding here for u to help..

Once again thanks a lot..dunno what I would have done If this forum wasnt here...
• 11-08-2007
matsp
Yes, you will need SOME way of counting how many times you've done it - unless you build the "count" into the code as Happy_reaper suggests.

I'd rather that you TRY to write something, post it here, and then we'll discuss how to "improve" and "fix" it. That way, you do the learning, we do the correcting - rather than you only learning "that if you ask someone else to do it, you get something".

Of course, I could write something that you would get zero points for by your teacher - even if it works... Would you rather that I posted that?

--
Mats
• 11-08-2007
shaheel
ur right.....I'll try my best to start the coding a bit..n post it here..If ever Im completely stucK..please bear with me..Im still a newbie in this....
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last