Помогите исправить ошибку "Index was outside the bounds of the array" - C#
Формулировка задачи:
int[] a = { 27, 12, 4, 2, 9, 10, 0 };
foreach (int v in a)
{
Console.Write("| {0} |", v);
}
for (int i = 0; i < a.Length - 1; i--)
{
for (int z = i + 1; z < a.Length; z++)
{
if (a[i] > a[i + 1])
{
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
Console.WriteLine();
foreach (int v in a)
{
Console.Write("| {0} |", v);
}
Console.ReadLine();Решение задачи: «Помогите исправить ошибку "Index was outside the bounds of the array"»
textual
Листинг программы
private static void Main(string[] args)
{
int[] a = { 27, 12, 4, 2, 9, 10, 0 };
foreach (int v in a)
{
System.Console.Write("| {0:00} ", v);
}
System.Console.Write("|");
for (int i = 0; i < a.Length; i++)
{
for (int z = i + 1; z < a.Length; z++)
{
if (a[i] > a[z])
{
int temp = a[i];
a[i] = a[z];
a[z] = temp;
}
}
}
System.Console.WriteLine();
foreach (int v in a)
{
System.Console.Write("| {0:00} ", v);
}
System.Console.Write("|");
System.Console.ReadLine();