Variable declaration problem
I am working on QuickSort Console Application as i made quicksort function
public static void Main(string[] args)
{
****************************************
int[] arr;
int hi, lo = 0;
getArray(arr);
Sort(lo, hi,arr);
//The problem lies here it is giving error on variable "hi" as how to determine its the last number in array
***********************************
}
public static void getArray(int [] arr)
{
try
{
int i;
for (i = 0; i <= arr.Length; i++)
{
Console.WriteLine("Enter the numbers to sort:" +arr[i]);
}
}
catch(Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
displayArr(arr);
}
public static void displayArr(int [] arr)
{
for (int i = 0; i <= arr.Length; i++)
{
Console.Write("Sorted:" +arr[i]);
}
}
// Please help me in this.
Thanks,
Mike GoldPosted Mar 27, 2007, 4:46 PM
shahPosted Mar 27, 2007, 10:35 AM
shahPosted Mar 24, 2007, 8:05 AM
shahPosted Mar 24, 2007, 8:03 AM
Mike GoldPosted Mar 23, 2007, 12:00 PM
I believe this is what you want to do...(sorting, however, will have to be your challenge).
Your for loops were overrunning the array. You had <= arr.Length instead of < arr.Length.
-Mike G
using System;
class
testMe{
public testMe(){
}
public void TestIt(string[] args){
int[] arr = {0, 1, 2, 3};
int hi, lo = 0;
getArray(arr);
Console.WriteLine("Before Sorting..."); // Sort(lo, hi, arr); Console.WriteLine("After Sorting...");
displayArr(arr); Console.ReadLine(); //The problem lies here it is giving error on variable "hi" as how to determine its the last number in array
}
public void getArray(int[] arr){
try
{
int i;
for (i = 0; i < arr.Length; i++)
{
Console.WriteLine("Enter the numbers to sort:");
string val = Console.ReadLine();
arr[i] = Convert.ToInt32(val);
}
displayArr(arr);}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
} public void displayArr(int[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
Console.Write("{0} ", arr[i]);
} Console.WriteLine();
}
}