Skip to content

Variable declaration problem

shah

Asked Mar 22, 2007, 6:35 AM EDT5 answers
Share
LinkedInX
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,

5 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0

    I believe this is what you want to do...(sorting, however, will have to be your challenge <g>).
    Your for loops were overrunning the array.  You had <= arr.Length instead of < arr.Length.
     
    -Mike G

    C#
    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();
    }
    }
    0
  • 0
    Thanks a million. Thank You very much Mike. That has been sorted
    0
  • 0
    One more thing how to make clean code on this forum. I mean how to put in a right manner the code. What tags are used for that?
    0
  • 0
    Now i have another problem when i get an array using [code] getArray(int [] arr) for (i = 0; i < arr.Length; i++) { String val = Console.ReadLine(); arr[i] = Convert.ToInt32(val); } public static void quicks(int lo, int hi, int[] arr) { int i = lo; hi = arr.Length; int j = hi; int pivot; pivot = arr[(lo + hi) / 2]; if (lo >= hi) { return; } Console.WriteLine(pivot); do { while (arr[i] < pivot) i++; while (arr[j] > pivot) // when it comes here it says Array // out of bound problem j--; if (i < j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } while(i < j ); // end of do condition if (lo < j) quicks(lo, j,arr); if (i < hi) quicks(i, hi,arr); [/code] Please help me in correcting quicksort function
    0
  • Mike Gold
    0
    It should copy out of VS.NET 8.0 cleanly.  If not, I suggest using a front page editor or equivalent HTML editor which will make sure the paragraph and line break html tags are maintained
    0