Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Populating one array with elements of another array. Exceeding the length of the second array exception

$
0
0

I am trying to take the elements of one array "arr", subtract them one after the other and then populate a second array "newArr" with these values.  For example I have my first array  { 300, 100, -10 } the second array should be {200, 90}

Here is what I got, but it throws an exception that  "Index was outside the bounds of the array."

using System; class Program { static void Main() { int value = 0; int[] arr = new int[] { 300, 100, -10 }; int[] newArr = new int[arr.Length]; for (int i = 0; i < arr.Length; i++) { if (i + 1 < arr.Length) { newArr[value] = arr[i] - arr[i + 1]; } value++; } Console.WriteLine(value); } }

What is wrong with it?





Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>