Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an sorted array having values like below: I need to calculate total as below:

Scenario 1 - Array values 12,15,17

12+15 = 27 
27+17 = 44 
44+27 = 71
Total = 71

Scenario 2 Array values 12,15,17,19

12+15 = 27
27+17 = 44
44+19 = 63
27+44+63 = 134

Total = 134

Scenario 3 Array values 12,15,17,19,23

12+15 = 27
27+17 = 44
44+19 = 63
63+23 = 86
27+44+63+86 = 220

Total = 220

Scenario 4 till N Array values 12,15,17,19,23.....N

I have to bring the above logic to C# code

I have written as below :

  int[] myNumbers = new int[] { 100,250,1000};

            Array.Sort(myNumbers);
            int sum = 0;
            int temp = 0;

            foreach (int y in myNumbers)
            {
                sum = sum + y;              
            }

            for(int i=0;i<myNumbers.Length-1;i++)
            {
               temp = temp + myNumbers[i];      
            }

           sum = sum + temp;

           Console.Write(sum);  

The above code works fine for array values 100,250,1000

But it fails for any other array values

Need help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
388 views
Welcome To Ask or Share your Answers For Others

1 Answer

Option 1

So if you want to get exact results as in your examples, you can use this method. It will return you an array of partial sums, that you can later sum up to get the result:

private static long[] CumulativeSums(long[] values)
{
    if (values == null || values.Length <= 1) return new long[0];

    var results = new long[values.Length];
    results[0] = values[0] + values[1];

    for (var i = 1; i < values.Length - 1; i++)
    {
        results[i] = results[i - 1] + values[i + 1];
    }

    return results;
}

And the use it as this:

var numbers = new long[] { 12, 15, 17, 19 };
var sumOfCumulativeSums = CumulativeSums(numbers).Sum();

And sumOfCumulativeSums will be 134.

Option 2

But the actual correct representation of cumulative sum is: a, a+b, a+b+c, .... So if you want the correct representation of method that returns you proper cumulative sums, you can use this method instead:

public static long[] CumulativeSums(long[] values)
{
    if (values == null || values.Length == 0) return new long[0];

    var results = new long[values.Length];
    results[0] = values[0];

    for (var i = 1; i < values.Length; i++)
    {
        results[i] = results[i - 1] + values[i];
    }

    return results;
}

Edit

Hope this helps you to solve your problem in either of ways, and if you have any questions or edits about the code, please ask.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...