Murtaza Hasan

Murtaza Hasan

  • NA
  • 1
  • 22.1k

why is the error " use of unassigned local variable" ?

Feb 6 2014 10:19 AM
I m a beginner , and in this program i am trying a simple sort . I dont know why i cant print  'sw'  in the FOR Loop ? 

I want an output : 
92
95
97
98
99






Here is my code, please help : 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[] Arr = new int[5] { 99, 98, 92, 97, 95 };
//int[] Arr = { 90, 67, 87, 2, 5 };
int a, b,i;
int kw;
for (i = 0; i < 5; i++)
{
if (Arr[i] > Arr[i + 1])
{
int sw;
sw = Arr[i + 1];
Arr[i + 1] = Arr[i];
Arr[i] = sw;
Console.WriteLine(sw);
}
Console.WriteLine(sw);
// Console.WriteLine(" Sorted Array is : ", sw);
}
Console.ReadLine();
}
}
}

Answers (2)