Find the answer to your Linux question:
Results 1 to 9 of 9
Ok I just joined this board in hope I can ask this question and that I'm in the right place... So I have to write a complete program that inputs ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    5

    permutaion function C++

    Ok I just joined this board in hope I can ask this question and that I'm in the right place...

    So I have to write a complete program that inputs n and k and prints out the p(n,k).

    This is what I have so far and I'm pretty sure something is incorrect

    #include <iostream>
    using namespace std;
    int main(){
    int p,n,k;



    cin>>n>>k;

    for(P(n,k) = n! / (n - k)!);
    {
    double factorial(unsigned n) {
    if (n == 1) return n;
    return n * factorial(n-1);
    }
    cout<<"p("<<n<<','<<k<<")"<<p<<endl;
    }




    return 0;
    }

    Thanks in advance

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Is p(n, k) a function?

    do you mean your trying to calculate P(n, r) = n!/(n - r)!

  3. #3
    Just Joined!
    Join Date
    Oct 2008
    Posts
    5
    yes thats what i mean

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Well you should define your function outside of the main function like:

    Code:
    #include <iostream>
    using namespace std;
    
    double mypermutation(int x, int y);
    
    int main()
    {
         int p,n,k;
         cin>>n>>k;
    
         mypermutation(n, k);
    
         cout<<"p("<<n<<','<<k<<")"<<p<<endl;
         return 0;
    }
    
    double mypermutation(int x, int y)
    {
        //does whatever it takes to calculate the permutation
        //return the answer as a double
    }

  5. #5
    Just Joined!
    Join Date
    Oct 2008
    Posts
    5
    Sry I'm quite the novice at this but where would that go again?

  6. #6
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    With respect drummer0702, but gerard4143 pretty much gave you the answer!

    If you check out the forum rules you'll see that homework questions aren't allowed. We all had to figure it out in our day -- so do you

    Do your best to figure out what gerard4143 meant, and we can try to answer any specific questions you have.

    Welcome to the forums
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

  7. #7
    Just Joined!
    Join Date
    Oct 2008
    Posts
    5
    Sorry about that. Yah thanks for what help you gave me I'll try and figure it out..Sorry again

  8. #8
    Just Joined!
    Join Date
    Oct 2008
    Posts
    5
    Okay I figured out what you meant thank you...Now my only problem is that it saying there is a syntax error for 'for' and that something about old style formatting...Idk what the latter is

  9. #9
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    If you post the program which compiles with the syntax error, we will be better able to help you.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...