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?