Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 1134
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello. If you don't know beforehand how many elements will be in the array, how do you declare the array so that the array increases with each new element?
An example to make it clearer:
for(int i=0; i<1000; i++)
{
if(A[i]<B[i])
HighA[i]=A[i];
}
Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));
Hello. If you don't know beforehand how many elements will be in the array, how do you declare the array so that the array increases with each new element?
An example to make it clearer:
for(int i=0; i<1000; i++)
{
if(A[i]<B[i])
HighA[i]=A[i];
}
Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));
You should read the documentation more often.
I've read and tried but I get an "array out of range" error. In the example it looked like this:
for(int i=0; i<1000; i++)
{
if(A[i]<B[i])
{
countHlines++;
ArrayResize(HighA,countHlines);
HighA[i]=A[i];
}
}
Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));
I've read and tried but I get an "array out of range" error. On the example it looked like this:
for(int i=0; i<1000; i++)
{
if(A[i]<B[i])
{
countHlines++;
ArrayResize(HighA,countHlines);
HighA[i]=A[i];
}
}
Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));
When condition A[i] < B[i] is not fulfilled, the array size remains unchanged and the loop counter is incremented. That's why it is out of range.
Try it this way.
for(int i=0; i<1000; i++)
{
if(A[i]<B[i])
{
int size = ArraySize(HighA);
ArrayResize(HighA, size+1);
HighA[size]=A[i];
}
}
Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));
When the A[i] < B[i] condition is not satisfied, the array size remains unchanged and the loop counter is incremented. That's why it is out of bounds.
Try it this way.
for(int i=0; i<1000; i++)
{
if(A[i]<B[i])
{
int size = ArraySize(HighA);
ArrayResize(HighA, size+1);
HighA[size]=A[i];
}
}
Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));
I've read and tried but I get an "array out of range" error. In the example it looked like this:
for(int i=0; i<1000; i++)
{
if(A[i]<B[i])
{
countHlines++;
ArrayResize(HighA,countHlines);
HighA[i]=A[i];
}
}
Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));
Well that would work faster
double HighA[];
for(int i=0; i<1000; i++)
{
if(A[i]<B[i])
{
countHlines++;
ArrayResize(HighA,countHlines);
HighA[countHlines-1]=A[i];
}
}
Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));
// Вариант №2
int countHlines=-1;
double HighA[];
for(int i=0; i<1000; i++)
{
if(A[i]<B[i])
{
countHlines++;
ArrayResize(HighA,countHlines+1);
HighA[countHlines]=A[i];
}
}
Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));
Well, it will work faster that way
double HighA[];
for(int i=0; i<1000; i++)
{
if(A[i]<B[i])
{
countHlines++;
ArrayResize(HighA,countHlines);
HighA[countHlines-1]=A[i];
}
}
Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));
// Вариант №2
int countHlines=-1;
double HighA[];
for(int i=0; i<1000; i++)
{
if(A[i]<B[i])
{
countHlines++;
ArrayResize(HighA,countHlines+1);
HighA[countHlines]=A[i];
}
}
Print(">>>>>>>>>>>..ArraySize(HighA)= ",ArraySize(HighA));
Thank you, I see! It will be faster because of ArraySize, does it slow it down somehow?
Not much, but it's clear and error-proof.