5

I have a ten element array of integers. I want to sum the elements by group, so for instance I want to add the value at element 0 with the value at element 1, then with the value at element 2, then 3, and so on through to element 9, then add the value at element 1 with the value at 2,3, through to 9 until every group of 2 values has been added together and stored in a variable. I then want to repeat the process with groups of 3, groups of 4, of 5, all the way through to group of 10. Each resultant total being stored in a separate variable. Thus far the only way I can figure out how to do it is thus :-

int i = 0;
int p = 1;
int q = 2;
int r = 3;

while (i < NumArray.Length - 3)
{
    while (p < NumArray.Length - 2)
    {
        while (q < NumArray.Length-1)
        {
            while (r < NumArray.Length)
            {
                foursRet += NumArray[i] + NumArray[p] + NumArray[q]+ NumArray[r];
                r++; 
            }
            q++;
            r = q + 1;
        }
        p++;
        q = p + 1;
        r = q + 1;
    }
    i++;
    p = i + 1;
    q = i + 2;
    r = i + 3;
}

The above is an example of summing groups of 4. I was wondering if anyone could be kind enough to show me a less verbose and more elegant way of achieving what I want. Many thanks.

7
  • 1
    Smells like homework? If so, please mark as such. If not, would you care to explain? Commented Jan 12, 2012 at 12:37
  • Im a bit confused as to the requirement. Can you update the question with an example input and the required output. Commented Jan 12, 2012 at 12:37
  • Can you explain the purpose of that calculations, because there is maybe a better aproach to the solution... Commented Jan 12, 2012 at 12:40
  • If you could provide exemple of content of arrays before and after the function would help. If you want to do something that runs until you have no more array, you might want to take a look at recursion (here is a link for the basics). Commented Jan 12, 2012 at 12:43
  • Happy to explain, the values in the array are decimal odds. Im working on a betting calculator. So, for instance a bet called a yankee consists off 6 doubles, 4 trebles and an accumulator. In such a case the numarray would hold 4 integer values wich for the double bets, Bet AB AC AD BC BD CD the trebles would be ABC ABC ACD BCD. In the above example NumArray would hold the odds for each selection and thus need to be processed as per my question. Commented Jan 12, 2012 at 12:45

2 Answers 2

4

Because everything is better with LINQ*:

using System; // Output is below
using System.Linq;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var inputArray = Enumerable.Range(0, 10).ToArray();

            var grouped =
                from Buckets in Enumerable.Range(1, inputArray.Length)
                from IndexInBucket in Enumerable.Range(0, inputArray.Length / Buckets)
                let StartPosInOriginalArray = IndexInBucket * Buckets
                select new
                {
                    BucketSize = Buckets,
                    BucketIndex = StartPosInOriginalArray,
                    Sum = inputArray.Skip(StartPosInOriginalArray).Take(Buckets).Sum()
                };

            foreach (var group in grouped)
            {
                Debug.Print(group.ToString());
            }

            Console.ReadKey();
        }
    }
} // SCROLL FOR OUTPUT

{ BucketSize = 1, BucketIndex = 0, Sum = 1 }
{ BucketSize = 1, BucketIndex = 1, Sum = 2 }
{ BucketSize = 1, BucketIndex = 2, Sum = 3 }
{ BucketSize = 1, BucketIndex = 3, Sum = 4 }
{ BucketSize = 1, BucketIndex = 4, Sum = 5 }
{ BucketSize = 1, BucketIndex = 5, Sum = 6 }
{ BucketSize = 1, BucketIndex = 6, Sum = 7 }
{ BucketSize = 1, BucketIndex = 7, Sum = 8 }
{ BucketSize = 1, BucketIndex = 8, Sum = 9 }
{ BucketSize = 1, BucketIndex = 9, Sum = 10 }
{ BucketSize = 2, BucketIndex = 0, Sum = 3 }
{ BucketSize = 2, BucketIndex = 2, Sum = 7 }
{ BucketSize = 2, BucketIndex = 4, Sum = 11 }
{ BucketSize = 2, BucketIndex = 6, Sum = 15 }
{ BucketSize = 2, BucketIndex = 8, Sum = 19 }
{ BucketSize = 3, BucketIndex = 0, Sum = 6 }
{ BucketSize = 3, BucketIndex = 3, Sum = 15 }
{ BucketSize = 3, BucketIndex = 6, Sum = 24 }
{ BucketSize = 4, BucketIndex = 0, Sum = 10 }
{ BucketSize = 4, BucketIndex = 4, Sum = 26 }
{ BucketSize = 5, BucketIndex = 0, Sum = 15 }
{ BucketSize = 5, BucketIndex = 5, Sum = 40 }
{ BucketSize = 6, BucketIndex = 0, Sum = 21 }
{ BucketSize = 7, BucketIndex = 0, Sum = 28 }
{ BucketSize = 8, BucketIndex = 0, Sum = 36 }
{ BucketSize = 9, BucketIndex = 0, Sum = 45 }
{ BucketSize = 10, BucketIndex = 0, Sum = 55 }

*Not everything is better with LINQ

Sign up to request clarification or add additional context in comments.

2 Comments

you could even do better: Enumerable.Range(0, 10).ToArray() :)
Good catch, can't believe I didn't do that!
1

If I understand you correctly you have an array of numbers with length n. You want to pick all combinations of m numbers from this. You then want to sum all these combinations and finally compute the sum of these sums.

For instance given n = 6 numbers you can pick m = 4 elements in 15 different ways (the numbers are indices in the array of numbers):

0 1 2 3
0 1 2 4
0 1 3 4
0 2 3 4
1 2 3 4
0 1 2 5
0 1 3 5
0 2 3 5
1 2 3 5
0 1 4 5
0 2 4 5
1 2 4 5
0 3 4 5
1 3 4 5
2 3 4 5

If n < 32 (no more than 31 numbers in your array) you can efficiently generate the indices using 32 bit arithmetic. The following function is based on Gosper's hack:

IEnumerable<UInt32> GetIndexBits(Int32 m, Int32 n) {
  unchecked {
    var i = (UInt32) (1 << m) - 1;
    var max = (UInt32) (1 << n);;
    while (i < max) {
      yield return i;
      var u = (UInt32) (i & -i);
      var v = u + i;
      i = v + (((v ^ i)/u) >> 2);
    }
  }
}

With m = 4 and n = 6 this function will generate these numbers (displayed in binary form):

001111
010111
011011
011101
011110
100111
101011
101101
101110
110011
110101
110110
111001
111010
111100

You can then create the sum using LINQ:

var m = 4;
var numbers = new[] { 1, 2, 3, 4, 5, 6 };
var sum = GetIndexBits(4, numbers.Length)
  .Select(
    bits => Enumerable
      .Range(0, numbers.Length)
      .Where(i => ((1 << i) & bits) != 0)
  )
  .Select(indices => indices.Sum(i => numbers[i]))
  .Sum();

With the provided input the sum will be 210 which is the same result as foursRet in the question when NumArray contains the numbers 1 to 6.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.