Errors, bugs, questions - page 1438

 
Сергей Таболин:

And when the question with array size will be solved, I still think you should see what values pr_open and pr_close have when they are called for the first time.


P./S.: They are not initialised here:

...
int            buf_lup[], buf_ldn[];
...
void OnTick()
{
   int      lup, ldn, sup, sdn;
   int      sum_up, sum_dn;
   double   pr_open, pr_close;

.........

      ldn = 0;
      sup = 0;
      sdn = 0;
      sum_up = 0;
      sum_dn = 0;

.........

               buf_ldn[ldn] = (int)((pr_open - pr_close) / Point()); // ОШИБКА тут
               sum_dn += buf_ldn[ldn++];

........
}

and you can't see in the code that anything is assigned to them.

Although maybe their initialization is hidden somewhere in these:

.........

lines of code you have hidden.

 
Сергей Таболин:

Exactly ))) There's no problem with them.

In general, it's kind of inconvenient to keep track of the size of a dynamic array all the time... You need to know how much it weighs. What's the point of it then? ....

The point is that if you don't know in advance how many elements will be put into it, there's no need to declare a static array of a guaranteed larger size (with plenty of margin, so that it'll last for all occasions) and waste memory. Simply, as new data needs to be added to the array, you dynamically increase its size.
 
Сергей Таболин:

In principle, this is understandable. The only thing I don't understand is why this array cannot be incremented automatically.

Why couldn't it be done that way? It's not my idea )))) That's exactly how I tried to use a dynamic array...

Plus I additionally have to save the index... Not good...

Who says you can't dynamically enlarge it?

Here to help you

ArrayResize

Устанавливает новый размер в первом измерении массива



int  ArrayResize( 
   void&  array[],              // массив, переданный по ссылке 
   int    new_size,             // новый размер массива 
   int    reserve_size=0        // резервное значение размера (избыточное) 
   );
 

Параметры

array[]

[out]  Массив для изменения размеров.

new_size

[in]  Новый размер для первого измерения.

reserve_size=0

[in]  Необязательный параметр. Размер для дополнительного резерва.

Возвращаемое значение

При успешном выполнении функция возвращает количество всех элементов, содержащихся в массиве после изменения размера; в противном случае возвращает -1 и массив не меняет размеры.

Примечание

Функция может быть применена только к динамическим массивам. При этом необходимо иметь ввиду, что нельзя изменять размер для динамических массивов, назначенных в качестве индикаторных буферов функцией SetIndexBuffer(). Для индикаторных буферов все операции по изменению размера производит исполняющая подсистема терминала.

Общее число элементов в массиве не может превышать 2147483647.

При частом распределении памяти рекомендуется использовать третий параметр, задающий резерв для уменьшения количества физического распределения памяти. Все последующие вызовы функции ArrayResize не приводят к физическому перераспределению памяти, а только меняется размер первого измерения массива в пределах зарезервированной памяти. Следует помнить, что третий параметр будет использоваться только тогда, когда будет происходить физическое распределение памяти, например:



ArrayResize(arr,1000,1000); 
for(int i=1;i<3000;i++) 
   ArrayResize(arr,i,1000);
 

В данном случае произойдёт 2 перераспределения памяти, один раз до входа в цикл на 2000 элементов, при этом размерность массива будет установлена в 1000 и второй при i равной 2000. Если третий параметр опустить, то произойдёт 2000 физических перераспределения памяти и это замедлит выполнение программы.

We should do a help on how to use the help )))))))))))))))))))))

 
Сергей Таболин:

In principle, this is understandable. The only thing I don't understand is why this array cannot be incremented automatically.

int arr[];

arr[] = 1;
arr[] = 9;
arr[] = 3;
....

Why couldn't it be done that way? It's not my idea )))) That's exactly how I tried to use a dynamic array...

Plus I additionally have to save the index... Not good...

I don't understand what you were trying to do in this code.

Here, sketched you a little example showing what dynamic arrays are for and how to use them.

   // динамический массив строк, в который поместим считанные из файла "FileName.txt" строки,
   // количество строк в файле нам неизвестно
   string Strings[];
   ArrayResize(Strings, 0, 20);   // сейчас это массив нулевой длины
   
   // открываем текстовый файл для чтения
   int hFile = FileOpen("FileName.txt", FILE_READ|FILE_TXT);
   
   if(hFile != INVALID_HANDLE)
   {
      // каретку в начало файла
      FileSeek(hFile, 0, SEEK_SET);
      
      // вспомагательные переменные
      string sString;
      int nTotalStrings = 0;
      
      // читаем файл построчно
      while(!FileIsEnding(hFile) && !IsStopped())
      {
         // считываем очередную строку
         sString = FileReadString(hFile);
         
         // добавляем считанную строку в массив, предварительно увеличив его размер на 1
         if(sString != "")
         {
            nTotalStrings++;
            ArrayResize(Strings, nTotalStrings, 20);
            Strings[nTotalStrings-1] = sString;
         }
      }
      
      // уходя, гасите свет
      FileClose(hFile);
   }
   
   // выведем считанные строки в журнал для проверки
   for(int i = 0; i < ArraySize(Strings); i++)
      Print(Strings[i]);
 
Сергей Таболин:

I don't understand why this array cannot be incremented automatically.

Because language developers were brought up on Syakh, and this severely hinders MQL's transition from hardcore SI-like to popsy PHP/JS-like. In plebeian PHP one simply assigns a new value and the array adjusts its size automatically. MQL makes the coder feel its importance. So take a shovel and enjoy being a member of the chosen professionals
 
Сергей Таболин:

This is, in principle, a simple example of how a dynamic array should normally be filled. I haven't written in C for a long time, I don't remember, but that's how arrays are filled in php! Everything is logical and understandable. If I add an element to an array (arr[] = x), the array is automatically expanded, and the element is added to the end of the array. And we don't have to stretch it out by ourselves, and we don't have to specify the element's index by ourselves. But here we have to make absolutely unnecessary movements:

the difference is obvious...

In my opinion, it's strange, to say the least ))))

Well, then take a shovel and write your own implementation of dynamic arrays, which will work exactly that way, what's stopping you? )) Although, to overload [] for assignment MKL won't allow, it's not hardcore enough. ))
 
Сергей Таболин:

This is, in principle, a simple example of how a dynamic array should normally be filled. I haven't written in C for a long time, I don't remember, but that's how arrays are filled in php! Everything is logical and understandable. If I add an element to an array (arr[] = x), the array is automatically expanded, and the element is added to the end of the array. And we don't have to stretch it out by ourselves, and we don't have to specify the element's index by ourselves. But here we have to make absolutely unnecessary movements:

the difference is obvious...

I find it strange, to say the least ))))

Who prevents us from using OOP and implementing similar syntax?

#property strict

/*
enum TestEnum
{
  zero,
  one,
  two,
  three,
  four,
  five,
  six,
  seven,
  eight,
  nine,
  ten,
  eleven,
  twelve,
  thirteen,
  fourteen,
  fitteen
};
*/

template <typename T>
class RubbArray
{
  private:
    T data[];
    
  protected:
    void add(T d)
    {
      int n = ArraySize(data);
      ArrayResize(data, n + 1);
      data[n] = d;
    }
    
  public:
    
    T operator[](int i) const
    {
      return data[i];
    }
    
    RubbArray *operator<<(T d)
    {
      add(d);
      return GetPointer(this);
    }

    T operator=(T d)
    {
      add(d);
      return d;
    }

    void operator=(const RubbArray &d)
    {
      int i, n = d.size();
      ArrayResize(data, n);
      for(i = 0; i < n; i++)
      {
        data[i] = d[i];
      }
    }

    T operator>>(int i)
    {
      T d = this[i];
      ArrayCopy(data, data, i, i + 1);
      ArrayResize(data, ArraySize(data) - 1);
      return d;
    }
    
    int size() const
    {
      return ArraySize(data);
    }
    
    void print() const
    {
      int i, n = ArraySize(data);
      string s;
      for(i = 0; i < n; i++)
      {
        s += (string)data[i] + ",";
      }
      Print(s);
    }
};

void OnStart()
{
  //RubbArray<TestEnum> d;
  RubbArray<double> d, d2;
  d << 5 << 7;
  d = 10;
  d << 15;
  d.print();
  Print(d[1]);
  double x = d >> 1;
  d2 = d;
  d2.print();
}
 
Alexander Puzanov:
Because language developers were brought up on Syakh, it severely hinders MQL's transition from hardcore SI-style to popsy PHP/JS-like. In plebeian PHP one simply assigns a new value and the array adjusts its size automatically. MQL makes the coder feel its importance. So, take a shovel and enjoy being a member of the select few professionals.
In my opinion, a simple array should not "adjust its size" without the programmer being aware of it. If such behavior is necessary, a corresponding class must be written. And then you can use a "smart" array without any problems. And simple structures, which the array initially belongs to, must not have "independent decision rights".
 
Four people have already told you - get a shovel and write your class, since you need it so badly. ))
 
Сергей Таболин:

"without the programmer's knowledge" does not work. It is the programmer who gives the command "add an element to the end of the array" (arr[] = x). And the array has no "independent decision rights". Only by executing the programmer's command does it increase its size, thus relieving the programmer from keeping track of that very size. )))

As my experience shows, if the programmer writes something into an array containing 10 items with index 20, it most likely means that he/she made a mistake, and the program should generate an error, but not increase the size of the array, thereby hiding a suspicious action.

All these "arrays that tweak the size" have far more problems with hiding potential errors than they do with convenience. What's more, and not having to keep track of the array size is also a potentially dangerous practice, leading to hard-to-fix bugs.

Reason: