if(true) - page 6

 
Georgiy Merts:

I don't know...

In my opinion, both such "empty" blocks and "infinite loops" (whether for or while) are a bad programming style, dangerous for potentially hard-to-calculate errors.

The loop's condition check operator should not be meaningless but bear some burden. If we have an "infinite loop", it means that there are some additional outputs and breaks inside the loop and they are not always obvious. By the way, I don't like break operator either - I always use continue operator in a loop.


And as was said here code obfuscation is just childish... Great programmers-copyists gathered here who are afraid that someone will sell their code or get millions of dollars in other ways... Pride is one of the deadly sins!

do not agree.
Try rewriting this code without the infinite loop and break

int ArrayDeleteVal(int &a[],const int val) 
  {
   int size=ArraySize(a);
   int i=0,start,s,count;
   while(i<size && a[i]!=val) i++; // ищем первый элемент массива со значением val
   if(i==size) return size;
   start=i; i++;
   while(i<size && a[i]==val) i++; // ищем элемент массива со значением, не равным val
   if(i==size) {ArrayResize(a,start); return start;}
   s=i; i++;
   for(;;)
     {
      while(i<size && a[i]!=val) i++; // ищем элемент массива со значением val
      count=i-s;
      if(count>6) { ArrayCopy(a,a,start,s,count); start+=count;} // если нужно скопировать более 6 элементов, то имеет смысл воспользоваться ArrayCopy
      else for(; s<i; start++,s++) a[start]=a[s];                // иначе простой цикл
      if(i==size) break;
      i++;
      while(i<size && a[i]==val) i++; // ищем элемент массива со значением, не равным val
      if(i<size) s=i; else break;
      i++;
     }
   if(start<size) ArrayResize(a,start); else start=size;
   return(start);
  }
 
Nikolai Semko:

don't agree.
Try to rewrite this code without the infinite loop and break

An interesting task.

In my opinion, the presented code is rather "opaque" and difficult to understand, although the structure is clear to me, and the function is useful.

At first sight, the loop should be while (i<size) {....}, but I haven't understood it thoroughly yet.

As soon as I get my hands on it, I'll get to it.

 
Georgiy Merts:

An interesting task.

In my opinion, the presented code is rather "opaque" and difficult to understand, although the structure is clear to me and the function is useful.

At first glance, the loop should be while (i<size) {....}, but I have not thoroughly understood it yet.

As soon as I get around to it, I'll get to it.

The function removes all val values from array a[] and compacts it, removing "holes" from deleted elements without changing the data sequence.

 
Nikolai Semko:

The function deletes all val values from the array a[] and compacts it, removing "holes" from the deleted elements without changing the data sequence.

Yes, yes, I told you - the purpose is clear, the function itself is useful. Now I can't yet, later I'll figure out how it works, and I'll rewrite it without infinite loop. Well, and then - I will write my variant of function, as I would write.

 
Georgiy Merts:

Yes, yes, I told you - the purpose is clear, the function itself is useful. Now I can't yet, later I will sort out my work and rewrite it without infinite loop. Well, and then - I'll write my version of the function, as I would write it.

the code was taken from here. There was a spontaneous competition a year and a half ago.

 
Nikolai Semko:

do not agree.
Try rewriting this code without the infinite loop and break


/// навскидку, даже не проверял
void ArrayDeleteVal(int &arr[],int val)
{
   int size=ArraySize(arr);
   int count=0;   // кол-во удалённых
   for(int i=0;i<size;i++) {
      if (arr[i]==val) count++;
      else if (count) {
         arr[i-count]=arr[count];
      }
   }
   ArrayResize(arr,size-count);
}
 
Maxim Kuznetsov:


Yes it is clear that there are more compact variants, but slower.
Your variant is 2-3 times slower than above and there is an error somewhere, because it gives wrong checksum.
We are talking about the fastest option without HashSet.


2020.04.08 14:15:35.829 ArrayDeleteValue1 (EURUSD,D1)   === Тест с сохранением порядка ===
2020.04.08 14:15:35.829 ArrayDeleteValue1 (EURUSD,D1)   
2020.04.08 14:15:35.977 ArrayDeleteValue1 (EURUSD,D1)   вариант Pastushak   : Контрольная сумма = 7225.757267; элементов - 998994; время выполнения - 144668 микросекунд
2020.04.08 14:15:35.981 ArrayDeleteValue1 (EURUSD,D1)   вариант Korotky     : Контрольная сумма = 7225.757267; элементов - 998994; время выполнения -   2416 микросекунд
2020.04.08 14:15:35.985 ArrayDeleteValue1 (EURUSD,D1)   вариант Fedoseev    : Контрольная сумма = 7225.757267; элементов - 998994; время выполнения -   1675 микросекунд
2020.04.08 14:15:35.988 ArrayDeleteValue1 (EURUSD,D1)   вариант Semko       : Контрольная сумма = 7225.757267; элементов - 998994; время выполнения -    797 микросекунд
2020.04.08 14:15:35.991 ArrayDeleteValue1 (EURUSD,D1)   вариант Nikitin     : Контрольная сумма = 7225.757267; элементов - 998994; время выполнения -   1866 микросекунд
2020.04.08 14:15:35.997 ArrayDeleteValue1 (EURUSD,D1)   вариант Vladimir    : Контрольная сумма = 7225.757267; элементов - 998994; время выполнения -   3453 микросекунд
2020.04.08 14:15:36.006 ArrayDeleteValue1 (EURUSD,D1)   вариант Peter       : Контрольная сумма = 7225.757267; элементов - 998994; время выполнения -   7633 микросекунд
2020.04.08 14:15:36.009 ArrayDeleteValue1 (EURUSD,D1)   вариант fann95      : Контрольная сумма = 7225.757267; элементов - 998994; время выполнения -   1135 микросекунд
2020.04.08 14:15:36.013 ArrayDeleteValue1 (EURUSD,D1)   вариант Kuznetsov   : Контрольная сумма = 7156.067670; элементов - 998994; время выполнения -   2368  микросекунд
2020.04.08 14:15:36.017 ArrayDeleteValue1 (EURUSD,D1)   вариант Kuznetsov1  : Контрольная сумма = 7219.503559; элементов - 1000000; время выполнения -   1874 микросекунд
2020.04.08 14:15:36.021 ArrayDeleteValue1 (EURUSD,D1)   вариант Kuznetsov2  : Контрольная сумма = 7225.757267; элементов - 998994; время выполнения -   2554 микросекунд
2020.04.08 14:15:36.021 ArrayDeleteValue1 (EURUSD,D1)   
2020.04.08 14:15:36.021 ArrayDeleteValue1 (EURUSD,D1)   === Порядок в массиве не сохраняется ===
2020.04.08 14:15:36.024 ArrayDeleteValue1 (EURUSD,D1)   вариант Kuznetsov3  : Контрольная сумма = 7224.813498; элементов - 998994; время выполнения -    735 микросекунд
2020.04.08 14:15:36.027 ArrayDeleteValue1 (EURUSD,D1)   вариант Semko2      : Контрольная сумма = 7224.813498; элементов - 998994; время выполнения -   1408 микросекунд
Files:
 
Georgiy Merts:

I don't know...

In my opinion, both such "empty" blocks and "infinite loops" (whether for or while) are a bad programming style, dangerous for potentially hard-to-calculate errors.

The loop's condition check operator should not be meaningless but bear some burden. If we have an "infinite loop", it means that there are some additional outputs and breaks inside the loop and they are not always obvious. By the way, I don't like break operator either - I always use continue operator in a loop.


And as was said here code obfuscation is just childish... Great programmers-copyists gathered here who are afraid that someone will sell their code or get millions of dollars in other ways... Pride is one of the deadly sins!

Yeah....
 
Maxim Kuznetsov:


Maxim, it doesn't work that way, there should be 2 cycles, otherwise there is no way. First you remove unnecessary elements, then you "shrink" the array upwards (float). The second loop is inside the first one.

 

A for construct involves checking a condition and exiting before the cycle starts.

A while construct involves checking a condition and exiting before the loop ends.

An infinite loop implies checking the condition and exiting at any point in the loop.