Combination program problem

This is a discussion on Combination program problem within the C++ Programming forums, part of the General Programming Boards category; Hello, I was thinking of writing a program that writes all numbers of one combination in all orders,just for practice ...

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

    Question Combination program problem

    Hello, I was thinking of writing a program that writes all numbers of one combination in all orders,just for practice purpose :

    (1,2,3):

    1 2 3
    1 1 2
    1 2 3

    and so on...



    The program I wrote like this:

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std ;
    template <class t>
    vector<t> assign(vector <t> array,short co,short kam)
    {
           vector <t> copy_array=array ;
           copy_array[kam]=array[co] ;
           return copy_array ;
    }
    
    
    template <class t>
    void comb(vector <t>array,short place,short size) //parameters -the array I am working on, itīs position, and itīs length
    {
          if (place==array.size()-1 ) //if the position is zero
          for (short x=0;x<size;x++) //then it is written on the screen
          cout<<array[x]<<endl ;            
          
          for (short tocopy=0;tocopy<size;tocopy++)  
          comb(assign(array,tocopy,place),place+1,size) ; //the very proccess of assigning all values
    }
          
          
          
    //clear
    int main()
    {vector <int> test ;
    test.push_back(1) ;
    test.push_back(2) ;
    test.push_back(3) ;
    short int size= test.size() ;
            comb(test,0,size) ;
        
    
        cin.get() ;
        return 0 ;
    }
    However, this program ends up in runtime error, would you please at least try to tell me, where is the problem?

    Thanks
    Garland

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I changed your for loop to this:
    Code:
          for (short tocopy=0;tocopy<size;tocopy++) { 
              cout << "tocopy: " << tocopy << endl
                  << "place: " << place << endl
                  << "size: " << size << endl;
              comb(assign(array,tocopy,place),place+1,size) ; //the very proccess of assigning all values
          }
    The output then was
    Code:
    tocopy: 0
    place: 0
    size: 3
    tocopy: 0
    place: 1
    size: 3
    1
    1
    3
    tocopy: 0
    place: 2
    size: 3
    tocopy: 0
    place: 3
    size: 3
    tocopy: 0
    place: 4
    size: 3
    tocopy: 0
    place: 5
    size: 3
    tocopy: 0
    place: 6
    size: 3
    tocopy: 0
    place: 7
    size: 3
    tocopy: 0
    place: 8
    size: 3
    tocopy: 0
    place: 9
    size: 3
    tocopy: 0
    place: 10
    size: 3
    tocopy: 0
    place: 11
    size: 3
    tocopy: 0
    place: 12
    size: 3
    tocopy: 0
    place: 13
    size: 3
    tocopy: 0
    place: 14
    size: 3
    tocopy: 0
    place: 15
    and so on until it finally segfaulted at place=1994. In other words, you have an infinite loop. You never check to make sure place+1 is less than size.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    15

    A word(or two) of gratitude

    Thanks a lot for even bothering to take a look at the program, I can now, thanks to you, see where I did the mistake, so thanks again and have a nice day(or two).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 10:10 AM
  2. problem w/ doubles in friend's program
    By mkylman in forum C Programming
    Replies: 16
    Last Post: 11-22-2008, 09:45 AM
  3. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 01:34 AM
  4. Some Problem With My Program
    By Americano in forum C Programming
    Replies: 5
    Last Post: 10-18-2003, 01:58 AM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM

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