quimak
quimak

Reputation: 125

Forcing one more iteration of loop?

int iterationMax = 1;
double actualMax = 0;
int A = 3;

List<double> tmp = new List<double> { 400, 0, -300, 400, 600 ,300, 400, 
                                      1200, 900, 400, 1200, 1500};
List<double> listMax = new List<double>();
for (int i = 0; i < tmp.Count; i++)
{
    if (iterationMax < A) // A == 3
    {
        if (tmp[i] > actualMax)
        {
            actualMax = tmp[i];
        }
        iterationMax++;
    }
    else
    {
        listMax.Add(actualMax);
        actualMax = 0;
        iterationMax = 1;
    }
}

Console.WriteLine("\nMaxs: ");
foreach (double max in listMax)
{
    Console.Write(max + ", ");
}

List tmp holds = { 400,0,-300|400,600,300|400,1200,900|400,1200,1500}, Produce 400, 600, 1200, 1200, Should be 400, 600, 1200, 1500. I don't know how to enter else statement to add 1500 to list.

I just want to get max from every 3 elements.

Upvotes: 0

Views: 112

Answers (4)

Gilad Green
Gilad Green

Reputation: 37299

When one needs to perform manipulations on collection it is many times nicer to use Linq.

Use GroupBy in the index/3 and as it is an int each 3 following items will have a different key. Then for each group select the maximum value:

var items = new int[] { 400, 0, -300, 400, 600, 300, 400, 1200, 900 };

var results = items.Select((item, index) => new { item, index })
                   .GroupBy(item => item.index / 3)
                   .Select(group => group.Max(item => item.item));
//400, 600, 1200

Upvotes: 4

Alexandru Pupsa
Alexandru Pupsa

Reputation: 1878

The problem is, when iterationMax reaches 3, you don't do anything with the value in temp, that loop is lost.

for (int i = 0; i < tmp.Count; i++)
{
    if (tmp[i] > actualMax)
    {
        actualMax = tmp[i];
    }
    iterationMax++;

    if (iterationMax > A)
    {
        listMax.Add(actualMax);
        actualMax = 0;
        iterationMax = 1;
    }
}

Upvotes: 0

Dan Dumitru
Dan Dumitru

Reputation: 5423

A quick fix for your code would be:

var A = 3;

int iterationMax = 0;
double actualMax = 0;

List<double> tmp = new List<double> {400,0,-300,400,600,300,400,1200,900,400,1200,1500};
List<double> listMax = new List<double>();
for (int i = 0; i < tmp.Count; i++)
{
    if (iterationMax < A) // A == 3
    {
        if (tmp[i] > actualMax)
        {
            actualMax = tmp[i];
        }
        iterationMax++;
    }

    if (iterationMax == A)
    {
        listMax.Add(actualMax);
        actualMax = 0;
        iterationMax = 0;
    }
}

Console.WriteLine("\nMaxs: ");
foreach (double max in listMax)
{
    Console.Write(max + ", ");
}

As others have said, start iterationMax from 0 and turn that else into an if (iterationMax == A).

Upvotes: 1

Excavator
Excavator

Reputation: 48

Setting iterationMax to 0 in initialization and under else should solve your issue.

Currently your if structure will only check the first two out of three elements. Since 1500 is element #3, it will not be checked.

Upvotes: 0

Related Questions