Why prints that the index is out of bounds.
It is necessary to display the sum of all elements of the array.

static void Main(string[] args) { Console.WriteLine("Π²Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΊΠΎΠ»-Π²ΠΎ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ Π² массивС:"); int n = int.Parse(Console.ReadLine()); double[] a = new double[n]; Random rnd = new Random(); for (int i = 0; i < a.Length; i++) { a[i] = rnd.Next(1, 100); Console.Write("{0: 0}", a[i]); } Console.WriteLine(); double max = a.Max(); Console.WriteLine("МаксимальноС:{0}", max); double min = a.Min(); Console.WriteLine("МинимальноС:{0}", min); double sum = 0; for (int i = 0; i <= n; i++) { sum += a[i]; } Console.WriteLine("Π‘ΡƒΠΌΠΌΠ° ΠΌ/Ρƒ ΠΌΠ°Ρ… ΠΈ ΠΌin:{0} ", sum); Console.ReadLine(); } 

As a result of the code execution I get an error on the line: sum + = a [i];

Closed due to the fact that off-topic participants Denis , aleksandr barakin , rdorn , Alex , Alexander Petrov 23 Nov '16 at 17:27 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Denis, aleksandr barakin, rdorn, Alex, Alexander Petrov
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 3
    Must be i <n; - koks_rs
  • 2
    If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦
  • Trace for n = 1 or 2 - and everything will be clear :) - nick_n_a

3 answers 3

 for (int i = 0; i <= n; i++) { sum += a[i]; } 

change to

 for (int i = 0; i < n; i++) { sum += a[i]; } 
  • Thank you very much! I corrected everything else .. - roy34
  • @ roy34 thank the person not only in words, but in deed - check the box next to this answer and close the question, and add points to the person. - Bulson

The reason for the error is that you are trying to use indexes where it is not required. As a result, when you do it yourself, you can make an inaccuracy or typo.

Since you created an array containing n elements, the allowable range of indices [0, n-1]

  for (int i = 0; i <= n; i++) ^^^^^^^ { sum += a[i]; } 

therefore it would be better to write

 foreach ( var x in a ) sum += x; 

without referring to indices. :)

    As said earlier, the problem is in the line: for (int i = 0; i <= n; i++) .

    To solve this problem, I would have approached a bit from the other side. When iterating through a collection, you can use not third-party variables that may be erroneous, but the length of the array. You used it in the first for .

    For example, if n = 10 , and in the array you have nine elements, then you will again get an exception. Suddenly the variable n has changed somewhere?

    You can use Length (for lists Count ). Like this:

     for (int index = 0; index < arr.Length; index++) {...} // Π² случаС с массивом for (int index = 0; index < lst.Count; index++) {...} // Π² случаС со списком 

    The foreach, in method for iterating a colection is also very convenient if you do not need indexes, in which case you do not need to worry about knowing the number of array elements:

     foreach (var item in array) {...} 

    However, it should not be used to add or remove items from the original collection to avoid unforeseen side effects. If you need to add or remove items from the original collection, you should use a for loop.

    foreach bit simpler, since it works for arrays or collections of objects that implement the IEnumerable or IEnumerable<T> interface from System.Collections.Generic .

    But this is not all that can facilitate the work of the programmer, there is also an Enumerable.Sum method that calculates the sum of a sequence. And you do not have to go through all the elements of the collection. For example:

     double sum = a.Sum(); // сумма элСмСнтов Π’Π°ΡˆΠ΅Π³ΠΎ массива 

    You can also note the Enumerable.Aggregate method, which applies an aggregate function to a sequence. For example, the receipt of the amount will be as follows:

     double sum = a.Aggregate((result, item) => result + item); 
    • foreach is inconvenient if indexes are needed - Grundy
    • @Grundy, no doubt. But this is an alternative when they are not needed and you need to sort out the collection. So it is very convenient. - Denis Bubnov
    • up to a heap, you can say a few words about Aggregate - Grundy