Home c# The index was out of bounds of the array!

The index was out of bounds of the array!




Why does it show 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 ("enter the number of values ​​in the array:");
  int n = int.Parse (Console.ReadLine ());
  double [] a = new double [n];
  Random rnd = new Random ();
  for (int i = 0; i & lt; a.Length; i ++)
    a [i] = rnd.Next (1, 100);
    Console.Write ("{0: 0}", a [i]);
  Console.WriteLine ();
  double max = a.Max ();
  Console.WriteLine ("Maximum: {0}", max);
  double min = a.Min ();
  Console.WriteLine ("Minimum: {0}", min);
  double sum = 0;
  for (int i = 0; i & lt; = n; i ++)
   sum + = a [i];
  Console.WriteLine ("Sum of m / y max and min: {0}", sum);
  Console.ReadLine ();

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

Answer 1, authority 100%

for (int i = 0; i & lt; = n; i ++)
  sum + = a [i];

replace with

for (int i = 0; i & lt; n; i ++)
  sum + = a [i];

Answer 2, authority 100%

The reason for the error is that you are trying to use indexes where they are not required. As a result, when you do it yourself, you may be inaccurate or misspelled.

Since you created an array containing n elements, the valid index range is [0, n-1]

for (int i = 0; i & lt; = n; i ++)
     sum + = a [i];

so it would be better to write

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

not referencing indexes. 🙂

Answer 3, authority 29%

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

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

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

Length can be used (for Count lists). Like this:

for (int index = 0; index & lt; arr.Length; index ++) {...} // in case with an array
for (int index = 0; index & lt; lst.Count; index ++) {...} // in the case of a list

Also very convenient is the foreach, in method to iterate over the collection, if you don’t need indices, then you don’t 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 unexpected side effects. If you need to add or remove items from the original collection, use the for loop.

foreach is a little simpler as it works for arrays or collections of objects that implement the IEnumerable or IEnumerable & lt; T & gt; interface from System.Collections.Generic .

But that’s not all that can make the programmer’s work easier, there is also a method Enumerable.Sum , which computes the sum of the sequence. And you don’t have to iterate over all the elements of the collection. For example:

double sum = a.Sum (); // sum of the elements of your array

You can also check the Enumerable.Aggregate method , which applies an aggregate function to the sequence. For example, receiving the amount will be like this:

double sum = a.Aggregate ((result, item) = & gt; result + item);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions