Thread: HELP with coding.....

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    39

    Question 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));
    
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf("&#37;",&n);
    Are you forgetting something here?

    Also main returns int, not void.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    39
    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..????

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    39
    n in
    Code:
    }
    
    int factorial(int x)
    
    {
    	if (n<1)
    		return(1);
    	else
    		return(n*factorial(n-1));
    
    }
    is said to be undefined....

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    39
    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

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    39
    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

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    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.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Happy_Reaper View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Aug 2007
    Posts
    39
    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????

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    39
    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...

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Aug 2007
    Posts
    39
    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....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  2. Coding Guideline ....!!
    By imfeelingfortun in forum Tech Board
    Replies: 8
    Last Post: 10-08-2006, 07:09 AM
  3. Before Coding
    By cyberCLoWn in forum C++ Programming
    Replies: 16
    Last Post: 12-15-2003, 02:26 AM
  4. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 04:32 PM