Use/return a integer value from a double array

ReLor2  
Hi,

i try to understand the basic things in mql5

So at the moment i want to define a array, for that i do this

double my_double_array[]={"0","0","0"};

now i want to store and return some different kinds of datas

in the first element should a integer value 
my_double_array[0]=0; //only number, not double/float...

in the second a double value

my_double_array[1]=0.00;


At the moment all values returns "0.00" (= double).

I thought double arrays can store integer values too?

Have i do something more to get what i want?

Thanks to make it clear for me  ;-)

Paul Anscombe  
ReLor2:
Hi,

i try to understand the basic things in mql5

So at the moment i want to define a array, for that i do this


now i want to store and return some different kinds of datas

in the first element should a integer value 

in the second a double value


At the moment all values returns "0.00" (= double).

I thought double arrays can store integer values too?

Have i do something more to get what i want?

Thanks to make it clear for me  ;-)

You can store whole numbers as doubles and typecast them to integers.  
You should set the size of the array



ReLor2  

Hello,

if I understand that correctly, is there no way to declare the memory space of the individual array elements as an "integer" type?

I need to specifically format the output of the array element instead? This would mean, however, that this formatting must take place always and everywhere, if the value is saved directly and correctly, this effort would not be necessary.

Isn't there such a possibility or did I misunderstand something? If in doubt, maybe someone can give me a short example of my code above, then it might explain it better? Thanks!

Keith Watford  
ReLor2:
Hi,

i try to understand the basic things in mql5

So at the moment i want to define a array, for that i do this


now i want to store and return some different kinds of datas

in the first element should a integer value 

in the second a double value

Use a struct array.

Docs

Documentation on MQL5: Language Basics / Data Types / Structures, Classes and Interfaces
Documentation on MQL5: Language Basics / Data Types / Structures, Classes and Interfaces
  • www.mql5.com
Structures, Classes and Interfaces - Data Types - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Tobias Johannes Zimmer  
double array[]={1.234, 45.67, 789.0};

for(int i=0; i<ArraySize(array) && !IsStopped; i++;)
Print((int)array[2]);

//Results: 
//1
//45
//789

That is the whole trick to it. But it comes in handy when working with global variables of the terminal. You can store meta information like

-indicator periods or 
-boolean 1(true) or 0(false) or
-enums cast to integers (MQ does this often)
-datetime cast to int format (seconds since january first 1970)

Also if you look at the volumes indicator: tick volume is long format, but the indicator buffer is double. Still works.
Fernando Carreiro  
@Tobias Johannes Zimmer #That is the whole trick to it. But it comes in handy when working with global variables of the terminal. You can store meta information like ... Also if you look at the volumes indicator: tick volume is long format, but the indicator buffer is double. Still works.

You are a long time poster, but you still don't post code properly. Please do so with the "</>" icon or Alt-S.

Tobias Johannes Zimmer  
Fernando Carreiro #:

You are a long time poster, but you still don't post code properly. Please do so with the "</>" icon or Alt-S.

You are right, it is better:

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   double array[] = {1.234, 45.67, 789.0};

   for(int i = 0; i < ArraySize(array) && !IsStopped(); i++)
      Print((int)array[i]);

//Results:
//1
//45
//789
  }
(Fixed some errors btw)
ReLor2  
Keith Watford #:

Use a struct array.

Docs

i think this is my favorite way.

Is it possible to work with this in/for a function?

Can i give this array to a function, if is there a special way or only like above?

void my_function(my_struct_array)
{
//do i haveto return it or can I access of the proceesed values outside from the function to?

}

Keith Watford  
ReLor2 #:

i think this is my favorite way.

Is it possible to work with this in/for a function?

Can i give this array to a function, if is there a special way or only like above?

void my_function(my_struct_array)
{
//do i haveto return it or can I access of the proceesed values outside from the function to?

}

You can pass it by reference.

Reason: