-1

I have a list of elements like this x [28, 19, 24, 50, 29, 43] and would like to take two elements at a time to process it (first iteration would go through 28, 19, second through 24, 50 and so on).

everytime I go through two elements I need to sum them and divide them with 2 to get their average and then save that average into another list.

Ive tried something like:

List<double> avg = new List...;

for(int i = 1; i < x.Count(); i++)
        {
            if(i%2 == 0)
            {
                double D = x[i] + x[i - 1] / 2;
                avg.Add(D);
            }
        }

Help of any sorts would be appreciated. Thank you!!!

PilotSB
  • 25
  • 4
  • 1
    I don't completely get what's the question, but if you skip in pairs of 2, in the `for` line it shouldn't be `i++`, but `i+=2` instead. – Alejandro Mar 04 '21 at 15:16
  • works for me (except that you forgot to set parentheses for your average-calculation) - what exactly is your problem? what's the question you want to ask? – Franz Gleichmann Mar 04 '21 at 15:16
  • @Alejandro But how do I take and sum the two elements that get iterated. Sorry English is not my first language. – PilotSB Mar 04 '21 at 15:18
  • "But how do I take and sum the two elements that get iterated" - you're already doing that in your code. please try to describe your actual problem more precisely (and in your question, not in comments) – Franz Gleichmann Mar 04 '21 at 15:18
  • @Franz Gleichmann Oh so the issue isn't in this part of the code. My method that this code is part of doesn't return the correct value. Ima look into other parts of my code. I was sure that this was the problem but I guess not. Thank you! – PilotSB Mar 04 '21 at 15:20
  • (also: don't forget that indizes start at _0_, not at 1.) – Franz Gleichmann Mar 04 '21 at 15:21
  • @ThomasWeller Yes, that's what I got from the question description, since in the loop it reads `i` and `i-1`, thus getting the skiped again. – Alejandro Mar 04 '21 at 15:27
  • I think this is answered pretty well but just wanted to add that I hope you're not taking this list of averages and then averaging that - an average of averages has a lot of potential to be wrong https://math.stackexchange.com/questions/95909 – McAden Mar 04 '21 at 15:43

1 Answers1

0

You have 3 issues in your code:

  1. You start the loop at index 1, but index 1 will be skipped due to the if (i%2 == 0), so essentially the loop starts at 2. So, change the condition to if (i%2 == 1).
  2. To get an average, you need to sum up two numbers, then divide it by 2. You use one number and add the half of the second one. Use braces to fix that: (x[i] + x[i - 1]) / 2.
  3. You use an integer division but expect a double result. Fix that by dividing through 2.0 instead of 2: (x[i] + x[i - 1]) / 2.0

Full code:

var x = new List<int>{28, 19, 24, 50, 29, 43};
List<double> avg = new List<double>();

for(int i = 1; i < x.Count(); i++)
{
    if(i%2 == 1)
    {
        double D = (x[i] + x[i - 1]) / 2.0;
        avg.Add(D);
    }
}

However, the code seems too complex. If you read that again in a few months you'll probably not understand it any more.

IMHO

  • it would be better to start at index 0, because that's the usual way to process all numbers of an array.
  • instead of weird modulo operation, just walk in steps of 2.
  • Change the name of the variables.
  • Add some precondition check if averaging is possible
var pairs = new List<int>{28, 19, 24, 50, 29, 43};

List<double> averages = new List<double>();
if (pairs.Count() %2 != 0) throw new ArgumentException("Can't build averages on that list. The list needs to have an even number of elements.");
for(int i = 0; i < pairs.Count() - 1; i+=2)
{
    double average = (pairs[i] + pairs[i + 1]) / 2.0;
    averages.Add(average);
}
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222