Почему код иногда выдаёт переполнение массива? - C#
Формулировка задачи:
Товарищи, почему выдаёт иногда выдаёт переполнение массива?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication24
{
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int[] otr = new int[20];
int[] nol = new int[20];
int[] chet = new int[20];
int[] nchet = new int[20];
int[,] q = new int[4, 4];
int i = 0;
int o = 0;
int n = 0;
for (int w = 0; w < 4; w++)
{
for (int j = 0; j < 4; j++)
{
int p = rnd.Next(-20, 20);
q[w, j] = p;
if (q[w, j] < 0)//отрицательные
{
otr[o] = q[w, j];
i++;
Console.WriteLine("Отрицательные {0}", q[w, j]);
}
if (q[w, j] == 0)//нули
{
nol[n] = q[w, j];
i++;
Console.WriteLine("Нули {0}", q[w, j]);
}
if (q[w, j] % 2 == 0) //чётные
{
chet[i] = q[w, j];
i++;
Console.WriteLine("Чётные {0}", q[w, j]);
}
else //нечётные
{
nchet[i] = q[w, j];
i++;
Console.WriteLine("Нечётные {0}", q[w, j]);
}
}
}
}
}
}Решение задачи: «Почему код иногда выдаёт переполнение массива?»
textual
Листинг программы
otr[o] = q[w, j]; i++;