Hi,

I have a List containing double values and a double mean.

I need to check and store all 5 consecutive values (in indivuals lists) that are higher that mean value.

At this moment my solution always clear my aux List :( any ideas?

double media = 1.8815;
List<double> lista = new List<double>();

lista.Add(2.201);
lista.Add(2.101);
lista.Add(2.309);
lista.Add(2.091);
lista.Add(1.678);
lista.Add(1.825);
lista.Add(2.15);
lista.Add(2.251);
lista.Add(1.937); 
lista.Add(1.89);
lista.Add(2.07); 
lista.Add(1.456);
lista.Add(1.808);
lista.Add(1.741);
lista.Add(2.04);
lista.Add(2.296);
lista.Add(1.456);
lista.Add(2.427);
lista.Add(2.208);
lista.Add(2.394);
lista.Add(1.8);
lista.Add(2.379);
lista.Add(2.383);
lista.Add(2.950);
lista.Add(2.222);
lista.Add(3.152);

List<double> aux = new List<double>();

for (int i=0; i < lista.Count; i++) {

if(lista[i] > media && aux.Count <5){
aux.Add(lista[i]);
}
else{aux.Clear();}

foreach (double valor in aux) { Console.WriteLine(valor); }
Console.Read();

Recommended Answers

All 10 Replies

It clears because cooner or later your code goes to else block, and clears the list.

But I actually dont get it which value would you like to put into aux list from lista. Can you clarify it a bit better?

Do you mean that tin the for loop you check the current value, and if this and next 5 values are higher then the media value, you put all these 5 values into aux list?
And you continue loopint to the end of the items in lista?

If so, you can do it like:

List<double> aux = new List<double>();
int counter = 0;
for (int i = 0; i < lista.Count; i++) 
{
    if(counter == 5)
        break;
    if(lista[i] > media && counter <= 5)
    {
        aux.Add(list[i]);
        counter++;
    }   
    else
    {
        counter = 0;
        aux.Clear();
    }
}

//show values...
commented: 1 +1

Hi, thank for your reply.

Yes, i want to check all list and every time that a value and the 4 next values are higher than media value I copy that small list of 5 and copy it... and continue looping to the end of the list.

Thanks in advance.

Thanks a lot, its perfect! :D

Sorry Bonca, your code proposal doesnt continue looping the list to the end :( any ideas to go around this?

It stops when find 5 consecutive values higher than media.

You means to add all numbers (of 5 in a row) if higher then the media into new list?

yes, and then retrieve the a big list containing all small lists of 5 elements :(

In this case, we have to re-do your code completely. Usin some Linq and an additional method. I did think of something like this:

        private void YourMethod()
        {
            double media = 1.8815;
            List<double> lista = new List<double>();
            lista.Add(2.201);
            //and other values...

            List<double> aux = new List<double>();
            for (int i = 0; i < lista.Count; i++)
            {
               if(CheckNext5(media, lista.Skip(i).Take(5).ToList()))
               {
                   for (int j = 0; j < 5; j++)
                       aux.Add(lista[i + j]); 
                   i = i + 4;
               }
            }
        }

        private bool CheckNext5(double media, List<double> list)
        {
            bool bFlag = true;
            foreach (double item in list)
            {
                if (item < media) 
                {
                    bFlag = false;
                    break;
                }
            }
            return bFlag;
        }

There is a method, that checked next 5 values if they are higher then the media variable, is so, they are added to the new list, else, loops goes on to get next values.
ps: vote and add a comment :)

Hope you like it :)

Now its really perfect! Thats it :) Thank you very much for our time.

No problem, any time actually :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.