Array index out of Range
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Array index out of Range

  1. #1
    Join Date
    Dec 2013
    Posts
    26

    Array index out of Range

    Hi.
    Why do I get this error?
    Here is my code:

    Code:
    static public void sort( int []a, int n)
    {
    	//sorts the input array a using the Insertion sort algorithm
    	int j;
    	int t;
    			
    	for( int i=1;i<n;++i )
    	{
    		j=i;
    		while( ( a[j]<a[j-1] ) && j>0 )
    		{
    			t = a[j];
    			a[j] = a[j-1];
    			a[j-1]  = t;
    			j--;					
    		}//end while
    	}//end for
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,945

    Re: Array index out of Range

    Step thru the code line by line, or print out the values

    Code:
    	for( int i=1;i<n;++i )
    	{
    		j=i;
    		while( ( a[j]<a[j-1] ) && j>0 )
    		{
    			t = a[j];
    			a[j] = a[j-1];
    // debug.print t , a[j]
    			a[j-1]  = t;
    			j--;					
    		}//end while
    	}//end for
    }
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Dec 2013
    Posts
    26

    Re: Array index out of Range

    Quote Originally Posted by dglienna View Post
    Step thru the code line by line, or print out the values

    Code:
    	for( int i=1;i<n;++i )
    	{
    		j=i;
    		while( ( a[j]<a[j-1] ) && j>0 )
    		{
    			t = a[j];
    			a[j] = a[j-1];
    // debug.print t , a[j]
    			a[j-1]  = t;
    			j--;					
    		}//end while
    	}//end for
    }
    The problem is I don't know how to do that in the MonoDevelop IDE.

    EDIT:I get Error only on un-commenting the line 'j--;'
    Last edited by soheil2; December 23rd, 2013 at 01:16 PM.

  4. #4
    Join Date
    Dec 2013
    Posts
    26

    Re: Array index out of Range

    I just included the While loop in a new for- loop that counts from i to 0, like this:
    for( int i=1;i<n;++i )
    {
    j=i;

    for(j=i;j>0;--j)
    while(a[j]<a[j-1])
    {
    t = a[j];
    a[j] = a[j-1];
    a[j-1] = t;
    }//end while
    }//end for
    and that worked.
    Although the performance sucks and I should be executed for doing that to a processor

  5. #5
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,945

    Re: Array index out of Range

    How about a hidden listbox? Load the values, set .Sorted=TRUE and it's solved!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Dec 2013
    Posts
    26

    Re: Array index out of Range

    How can I insert a list box in linux MonoDevelop? come out of MS windows. It is expensive!

  7. #7
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,945

    Re: Array index out of Range

    You want a TreeView with a ListStore model.
    from a search link
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Dec 2013
    Posts
    2

    Re: Array index out of Range

    Quote Originally Posted by soheil2 View Post
    Hi.
    Why do I get this error?
    Here is my code:

    Code:
    static public void sort( int []a, int n)
    {
    	//sorts the input array a using the Insertion sort algorithm
    	int j;
    	int t;
    			
    	for( int i=1;i<n;++i )
    	{
    		j=i;
    		while( ( a[j]<a[j-1] ) && j>0 )
    		{
    			t = a[j];
    			a[j] = a[j-1];
    			a[j-1]  = t;
    			j--;					
    		}//end while
    	}//end for
    }
    Your mistake is in while loop
    Use this code

    Code:
    static public void sort( int []a, int n)
    {
    	//sorts the input array a using the Insertion sort algorithm
    	int j;
    	int t;
    			
    	for( int i=1;i<n;++i )
    	{
    		j=i;
    		while( j>0 && ( a[j]<a[j-1] )  )
    		{
    			t = a[j];
    			a[j] = a[j-1];
    			a[j-1]  = t;
    			j--;					
    		}//end while
    	}//end for
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center