Array of binary - split & converted into population dimensions


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Array of binary - split & converted into population dimensions

  1. #1
    Join Date
    Apr 2004
    Posts
    136

    Array of binary - split & converted into population dimensions

    Ok...super tricky problem & very little time to do it. Actually tomorrow I need it by!!!

    I have a population of binary[] that's needs splitting & converted into 3 dimensions of a population array..

    maybe this will clear things up....

    #define dimension 3
    #define cl 10
    #define popul 10

    parent[popul][cl*dimension]
    population [popul][dimension]


    a parent will hold 30 binary chars....
    I need to take 10 chars & insert this, converted to decimal, into a single dimension of the population array.
    same for 2nd & third dimensions of the population array.

    so... parent[0][n] holds all the binary for [population][0][0]/population[0][1]&population[0][2]

    previously i was working with passing 1 binary string into 1 member of population ...but now everything with the dimensions has made it so **** complicated...Continuing to count the cl binary bits & change to a new population dimension is probving well tricky.... this is what i have so far

    Code:
    for(j = 0; j < popul; j++)
    { //start for1
    
        for(z=0;z<dimension;z++)
        {//for 2
         int t=1;
         int total_val=0;
                cout<<"on popul count "<<j<<" dimension "<<z<<endl;
    
                for(x=z+1*cl; x>cl-1; x--) /////need this to loop cl every 10 bits..after 10 bits the population dimension needs to increment. =/
                { //start for3
                	int bit_val = t;
    
    
    
    		if(parent[j][x]=='1')
                	{
                	val=bit_val+total_val;
               	total_val = val;
               	}
    
             	pop[j][z]=total_val;
            	t=i*2;
    
              }//end for3
    
         }  //for2
    } //end for1


    any help is greatly appreciated

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    Can you ask a specific question?

    as you have in your code, adding a dimension generally just means one more for loop. You seem to have this down pretty well. You can actually usually collapse such convoluted structure to a single dimension for speed (at the cost of somewhat bizzare code) but that sort of thing is done AFTER you have the algorithms down.

    the one thing that seems to be missing .... is your 3-d array? If you want a 3-d system, you need something like

    int three_dee [height][width][depth];
    then you have, of course

    for(h < height)
    for(w < width)
    for(d < depth)
    {
    three_dee[h][w][d] = value; //or whatever you are doing!
    }

    and from here, it depends on how *you* think of the data. Is it a pure 3-d array? An array of 2-d arrays? An array of arrays of arrays? What the data means can change how you process it of course... for anything you *can* make the above loop style work but it may make more sense to do fool with the loops or the swap the order of variables to fit your needs.


    You may also check out a "bitset" left over from C, these are rarely used but may fit some need of yours (a vector of bool or array of bool can work nicely too). Or just a plain old 64 bit int has room for 30 bits, or a string of lenght 30 using '1' and '0' or whatever. If you plan on using the bit operators such as << or ^ or ! etc you should use actual bits though.

  3. #3
    Join Date
    Apr 2004
    Posts
    136
    lol sorry for not asking a specific question!
    I think I have temporarily fixed it but not fixed to the standard that I want...

    A user should be able to modify the dimensions to his satisfaction...but with my temp fix they cannot... #define dimension 3 ... can be changed & the whole program should function as normal...but mine can't =/..

    example..

    dimension=3
    cl = 10
    population = 10

    [cl*dimension] <--------- now holds 30 binary chars --
    [population][dimension] <-------------- 0|45|77|833

    [cl*dimension] = 101011111000111011010111011010 (something like that)
    I need to split this at every predefine cl (10) & convert this into decimal & place this decimal figure into 1 of the populations dimensions....

    so 1 row of cl*dimension fits into 1 row & 3 columns of population...

    As said it's working right now for 3- dimensions...wondering if there's any suggestions that could fix it to work for user dimensional changes...

    Code:
    for (i=0; i<popul; i++)
                  { //for1
                  t=1;
                  total_val=0;
    
                      for (int z=0;z<dimension;z++)
                      { //for2
    
    
                           for (x=cl*dimension-1; x>-1; x--)
                           { //for3
                            
                            bit_val=t;
    
                                          if (x==(cl*2)-1||x==cl-1) ///binary char length at 19 or 9..
                                            {
                                            t=1;
                                            total_val=0;
                                            bit_val=t;
                                            z++;
                                            cout<<"First bit val = "<<bit_val<<endl;
                                            cout<<"x loop position 9- or 19- = "<<x<<"   ";
    
                                                          if(parent[i][x]=='1')       //noticed i must of accidentally took out {  } but code worked fine this way..so..!
                                                            
                                                            total_val=1;
                                                            val=bit_val+total_val;
                                                            pop[i][z]=total_val;
                                                            t=t*2;
    
                                                           cout<<"total added val for bits ="<<total_val<<endl;
                                            }
                                     
                                else //at end of binary char working back to 19
                                {
                                     if(parent[i][x]=='1')
                                     {
                                     cout<<"x loop position = "<<x<<"   ";
                                     val=bit_val+total_val;
                                     total_val = val;
                                     }
    
                                     pop[i][z]=total_val;
                                     t=t*2;
                                     cout<<"total added val for bits = "<<total_val<<endl;
                                 }
    
                          }//e-for3
    
                      }//e-for2
    
                  }//e-for3

    Changing population dimensions seems to be the problem... how could i explicitly tell the function to change population dimensions, if i don't know the size of the dimension?

    if(cl ..... what comparison could pull me out of the loop depending on the binary position & dimension? & also keep track of the binary position.... & shift population dimensions!?

    I know what I got to do...but figuring out worthy...flexible..functioning code is very problamatic

    Any advice is greatly appreciated...but plz don't strain over it! If i don't find a flexible fix i'll just hang out until I go back
    Last edited by process; 12-19-2008 at 05:43 PM.

  4. #4
    Join Date
    Dec 2003
    Posts
    3,366
    If you want resize, you should probably use vector or valarray. These can be made to resize (at great performance cost, and they are slow to begin with). If you want to do that by hand, you can use old C memory functions (realloc) instead of new.

    The old matrix trick is also handy --- instead of allocating 3-d you handle the indexing yourself... for example, a 2-d array can be indexed as single_d[desired_row*max_col + desired_col] and if you need to rearrance it (for example, transpose it) it is far easier because you already have all the memory you need, you just index it differently. You can couple this with the vector or realloc strategy too, allocating a LOT of memory and if you ever need more, reallocate it, but keeping it single dimensional will save much aggravation at the cost of a messy index (which you can fix with a class operator overload, if you so desire, but that is extra "do-nothing" performance penalty code).

    Some combination of this can do what you want.

  5. #5
    Join Date
    Apr 2004
    Posts
    136
    Thanks for your advice again Jonnin. I'm going to be pretty frank by saying my knowledge is pretty limited & the flexibility of what has been programmed so far may also be limited.

    ex: vectors / val-arrays I don't think i've ever used. Likewise with realoc(which seems appealing)...& also matrix...

    This binary to decimal function is part of a genetic algorithm. One the decimal population is filled again it returns to a binary to decimal function..which proceeds to fitness, parent selector,crossover & mutation functions.

    May seem a little crazy going from binary to decimal to a decimal to binary function but the decimal to binary function is one of the first functions in the generation loop which gets it's initial population from random numbers.

    So, the binary to decimal function must return a population[popul][dimension] ... because that's what it's expecting. So the string of 30 binary chars must fill the population in accordance to the populations dimensions(3) .....if the dimensions were 4...the binary char array would be 40 & the population dimensions would hold 4 decimals.

    Is there any recommendations on splitting the char binary array(30...could differ depending on users wants) & converting them to decimal but also putting these decimals into the population dimensions... without adding the complexity of learning new allocation methods. There must be a looping way - a means of conditional statements that will change dimensions on binary length(10)...

    binary(30) ... population 3 dimensions...

    0-9 binary = 1 population dimension... 10-19 = 2nd population dimension... 20-29 = 3rd population dimension
    loop to next binary array row...loop to next population row

    & so on..depending on the input of dimensions

    either way i got a temp fix for now... but desire flexibility..
    I know I need a few conditional statements to transact through each loop stages (depending on the dimensions)

  6. #6
    Join Date
    Dec 2007
    Posts
    401
    wouldn't something simple (like this) suffice?
    Code:
    #include <cstddef>
    
    enum { N_PER_DIM = 10 } ;
    
    void binary_to_decimal( char parent[], std::size_t DIMENSION, int population[][ N_PER_DIM ] )
    {
       for( std::size_t i = 0 ; i < DIMENSION ; ++i )
           for( std::size_t j = 0 ; j < N_PER_DIM ; ++j )
                population[i][j] = parent[ i * N_PER_DIM + j ] - '0' ;
    }
    an example of using the function:
    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    
    enum { N_PER_DIM = 10 } ;
    void binary_to_decimal( char parent[], std::size_t DIMENSION, int population[][ N_PER_DIM ] ) ;
    
    int main()
    {
       std::srand( unsigned( std::time(0) ) ) ;
       std::cout << "how many dimensions? " ;
       std::size_t dimensions ; std::cin >> dimensions ;
    
       char* parent = new char[ dimensions * N_PER_DIM ] ;
       for( std::size_t i = 0 ; i < dimensions * N_PER_DIM ; ++i ) 
             parent[i] = std::rand()&#37;2 == 0 ? '0' : '1' ;
    
       int (*population)[ N_PER_DIM ] = new int[ dimensions ][ N_PER_DIM ] ;
       binary_to_decimal( parent, dimensions, population ) ;
       // etc.
    }

  7. #7
    Join Date
    Dec 2003
    Posts
    3,366
    given what your program does, and what you want it to do... a little interaction may be the easy way out.

    Try this:
    1) ask the user for the max popultion/whatever dimension.
    2) allocate enough memory to support this
    3) just use the big array for any size, even if it is max = 50 and current = 5, you can do that, because array function arguments just pass the first location without regard to its size.
    4) you can set up some logic so 0-9 is 1, 10-19 is 2, ... integer division could be used for example. X/10 +1 works for that, or some variation to cover the exact range you wanted will do it without a bunch of if statements to cover all the cases. Test whatever logic you use in a small stub program to make sure it does exactly what you wanted it to -- I keep a small project for exactly that sort of testing.

    Will that do the trick for you?

Similar Threads

  1. Replies: 26
    Last Post: 12-01-2012, 04:12 AM
  2. Reversing an Array
    By Tmcclain in forum Java
    Replies: 7
    Last Post: 02-13-2009, 10:57 PM
  3. Generating a random binary array
    By the_bee in forum Java
    Replies: 2
    Last Post: 12-20-2005, 01:59 PM
  4. redim help !!!
    By angelito in forum VB Classic
    Replies: 1
    Last Post: 11-21-2005, 06:16 AM
  5. adding split into an array
    By chrisgeary in forum ASP.NET
    Replies: 2
    Last Post: 08-04-2005, 11:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center