Thread: Help with counting Permutations

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    27

    Help with counting Permutations

    I am having problems using the permutation formula in my code. I need to use P(n,k) to count k permutations in a string of n characters.
    Example:
    Input: P(3,2)
    Output: "A string of 3 characters has 6 permutations of length 2"

    I know the formula is 'P(n,k) = n! / (n-k)!' But I get compiler errors when trying to execute.

    Here is the section of my code related to my problem.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main()
    
    int n, k, p;
    
    printf("Enter one of the following options: \n\
                 Q - Quit the program\n\
                 Eval(<exp>) - Evaluate a simple expression\n\
                 ! - Evaluate an integer factorial\n\
                 P(n,k) - Count k-permutations of a string of n characters\n\
               \n\ Enter your option: ");
    
    if(strstr(userInput,"P") != NULL){
            sscanf(userInput,"%*c%*c%d%*c%d%*c", &n, &k);
            p = n! / (n - k)!;
            printf("A string of %d characters has %d permutations of length %d\n", n, p, k);
             }
    How can I get the code to compile correctly?

    Thank you,
    Justin

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have omitted:

    1) Curly Braces for main()
    2) Any values for your variables
    3) a return int for main()
    4) how you were going to figure the factorial of n, to prepare for your equation
    5) any code logic, whatsoever.

    You need to put some "skin' into this assignment. You're a smart guy, I'm sure you'll find something to get the ball rolling.

    You don't have a C problem just yet. You have a "done really nothing", problem. Come back when you have a C problem.
    Last edited by Adak; 05-06-2010 at 01:39 AM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    27
    Sorry, I was just posting the portion of my code for which I was having issues, here is my full code. I have the first 3 printf options working properly, but I am having trouble with the final 'else if' section. I am supposed to take input in the form of "P(3,2)", for example, and the program output would calculate the permutations of three characters using just two at a time.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #define bufSize 2048
    
    int main()
    {
      char userInput[bufSize];
      int opers[5];
      char operA;
      int exprA;
      int numA, idx;
      double result;
      char doneFlag = 'f';
     
      do {     
          printf("Enter one of the following options: \n\
                 Q - Quit the program\n\
                 Eval(<exp>) - Evaluate a simple expression\n\
                 ! - Evaluate an integer factorial\n\
                 P(n,k) - Count k-permutations of a string of n characters\n\
               \n\ Enter your option: ");
          
          fgets(userInput,bufSize,stdin);
          
        if(strstr(userInput,"Q") != NULL) {
            doneFlag ='t';
        } else if(strstr(userInput,"q") != NULL) {
            doneFlag ='t';
        } else if(strstr(userInput,"Eval") != NULL){
          sscanf(userInput,"%*c%*c%*c%*c%*c%d %c %d%*c", &opers[0], &operA, &opers[1]);
          switch(operA) {
                       case '+':
                       exprA = opers[0] + opers[1];
                       printf("%d %c %d = %d\n", opers[0], operA, opers[1], exprA);
                       break;
                       case '-':
                       exprA = opers[0] - opers[1];
                       printf("%d %c %d = %d\n", opers[0], operA, opers[1], exprA);
                       break;
                       case '/':
                       exprA = opers[0] / opers[1];
                       printf("%d %c %d = %d\n", opers[0], operA, opers[1], exprA);
                       break;
                       case '*':
                       exprA = opers[0] * opers[1];
                       printf("%d %c %d = %d\n", opers[0], operA, opers[1], exprA);
                       break;
                       case '%':
                       exprA = opers[0] % opers[1];
                       printf("%d %c %d = %d\n", opers[0], operA, opers[1], exprA);
                       break;
                       case '^':
                       exprA = pow(opers[0],  opers[1]);
                       printf("%d %c %d = %d\n", opers[0], operA, opers[1], exprA);
                       break;
                       default:
                               exprA = 0;
                               printf("?'%c' is not a valid operator", operA);
                       }
        } else if(strstr(userInput,"!") != NULL){               // Calculate the Factorial of !
            sscanf(userInput,"%*c%d", &opers[2]);
              result = 1.0;
              for(idx = 1; idx <= numA; idx++) {
                result *= (double)idx;
              }
              printf("%d! = %.0lf\n", opers[2], result);
        } else if(strstr(userInput,"P") != NULL){            // Calculate P(n,k)
            sscanf(userInput,"%*c%*c%d%*c%d%*c", &opers[3], &opers[4]);
            opers[5] = opers[3] / (opers[3] - opers[4]);
            printf("A string of %d characters has %d permutations of length %d\n", opers[3], opers[5], opers[4]);
              }
        
       } while(doneFlag == 'f');
    
    
     return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I know the problem is well known, but it missed me, and I don't have the time to jump into it, atm.

    << Bump for a smart problem >>

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    27
    I got it figured out, it was all too simple, and I was just over-thinking it.
    Thanks

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    27
    Nevermind, It does not work. I thought I had the P(n,k) encoded correctly, but I get a compiler error. I am sure that my 'for() && () {' line is not formatted correctly, can someone help me correct it?

    This is getting a little frustrating, as I am new to C, and have been looking everywhere for answers. I can calculate the first n!, but I am lost in regards to calculating (n-k)!, how do I modify my code to enable it to calculate correctly?

    Please Help.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #define bufSize 2048
    
    int main()
    {
      char userInput[bufSize];
      int opers[4];
      char operA;
      int exprA;
      int idxA;
      int idxB, idxC, resultB, resultC, resultD;
      double resultA;
      char doneFlag = 'f';
     
      do {     
          printf("Enter one of the following options: \n\
                 Q - Quit the program\n\
                 Eval(<exp>) - Evaluate a simple expression\n\
                 ! - Evaluate an integer factorial\n\
                 P(n,k) - Count k-permutations of a string of n characters\n\
               \n\ Enter your option: ");
    
    fgets(userInput,bufSize,stdin);
    
    if(strstr(userInput,"!") != NULL){               // Calculate the Factorial of !
            sscanf(userInput,"%*c%d", &opers[2]);
              resultA = 1.0;
              for(idxA = 1; idxA <= opers[2]; idxA++) {
                resultA *= (double)idxA;
              }
              printf("%d! = %.0lf\n", opers[2], resultA);
        } else if(strstr(userInput,"P") != NULL){              // CALCULATE  P(n,k)
            sscanf(userInput,"%*c%*c%d%*c%d%*c", &opers[3], &opers[4]);
            resultB = 1;
            for(idxB = 1; idxB <= opers[3]; idxB++) && (idxC = opers[3] - opers[4]; <= opers[4]; idxC++) {
                 resultB = idxB / idxC;
            }  
            printf("A string of %d characters has %d permutations of length %d\n", opers[3], resultD, opers[4]);
              
              }
       } while(doneFlag == 'f');
    
    
     return 0;
    }
    Last edited by jusfry01; 05-06-2010 at 03:24 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why would you compute (n-k)! differently than n! ? It's the same function, to be computed in the same way.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    There's also no such syntax as this.
    Code:
    for(idxB = 1; idxB <= opers[3]; idxB++) && (idxC = opers[3] - opers[4]; <= opers[4]; idxC++) {
    Maybe you're trying to create a nested for loop?
    Code:
    for(idxB = 1; idxB <= opers[3]; idxB++) {
        for(idxC = opers[3] - opers[4]; <= opers[4]; idxC++) {
            // ...
        }
    }
    By the way, there's a builtin macro in stdio.h called BUFSIZ that you could make use of . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    27
    I understand that n! and (n-k)! are the same function, but I am having trouble doing the factorial of the result of (n-k). To this point, I have only learned to do a factorial on a single value, and am not sure how to generate the factorial of the (n-k).

    My structure is in a nested for statement now, and the program reads all of my input, but tells me that I have only 1 permutation no matter what numbers I put in.

    Here is the section of my code that I am having trouble with... Please help me sort this out

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #define bufSize 2048
    
    int main()
    {
      char userInput[bufSize];
      double opers[1];
      int num[3];
      char operA;
      double exprA;
      int idxA;
      int idxB, idxC, resultB;
      double resultA;
      char doneFlag = 'f';
     
      do {     
          printf("Enter one of the following options: \n\
                 Q - Quit the program\n\
                 Eval(<exp>) - Evaluate a simple expression\n\
                 ! - Evaluate an integer factorial\n\
                 P(n,k) - Count k-permutations of a string of n characters\n\
               \n\ Enter your option: ");
          
          fgets(userInput,bufSize,stdin);
    
     if(strstr(userInput,"P") != NULL){              // Calculate P(n,k)
            sscanf(userInput,"%*c%*c%d%*c%d%*c", &num[1], &num[2]);
            resultB = 1;
            for(idxB = 1; idxB <= num[1]; idxB++) {
                     for(idxC = num[1] - num[2]; idxC <= num[2]; idxC++) {
                 resultB = idxB / idxC;
            }
            }  
            printf("A string of %d characters has %d permutations of length %d\n", num[1], resultB, num[2]);
              
              }
       } while(doneFlag == 'f');
    
    
     return 0;
    }
    Last edited by jusfry01; 05-07-2010 at 12:19 AM.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    1

    Ahhhh as well

    here is the mathmatical formula for calculating permutations if it helps. By the way, I am in the same class and this assignment is a B!%$&.

    P(n,k) = n! / (n - k)! =
    120

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well IMO you guys should have a separate function that computes factorial of some generic parameter m.

    Then if you want to calculate the factorial of n you just call that function(assume it's called factorial) with the argument n or n-k.

    i.e.

    long result = factorial(n);
    long result2 = factorial(n-k);
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you have a function that will calculate the factorial, then just have the calling function make the subtraction first, and then send that number, to the factorial function.

    I'd be glad to help you sort out your problem with this program, but I don't see what problem you are having. Once you have the factorial logic worked out, you've got the heavy lifting done, (don't you?).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Permutations
    By bumfluff in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2008, 12:33 PM
  2. How to implement reference counting with 'Smart_ptr'?
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 06-10-2007, 05:28 AM
  3. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM
  4. permutations of a string
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 05-23-2006, 08:52 AM
  5. Counting using Lib Cstring
    By niroopan in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2002, 05:51 PM