2D array help needed... - page 2

 
William Roeder #:

First thing you must do is decide what your search value or index value is, versus what you are storing.

Ignoring bar index for now, your index value is swing and you are storing price. That would be a one-dimensional array, not two dimensions. You use swing index in and get price out.

In your case, you still use swing index in and get either bar or price out. Therefor, you need a two-dimensional array, not three. The first index is swing index, and the second selects price or bar index out.

I wouldn't use that, as you have two different data types. I would create a single dimension array of a structure of what you are actually storing.

Thanks for the suggestion. Looks like it will be a lot more efficient/easier than my own jungle of arrays.
First of all, apologies for the slow response. Work/life got in the way.

Also, just fyi, I believe you missed a semicolon in your code:

struct Data{ int iBar; double price; };    //missed second to last semicolon :)
Data ZZvalue[];

   ArrayResize(ZZvalue,SwingNr+1);
      ZZvalue[SwingNr].price = ZigZag;
      ZZvalue[SwingNr].iBar  = i;
   SwingNr++;



Now for the embarrassing bit. I've had to read up on structures as this is literally the first time I'm dealing with those.

I think(?) I understand them, but it may be the array part again that's throwing a spanner in the works.

How do I access the data in the structure?
When I look at this https://www.w3schools.com/cpp/cpp_structs.asp , by my reasoning it should be as simple as

 Print(ZZValue[0].price+" --- "+ZZValue[0].iBar);		//access first "data set". value in brackets denotes Swingnr

or 

 Print(ZZValue[5].price+" --- "+ZZValue[5].iBar);		//access fifth "data set". value in brackets denotes Swingnr


But apparently it isn't. :)
Below is the code with my own comments and reasoning... (apologies if it's a tad messy)


Thanks again for any input.



      if(NewBar()==true)
      {
      ObjectsDeleteAll(0);
         DrawSwingPoints();
            
            //int ZZValue[];
            //ArrayResize(ZZValue,sBars);
            double ZZBars[];
            ArrayResize(ZZBars,sBars);
               
         //for( int i = 0; i<Bars; i++)   //FROM MOST RECENT BAR TO OLDEST
         for( int i = Bars-1; i>0; i--)   //FROM OLDEST BAR/SWING TO LATEST
         {
               double ZigZag            =iCustom(Symbol(),_Period,"ZigZag",12,5,3,0,i);
          
               if(ZigZag !=0)
               {
               int ZZValue[];
              // SwingNr++;
               BarNr = i;

               struct Data{ int iBar; double price; };      //Declare named structure Data containing 2 variables 
               Data ZZvalue[];                              //Assign ZZvalue array to Data
               
               ArrayResize(ZZvalue,SwingNr+1);              //Resize array to number of swings +1
                  ZZvalue[SwingNr].price = ZigZag;          //Assign SwingNR to array and corresponding zigzag value to .price
                  ZZvalue[SwingNr].iBar  = i;               //At same swing nr, assign corresponding bar value to .iBar
               SwingNr++;                                   //increase swingnr by 1.
               
               Print(ZZValue[0].price+" --- "+ZZValue[0].iBar);      //Errors: struct or class type expected    at .price and .iBar
               
               //'price' - struct or class type expected        84      33
               //'price' - struct or class type expected        84      33
               //'iBar' - struct or class type expected 84      58
               //'iBar' - struct or class type expected 84      58

               } //End if(ZigZag)
         } //End for
      }     //End NewBar
C++ Structures (struct)
C++ Structures (struct)
  • www.w3schools.com
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
 
MartinS13X #: When I look at this https://www.w3schools.com/cpp/cpp_structs.asp , by my reasoning it should be as simple as

It is that simple.

              int ZZValue[];
              ⋮               
               Print(ZZValue[0].price+" --- "+ZZValue[0].iBar);
But your array is not a struct.
 
William Roeder #:

It is that simple.

But your array is not a struct.

at least I'm not crazy then and it would explain wording of the errors.

Would you care to elaborate on your second statement?

 
MartinS13X #: Would you care to elaborate on your second statement?

Does the highlighting help?

 
William Roeder #:

Does the highlighting help?

Only slightly... but my thinking is that I would have to declare the array would I not? whether I declare it as an int or double does not seem to make any difference.
Other than that I don't know what you mean. Sorry. :)

So I've cleaned things up a bit and tried some more.

      int sBars;
          sBars=Bars;
      int SwingNr=0;
                  
      if(NewBar()==true)
      {
      ObjectsDeleteAll(0);
            
            double ZZValue[];
            ArrayResize(ZZValue,sBars);

         for( int i = Bars-1; i>0; i--)   //FROM OLDEST BAR/SWING TO LATEST
         {
               double ZigZag            =iCustom(Symbol(),_Period,"ZigZag",12,5,3,0,i);
          
               if(ZigZag !=0)
               {
               SwingNr++;
               ZZValue[SwingNr]         = ZigZag;       //ZZValue is assigned ZigZag values (prices) occurring at swing xxx
               //ZZValue[SwingNr]       = i;                    //ZZValue is assigned i value occuring at Swing xxx
               //ZZValue[SwingNr]       = SwingNR;      //ZZValue is assigned SwingNR

               struct Data{ int SwingNr; int iBar; double price; };
               Data ZZvalue;
               
               //ArrayResize(ZZvalue,SwingNr+1);
                  ZZvalue.price = ZigZag;
                  ZZvalue.iBar  = i;
                  ZZvalue.SwingNr = SwingNr;
               
               Print(ZZValue[5]+" --- "+ZZvalue.SwingNr+" ---  "+ZZvalue.price+" --- "+ZZvalue.iBar);
               
               }  //End if(ZigZag)
         }  //End for
      }  //End NewBar


where Print ends up spitting out the correct values because at this point it seems the array is valid and working.:

EURUSD-e,M1: 1.13075 --- 4 ---  1.13358 --- 866
EURUSD-e,M1: 1.13075 --- 3 ---  1.13075 --- 900         //ZZValue[SwingNR] = ZZValue[3] = ZZValue = 1.13075
EURUSD-e,M1: 0 --- 2 ---  1.13179 --- 927
EURUSD-e,M1: 0 --- 1 ---  1.13032 --- 952

But... when I edit the print line as follows:

Print(ZZValue[5]+" --- "+ZZvalue[5].SwingNr+" ---  "+ZZvalue.price+" --- "+ZZvalue.iBar);


I get an " '[' - Array required " error.

I believe I can make the code work as is, but you've peaked my interest in this structure business. :)


 
MartinS13X #: ? whether I declare it as an int or double does not seem to make any difference.
It makes no difference because neither of those types are a struct. What part of “an array of a struct” was unclear? You already posted the correct code at #11.
 
William Roeder #:
It makes no difference because neither of those types are a struct. What part of “an array of a struct” was unclear? You already posted the correct code at #11.

Did I? uh oh... I'll have to check back then.
Probably some very annoying thing out of place.

Thanks for your assistance William. It's much appreciated and been a lot of help.
Gave me some new ideas 

Thinking of actually doing a C++ course. Should help. :)

Reason: