-
December 23rd, 2013, 05:18 AM
#1
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
}
-
December 23rd, 2013, 01:10 PM
#2
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
}
-
December 23rd, 2013, 01:50 PM
#3
Re: Array index out of Range
Originally Posted by dglienna
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 02:16 PM.
-
December 23rd, 2013, 11:06 PM
#4
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
-
December 24th, 2013, 02:19 AM
#5
Re: Array index out of Range
How about a hidden listbox? Load the values, set .Sorted=TRUE and it's solved!
-
December 24th, 2013, 02:37 AM
#6
Re: Array index out of Range
How can I insert a list box in linux MonoDevelop? come out of MS windows. It is expensive!
-
December 24th, 2013, 02:51 AM
#7
Re: Array index out of Range
You want a TreeView with a ListStore model.
from a search link
-
December 25th, 2013, 11:56 AM
#8
Re: Array index out of Range
Originally Posted by soheil2
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|