Who knows the solution (for loops / if statements)

 

Hello,

Could someone suggest me a better way of doing this:

for(int A =0; A<50;A++){
   for(int B = 0; B<50; B++){
      for(int C = 0; C<50; C++){
         for(int D = 0; D<50; D++){
            if(A!=B && A!=C && A!=D && B!=C && B!=D && C!=D){
             // do something..
            }
         }
      }
   }
}

Cause I think this is the stupid way to do something which can be done in a better way..

Thanks!

 
  1. for(int A = 3; A<50;A++)
    for(int B = 2; B<A; B++)
    for(int C = 1; C<B; C++)
    for(int D = 0; D<C; D++){
       // do something..
    }
    
  2. Perhaps you should state what you're trying to do instead of asking for a less "stupid way to do something."
 
WHRoeder:
  1. Perhaps you should state what you're trying to do instead of asking for a less "stupid way to do something."


Hi WHRoeder,

Thanks for the reply. I'll think about it.

To answer to your suggestion, I'm trying to make all possible combinations of 8 items of 74 different items without using an item more than once in a single combination.

Forgot to mention, that the position of an item in a combination is not relevant.

Meaning if there is already a combination ABCD then DCBA should not be added...

 
  1. Combinations not permutations the code basically does like the diagram on Combination - Wikipedia, the free encyclopedia (Permutations means position is important.)
  2. 74 items taken 8 at a time is 11,969,016,300. (Permutations jumps to 482,590,739,000,000.)
  3. Let me guess - 74 different indicators as filters. The 8 is arbitrary. Try using bitmaps:
       #define IND01    1
       #define IND02    2
       #define IND03    4
       #define IND04    8
       #define IND05   16
       #define IND06   32
       #define IND07   64
       #define IND08  128
    extern int bm1 = 255;   // Tester start=0, step=1, max=255
       #define IND09    1
       #define IND10    2
       #define IND11    4
       #define IND12    8
       #define IND13   16
       #define IND14   32
       #define IND15   64
       #define IND16  128
    extern int bm2 = 255;
    :
    bool useInd01(){ return(bm1 & IND01); // Bitwise and (not logical and)
    :
    bool useInd16(){ return(bm2 & IND16);
    replace 01, 02, ... with meaningful names.
 
WHRoeder:
  1. Let me guess - 74 different indicators as filters. The 8 is arbitrary. Try using bitmaps: replace 01, 02, ... with meaningful names.


Spot on. Though not all indicators can work together. See it as a list of restrictions:

use indi7 only if indi1 is already in the combination

don't use indi8 if indi4 is already in the combination

Would bitmaps still be a good solution? If yes I'll study it - know nothing about it atm.

Thanks for your help!

 
bool useInd07(){ return(bm1 & IND07 != 0 &&  useInd01() ); // use indi7 only if indi1 is already in the combination
bool useInd08(){ return(bm1 & IND08 != 0 && !useInd04() ); // don't use indi8 if indi4 is already in the combination 
 
There might be strong correlation between some of those 74 indicators, what means using them together is useless in most of the cases. Computing a correlation matrix would show which ones you don't really need.
 

So I've been trying to get the hang of working with bitmaps.

   #define IND01    1
   #define IND02    2
   #define IND03    4
   #define IND04    8
   #define IND05   16
   #define IND06   32
   #define IND07   64
   #define IND08  128
extern int bm1 = 255;   // Tester start=0, step=1, max=255
   #define IND09    1
   #define IND10    2
   #define IND11    4
   #define IND12    8
   #define IND13   16
   #define IND14   32
   #define IND15   64
   #define IND16  128
extern int bm2 = 255;
:
bool useInd01(){ return(bm1 & IND01); // Bitwise and (not logical and)
:
bool useInd16(){ return(bm2 & IND16);

In this example I don't see how a function like useInd01 can return different values ? I also don't see what bm1 and bm2 are doing. So I tried to make an easy example to test things out:

   #define IND01    1
   #define IND02    2
   #define IND03    4  
   #define IND04    8  
   #define IND05    16  

int init()
  {  
  start();
   return(0);
  } 
int deinit()
  { 
   return(0);
  } 
int start()
  { 
  string CMNT = "";
  for(int X=0; X<32;X++){
   CMNT = CMNT + "\n Combi "+X+" = "+useInd01(X)+" "+useInd02(X)+" "+useInd03(X)+" "+useInd04(X)+" "+useInd05(X); 
  }
  Comment(CMNT);
   return(0);
  } 
  
   
bool useInd01(int CC){ return(CC & IND01); } 
bool useInd02(int CC){ return(CC & IND02); } 
bool useInd03(int CC){ return(CC & IND03); }  
bool useInd04(int CC){ return(CC & IND04); }  
bool useInd05(int CC){ return(CC & IND05); } 
 

This does seem to make different combinations although I don't really understand what I'm doing (I'm just trying things out). On the chart you get different combinations printed with values on each of the 5 entries. So I could take 0 = off and != 0 is on and then I can use it to determine at which combination which indicator should be used.

Could someone explain the differences between the two structures above?

 
ForexAlpha:
extern int bm1 = 255;   // Tester start=0, step=1, max=255
In this example I don't see how a function like useInd01 can return different values ? I also don't see what bm1 and bm2 are doing.
What part of "Tester start=0, step=1, max=255" was unclear? The TESTER is varying bm1 from 0 to 255 and selecting all possible combinations of indicators. For any SINGLE run bm1 is constant and useInd01 will be also.
Reason: