-1

I tried it myself, but I can't finish it. I can't figure out how to find the sum of elements on a segment. And is there any method for this?

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("the length of array");
            int n = Convert.ToInt32(Console.ReadLine());
            int[] array = new int[n];
            for (var i = 0; i < array.Length; i++)
            {
                array[i] = Convert.ToInt32(Console.ReadLine());
            }

            int a = Convert.ToInt32(Console.ReadLine());
            int b = Convert.ToInt32(Console.ReadLine());

            for (int i = a; i <= b; i++)
            {

            }
        }
    }
}
GSerg
  • 76,472
  • 17
  • 159
  • 346
  • 1
    `sum += array[i]`? – GSerg Oct 07 '19 at 15:55
  • See also [Get sum of elements of an Array in C# using generics](https://stackoverflow.com/q/1146208/215552) for a generic function. – Heretic Monkey Oct 07 '19 at 16:02
  • There are lots of ways you could do this. But before asking the question, you should try _something_. Then you can explain what it is you tried, why that didn't work, and what _specifically_ you need help with. That said, the basic question has been asked many times in one form or another. The `List` class works (mostly) just like an array, and so the marked duplicates provide information on how to accomplish the goal you stated. If you need different help, you need to be a lot more specific. Please read [ask] for more information on presenting your question in a clear, answerable way. – Peter Duniho Oct 07 '19 at 16:03
  • See also https://stackoverflow.com/questions/16307829/sum-a-range-of-ints-in-a-list for more inspiration. – Peter Duniho Oct 07 '19 at 16:04

1 Answers1

-3

if you want it short:

array.Skip(a).Take(b-a+1).Sum()
Holger
  • 2,446
  • 1
  • 14
  • 13
  • 5
    might want to explain your answer because if op can't properly ask for what she wants she will never understand this – incapaz Oct 07 '19 at 15:57