Clearing an array of defined element(s) - page 18

 
Vladimir Pastushak:
I figured it out, it was the field shift in the structure, not the whole structure...

If the structure is a simple type, it should work.

 

Another nuance. I have proved that my algorithm works correctly. I printed out the values that I got after the function was executed. How well the other algorithms work is a mystery.

The checksum seems to prove correct though.

 
Реter Konow:

ZS. You did make one mistake when writing the function according to my solution, though:

My function is not

а

But thanks anyway.

No, I didn't make a mistake, I corrected yours. Think well, what does your line do? Just try and change it and you'll see. It will be the wrong checksum and the wrong size.
 
Реter Konow:


ZS. You did make one mistake when writing the function according to my solution, though:

My function is not

а


in your code it's the same as

 
Nikolai Semko:
No, I didn't make a mistake, I corrected yours. Think well, what does your line do? It doesn't do anything. Just try and change it and you'll see. It will be the wrong checksum and the wrong size.

The result is exactly the same. There is no error.

Yes, it doesn't do anything. But it makes more sense with it. ))

 
Stanislav Dray:

in your code, it equals

Yes.

 
Реter Konow:

Yes.

Oh yeah, sorry.

I remembered.
Your code was taken from here.

Your line wasn't there and I remember that line I had to add to make it work properly.

Tag Konow:

But it makes more sense with it. ))

There's an extra mathematical operation in it. ))

 

Question. Why in this code, after changing the size of the array, it still prints the same as before the change?


//+------------------------------------------------------------------+
//|                                             Erase and Resize.mq5 |
//|                                                      Peter Konow |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Peter Konow"
#property link      "https://www.mql5.com"
#property version   "1.00"
//--------------------------------------------------------------------
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   int Arr[20] = {1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2};
   ulong q1 = GetMicrosecondCount(); 
   //--------------------------------
   PeterArray(Arr,3);
   //--------------------------------
   ulong q2 = GetMicrosecondCount();
   //--------------------------------
   Print("Array new size  ",ArraySize(Arr),"  Тime of operation  ",q2-q1);
   ArrayPrint(Arr); 
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int PeterArray(int &Arr[],int val) // вариант Peter Konow
  {
   int deleted=0,q=0;
   for(int a1=0; a1<ArraySize(Arr); a1++)
     {
      if(deleted)Arr[q]=Arr[q+deleted];
      if(Arr[q]==val){deleted++; q--;}
      q++;
     }
   ArrayResize(Arr,ArraySize(Arr) - deleted);
   return (q);
  }
//+------------------------------------------------------------------+

This calls into question the validity of the checksum checking algorithms. So, when we calculate the checksum, we make a loop through the array that still keeps its previous elements?

I suggest checking the algorithms my way. By outputting an array of 20 values.

This will be a check for correctness, not speed.
 
Реter Konow:

Question. Why in this code, after changing the size of the array, it still prints the same as before the change?


This calls into question the validity of the checksum checking algorithms. So, when we calculate the checksum, we make a loop through the array that still keeps its previous elements?

I suggest checking the algorithms my way. By outputting an array of 20 values.

This will be a test of correctness, not speed.
I suggest a million, as there are a million in the test. Let everyone sit and compare.))
 
Nikolai Semko:
I suggest a million, since there are a million in the test. Let all sit and compare))).

The checksum is not calculated correctly.

Check it yourself. It counts the elements that remain in the deleted part of the array.

//+------------------------------------------------------------------+
//|                                             Erase and Resize.mq5 |
//|                                                      Peter Konow |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Peter Konow"
#property link      "https://www.mql5.com"
#property version   "1.00"
//--------------------------------------------------------------------
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   int Arr[20] = {1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2};
   ulong q1 = GetMicrosecondCount(); 
   //--------------------------------
   PeterArray(Arr,3);
   //--------------------------------
   ulong q2 = GetMicrosecondCount();
   //--------------------------------
   Print("Array new size  ",ArraySize(Arr),"  Тime of operation  ",q2-q1,"  Контрольная сумма: ",ControlSumm(Arr));
   
   ArrayPrint(Arr); 
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int PeterArray(int &Arr[],int val) // вариант Peter Konow
  {
   int deleted=0,q=0;
   for(int a1=0; a1<ArraySize(Arr); a1++)
     {
      if(deleted)Arr[q]=Arr[q+deleted];
      if(Arr[q]==val){deleted++; q--;}
      q++;
     }
   ArrayResize(Arr,ArraySize(Arr) - deleted);
   return (q);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
long ControlSumm(int &a[]) // суммирование всех элементов массива (контрольная сумма)
  {
   long sum=0;
   for(int i=0; i<ArraySize(a); i++) sum+=a[i];
   return sum;
  }
//+------------------------------------------------------------------+

Of course, this is not yet proof that the check is wrong, but there is already some doubt.


The checksum is calculated from the elements of that row:

2018.11.16 14:36:28.456 Erase and Resize (USDJPY,H1)    1 2 1 2 1 2 1 2 1 2 1 2 1 2 3 1 2 3 1 2

And the last 6 elements were previously deletedby ArrayResize.

But the function still counts them anyway.