Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding elements of an array

Tags:

arrays

c#

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.

like image 526
user995689 Avatar asked Jan 12 '12 12:01

user995689


People also ask

How do you add an element to an array in array?

First get the element to be inserted, say x. Then get the position at which this element is to be inserted, say pos. Then shift the array elements from this position to one position forward(towards right), and do this for all the other elements next to pos.

How do you add two elements to an array?

The idea is to start traversing both the array simultaneously from the end until we reach the 0th index of either of the array. While traversing each elements of array, add element of both the array and carry from the previous sum. Now store the unit digit of the sum and forward carry for the next index sum.

How do you add up the sum of an array?

To get the sum of an array of numbers:Use the Array. reduce() method to iterate over the array. Set the initial value in the reduce method to 0 . On each iteration, return the sum of the accumulated value and the current number.

Can you add elements to an array Java?

In Java, Arrays are mutable data types, i.e., the size of the array is fixed, and we cannot directly add a new element in Array.


2 Answers

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

like image 107
George Duckett Avatar answered Sep 23 '22 01:09

George Duckett


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.

like image 33
Martin Liversage Avatar answered Sep 27 '22 01:09

Martin Liversage