Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7055803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:47:32+00:00 2026-05-28T03:47:32+00:00

I have a ten element array of integers. I want to sum the elements

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:47:33+00:00Added an answer on May 28, 2026 at 3:47 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the flowing code $LastModified = filemtime($somefile) ; i want to add ten
I have ten arrays in my application. I want to write those array values
I have a method that's about ten lines of code. I want to create
Ok, so suppose. I have ten images in documents.images array. When I click on
Say I have ten lines and I want to prepend text to some word
Let's say I have a generator that I want to pull the 10th element
If I have ten items in a list, how can I dynamically add a
I have ten or more(i don't know) tables that have a column named foo
I have ten buttons that each correspond to a different number. I'm looking to
I have ten values in the dataset, numbers 1 - 10 and corresponding values.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.