multidimensional array - Recursive call instead of multiple for loop calls C++ -


on following example, 5 loops able obtain each individual index access multi dimensional array. there way dynamic systems if don't know dimension of array, implement , recursive function access array. => recursively implementing loop.

for example:

index[3] = 0; //=> because 3 dimenional 0  void recursiveindexwalk(int i){     a[0] = i;     recursiveindexwalk(a[1]);     //on inner last recursive call a[index[0]][index[1]][index[2]] = val;  }  main(){ //my goal perform instead of 3 loops => 1   recursive() called recursive(a[0]); } 

//========================

unsigned int dimensions = 3;    //known   unsigned int dimension_length = { 1, 2, 3}; //known   int a[1][2][3];  int counter = 0; (size_t r = 0; r < 1; r++) {            //|      (size_t q = 0; q < 2; q++) {       //|        (size_t p = 0; p < 4; p++) {      //|=>recursiveforcall(indexarray)               a[i][j][k] = counter++;          }         }        } 

you could, example, create vector containing index ids, each in each recurse add index list before calling recursive function remove list after.

however, it's better idea forgo recursion idea here , instead @ permutations using iterative technique. here's arbitrary proof of concept, should give ideas:

#include <stdio.h>  const unsigned int dimensions = 3; const unsigned int dimension_size[dimensions] = { 2, 4, 3 };  // first permutation. inline void get_first_permutation( unsigned int * const permutation ) {     ( int = 0; < dimensions; ++i )         permutation[i] = 0; }  // returns false when there no more permutations. inline bool next_permutation( unsigned int * const permutation ) {     int on_index = dimensions - 1;     while ( on_index >= 0 )     {         if ( permutation[on_index] >= dimension_size[on_index] )         {             permutation[on_index] = 0;             --on_index;         }         else         {             ++permutation[on_index];             return true;         }     }     return false; }  // print out permutation. void print_permutation( const unsigned int * const permutation ) {     printf( "[" );     ( int = 0; < dimensions; ++i )         printf( "%4d", permutation[i] );     printf( "]\n" ); }  int main( int argc, char ** argv ) {     // first permutation.     unsigned int permutation[dimensions];     get_first_permutation( permutation );      // print permutations.     bool more_permutations = true;     while ( more_permutations )     {         print_permutation( permutation );         more_permutations = next_permutation( permutation );     }      return 0; } 

this is, of course, assuming need know indices. if you're trying update counters can loop index 0 index dim_0*dim_1*...*dim_n

/* defined somewhere. don't know how gets allocated what-evs. :) */ unsigned int dimensions = 5; unsigned int dimension_length = { 1, 2, 4, 7, 6 }; int * int_array = ...  /* loop code. */ unsigned int array_length = 0; ( unsigned int d = 0; d < dimensions; ++d )     array_length += dimension_length[d]; int *array_pointer = int_array; ( unsigned int = 0; < array_length; ++i, ++array_pointer )     array_pointer = counter++; 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -