CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2012
    Posts
    2

    Question Generating all possible numbers

    Hi guys. I'm sure this is pretty simple, but I'm stumped for a way to do this. Essentially if I have an array with P collumns and V^P rows, how can I fill in all the combinations, that is, essentially, all possible numbers in base V of P digits. For example, for P=3 and V=2

    000
    001
    010
    011
    100
    101
    110
    111

    Keep in mind that this is an 2 dimensional array, not an array of ints.

    For P=4 and V=3.

    0000
    0001
    0002
    0010
    0011
    0012
    ....

    Having this array generated, the rest of work for what I'm trying to devolop is trivial. So having some code/tips on how to do this would be greatly appreciated. Thanks.


    This is a code I've written that should do the trick, but for some reason it gives this error on compiling: "main.cpp:65: error: invalid types `double[3][9][double]' for array subscript"

    Code:
    for(i=0;i<P;i++) {
            for(j=0;j<pow(V,i);j++) {
                for(k=0;k<V;k++) {
                    for(l=0;l<pow(V,(P-i-1));l++) {
                        a[i][(j*V*pow(V,P-i-1) + l + k*pow(V,P-i-1))];
                    }
                }            
            }
        }
    Any help would be welcome.

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: Generating all possible numbers

    Quote Originally Posted by andrepd View Post
    error: invalid types
    It's a type error. Pow returns a floating point and an integer is expected.

  3. #3
    Join Date
    Apr 2012
    Posts
    2

    Re: Generating all possible numbers

    Yes, if I cast the result of "pow" to short it works fine.

    However, the algorithm produces odd unexpected results. Does anybody know of an algorithm to perform what I described in the OP (filling in the with all possible value combinations)?

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Generating all possible numbers

    Quote Originally Posted by andrepd View Post
    However, the algorithm produces odd unexpected results.
    Looks like one more of these ubiquotious floating point oddities that have been discussed many times around here. For instance, here's a discussion going on right now: http://forums.codeguru.com/showthread.php?t=523168

    Generating all possible symbol combinations has been discussed quite some times as well. For instance, this may be interesting: http://forums.codeguru.com/showthread.php?t=508086

    More related discussions can be found using a forum search.
    Last edited by Eri523; April 23rd, 2012 at 08:41 AM. Reason: Fixed the forum search link
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Generating all possible numbers

    Quote Originally Posted by Eri523 View Post
    For instance, this may be interesting: http://forums.codeguru.com/showthread.php?t=508086
    It's funny, because back when I posted my solution, it looked like total overkill for binary counting (and was), however, if you want to count in any other base (eg, 3), then it works perfectly. My old code, parametrized with the OP's P and V:

    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    #include <limits>
    
    template <typename iter>
    bool increment(iter first, iter last,
                   typename std::iterator_traits<iter>::value_type min = std::numeric_limits<typename std::iterator_traits<iter>::value_type>::min(),
                   typename std::iterator_traits<iter>::value_type max = std::numeric_limits<typename std::iterator_traits<iter>::value_type>::max())
    {
      while((first!=last) && (*first == max))
      {
        *first = min;
        ++first;
      };
      if(first!=last)
      {
        ++*first;
        return true;
      }
      return false;
    }
    
    const int P = 4;
    const int V = 3;
    
    int main()
    {
      std::vector<int> vect(P, 0);
      do
      {
        std::copy(vect.rbegin(), vect.rend(), std::ostream_iterator<int>(std::cout));
        std::cout << std::endl;
      } while(increment(vect.begin(), vect.end(), 0, V-1));
    }
    generates:

    Code:
    0000
    0001
    0002
    0010
    0011
    0012
    0020
    ...
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Generating all possible numbers

    Quote Originally Posted by andrepd View Post
    Yes, if I cast the result of "pow" to short it works fine.

    However, the algorithm produces odd unexpected results. Does anybody know of an algorithm to perform what I described in the OP (filling in the with all possible value combinations)?
    Since int is the "natural" integer it's better to cast to int actually.

    Regarding the algorithm. It's just to generate all numbers between 0 and V^P - 1. That would be the rows. From each row number you extract P digits in base V and place in the colums.
    Last edited by nuzzle; April 23rd, 2012 at 03:42 PM.

  7. #7
    Join Date
    Jan 2012
    Location
    toronto
    Posts
    13

    Re: Generating all possible numbers

    Generating all possible numbers?
    I thought this was some sort of philosophical thing when I clicked here

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured