Thread: permutating and printing all possible 5 numbers r for an array of 20 or N elements

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197

    Post permutating and printing all possible 5 numbers r for an array of 20 or N elements

    Hey guys this a program i am to write for my college professor and he has given me a list of N elements and i am to print all possible 5 combination of elements and print to a list. However, i was thinking
    Code:
    of using the rand() and srand() functions but since the number space is very large so rand() % N would produce repeated
    sequences of 5 numbers so i just wanted help how how to develop this algorithm. Here is my code but i know it is not correct.

    Code:
    int Permutate_Numbers(int Array[], int n) 
    {
         for( int i = 0; i < N C 5; i++)//counts right up to NCombination     5...doubt here??        
          {               
              srand(time(NULL));              
              for( int j = 0; j < 5; j++)
                 Result[i][j] = rand() % 5;       
            }         
          return 0;
     }  //but i have to make sure no number repeates on the same
    sequence list and no sequence list repeats subsequently... well i am not really sure about my algorithm or do i need to do some permutations again to be able to get all the possible 5 combinations of the 18 elements... thanks for the help...

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    Why don't you try it on paper, for say all combinations of 2 numbers from 4 numbers.

    You should be able to quickly write out the answer by hand.

    Once you have the answer in front of you, you can start to visualise how to get there from your numbers.

    FWIW, calling srand() in a loop is a waste of time and effort. time(NULL) will return the same value for a whole second, which is an awful lot of loop iterations.
    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
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    Just think about trying to break it down into smaller subproblems. What would you do it if your list had <= 5 elements? Right, you'd just print the list. What about 6 elements? You'd exclude a different element each time and print the resulting lists. What about 7? You'd exclude a different element each time to get all the possible 6 combinations, and then process each of those... and so on.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    ok thanks @Salem and @joeprogrammer what ure saying is correct i did not really take time to think about it well i'll try to write the code on paper and see for small input then continue increasing and thinking

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    however looking on paper it would not consist of N Combination 5 But N permuation 5 to be able to produce all the possible 5 sequences to be generated from the 18 elements on the list or array...thanks let me now work on an algorithm...

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Quote Originally Posted by Nyah Check View Post
    however looking on paper it would not consist of N Combination 5 But N permuation 5 to be able to produce all the possible 5 sequences to be generated from the 18 elements on the list or array...thanks let me now work on an algorithm...
    looks like a 2 step problem:
    generate unqiue combinations per joe and salem
    for each combination, generate the permutations

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help with Printing duplicate array elements
    By TheSprawl in forum C Programming
    Replies: 9
    Last Post: 11-23-2011, 11:22 PM
  2. Pointers and printing distinct elements in array
    By dave_the_bear10 in forum C Programming
    Replies: 2
    Last Post: 10-29-2011, 01:31 AM
  3. Printing out Array Elements
    By BB89 in forum C++ Programming
    Replies: 6
    Last Post: 03-24-2010, 02:00 PM
  4. printing elements of an array using pointers
    By zeebo17 in forum C Programming
    Replies: 3
    Last Post: 05-22-2009, 09:30 PM
  5. printing array elements in ascending order
    By galmca in forum C Programming
    Replies: 29
    Last Post: 10-24-2004, 11:24 PM