Ok, my problem is a rather weird one. My teacher asked us to write a piece of code that will count how many even numbers are in a given array. my code is as follows:
static void Main(string[] args)
{
int[] array = { 1, 2, 3, 4, 5, 6 };
Console.WriteLine(NumOfEvens(array));
}
static int NumOfEvens(int[] array)
{
return NumOfEvens(array, 0);
}
static int NumOfEvens(int[] array, int index)
{
if (index == array.Length - 1)
{
if (array[index] % 2 == 0)
return 1;
else
return 0;
}
if (array[index] % 2 == 0)
return 1 + NumOfEvens(array, index++);
else
return NumOfEvens(array, index++);
}
However, when I ran this, it would output a Stack Overflow. Debugging came to show that the function simply didn't increment my index variable. replacing the increment with a "+1" seemed to fix the issue, but I'm quite curious to learn what might be the cause of this issue.