I was following a tutorial showing how to create a Binary search algorithm from scratch. However I recieve the error "Use of unassigned local variable 'Pivot'". I'm new to the language and have only tried much simpler languages previously.
I apologise for the lack of internal documentation and appauling use of white space.
The error is near the bottom of the code labled using "//"
Here is the program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Binary_Search_2
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[10];
Random rnd = new Random();
for (int i = 0; i < arr.Length; i++)
{
arr[i] = rnd.Next(1, 10);
}
Array.Sort(arr);
for (int i = 0; i < arr.Length; i++)
{
Console.Write("{0},", arr[i]);
}
int Start = 0;
int End = arr.Length;
int Center = Start + End / 2;
int Pivot;
while (arr[6] > 0)
{
while (arr[6] < arr[Center])
{
End = Center;
Center = (End + Start) / 2;
if (Pivot == arr[Center])
{
Console.WriteLine("The Index is {0}", arr[Center]);
}
break;
}
while (arr[6] > arr[Center])
{
Start = Center;
Center = (End + Start) / 2;
if (Pivot == arr[Center]) //**This is where the error occurs.**
{
Console.WriteLine("The index is {0}", arr[Center]);
}
}
}
}
}
}
I'm sorry if this is actually something really simple but I don't have anyone to teach me directly and I'm out of ideas.