Permutation Calculation

This is a discussion on Permutation Calculation within the C++ Programming forums, part of the General Programming Boards category; Originally posted by moonwalker your program crashes if you try to choose r > n and do nPr you should ...

  1. #16
    Registered User
    Join Date
    Jun 2003
    Posts
    18
    Originally posted by moonwalker
    your program crashes if you try to choose r > n
    and do nPr

    you should use an if condition to check this and prevent people from entering r > n.
    I'm currently fixing that, and came across a lil' problem.

    Basically, when you choose #3 on the main menu after the r > n error check (after it displays), the menu shows back up, and then you have to choose #3 again. Here's my new code
    (at time of posting):

    PHP Code:
    #include "includes.h"

    int pc;        // Used for the choice between permutation, combination, or quiting
    int n2;        // n part of nCr and/or nPr
    int r2;        // r part of nCr and/or nPr
    int ans;    // The answer of permutations or combinations

    unsigned long factunsigned long n );

    unsigned long factunsigned long n )
    {
        if( 
    == ) return 1;
        else if( 
    <= ) return n;
        else return 
    fact(n-1);
    }

    void s_p()
    {
        
    system("cls");
        
    cout << " nPr = n! / (n-r)!\n";    // Formula definition
        
    cout << " n = ";                // N value
        
    cin  >> n2;                        // "  " input
        
    cout << " r = ";                // R value
        
    cin  >> r2;                        // "  " input
        
        
    if(r2 n2)
        {
            
    cout << "\n\n" << r2 << " > " << n2 << " doesn't factor out correctly!\n\n";
            
    cout << "1) Go back to beginning\n"
                    " >> "
    ;
            
    cin  >> ans;
            if (
    ans 1) { system("cls"); begin(); }
            if (
    ans != 1) { system("PAUSE"); }
        }

        
    begin();

        if(
    r2 <= n2)
        {
            
    permutation(fact(n2), fact(n2-r2));    // Figures the permutation
        
    }
    }

    void s_c()
    {
        
    system("cls");
        
    cout << " nCr = n! / (n-r)!r!\n";
        
    cout << " n = ";
        
    cin  >> n2;
        
    cout << " r = ";
        
    cin  >> r2;

        if(
    r2 n2)
        {
            
    cout << "\n\n" << r2 << " > " << n2 << " doesn't factor out correctly!\n\n";
            
    cout << "1) Go back to beginning\n"
                    " >> "
    ;
            
    cin  >> ans;
            if (
    ans 1) { system("cls"); begin(); }
            if (
    ans != 1) { system("PAUSE"); }
        }

        if(
    r2 <= n2)
        {
            
    combination(fact(n2), fact(n2-r2));
        }
    }

    void permutation(int nint r)
    {
        
    ans r;
        
    cout << "\n\n" << ans << "\n\n";
        
    system("PAUSE");
        
    begin();
    }

    void combination(int nint r)
    {
        
    ans / (fact(r2));
        
    cout << "\n\n" << ans << "\n\n";
        
    system("PAUSE");
        
    begin();
    }

    void begin()
    {
        
    Start:
        
    cout << " Permutation (1)\n"
                " Combination (2)\n"
                " Exit        (3)\n\n"
    ;
        
    cout << " "
        
    cin  >> pc;

        switch(
    pc)
        {
        case 
    1:
            
    s_p();
            break;

        case 
    2:
            
    s_c();
            break;

        case 
    3:
            
    system("PAUSE");
            break;

        default:
            
    cout << "\n" << pc << " is not a valid choice!  Please choose another!\n\n";
            
    system("PAUSE");
            
    system("cls");
            goto 
    Start;
            break;
        }

    I didn't change anything else.
    Quantum Theory Entertainment

    Team #0005 US FIRST Robotics Team Website Designer
    Contact me on AIM: evanescence s0ul

    Compiler/IDE: Microsoft Visual Studio (C++) 6.0 / Microsoft .NET 2003

  2. #17
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    completely OT, but I like the colored code. What are you using to do that?
    Away.

  3. #18
    Registered User
    Join Date
    Jun 2003
    Posts
    18
    Originally posted by blackrat364
    completely OT, but I like the colored code. What are you using to do that?
    OT means???

    I'm pressuming you mean the stuff I posted above? I'm using the [ php ] (w/o spaces) and [ /php ] (also w/o spaces). It's VB stuff.
    Quantum Theory Entertainment

    Team #0005 US FIRST Robotics Team Website Designer
    Contact me on AIM: evanescence s0ul

    Compiler/IDE: Microsoft Visual Studio (C++) 6.0 / Microsoft .NET 2003

  4. #19
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    OT means off topic. So if you use the PHP tags for your code, it colors it? Or are you inserting a tag for each color?
    Away.

  5. #20
    Registered User
    Join Date
    Jun 2003
    Posts
    18
    Originally posted by blackrat364
    OT means off topic. So if you use the PHP tags for your code, it colors it? Or are you inserting a tag for each color?
    Ok, quick question, how is it exactly off-topic?

    Right, it colors it. Just using the PHP tags.
    Quantum Theory Entertainment

    Team #0005 US FIRST Robotics Team Website Designer
    Contact me on AIM: evanescence s0ul

    Compiler/IDE: Microsoft Visual Studio (C++) 6.0 / Microsoft .NET 2003

  6. #21
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    It's off the topic of permutations
    Away.

  7. #22
    Registered User
    Join Date
    Jun 2003
    Posts
    18
    Originally posted by blackrat364
    It's off the topic of permutations
    This thread will probably break the rules soon because of the "conversation", but I don't see how asking about fixing some code for calculating permutations / combinations is OT from permutations.....

    P.S. PM, IM, or E-mail me if you wish to continue this conversation.
    Quantum Theory Entertainment

    Team #0005 US FIRST Robotics Team Website Designer
    Contact me on AIM: evanescence s0ul

    Compiler/IDE: Microsoft Visual Studio (C++) 6.0 / Microsoft .NET 2003

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with modulo calculation
    By manutdfan in forum C Programming
    Replies: 6
    Last Post: 01-12-2009, 03:37 PM
  2. bit level permutation function
    By zxcv in forum C Programming
    Replies: 2
    Last Post: 07-27-2008, 02:26 PM
  3. Help with calculation and loop
    By Betty in forum C Programming
    Replies: 7
    Last Post: 04-10-2006, 06:37 AM
  4. Permutation algorithm??
    By lris2005 in forum C++ Programming
    Replies: 1
    Last Post: 04-01-2006, 06:51 AM
  5. INT ARRAY Permutation!
    By arthur5005 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 06:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21