# How do I set a negative number to index of array in C# ?

### Question

• Hello everybody,

I'm newbie in C#, and I've been doing algorithm about Shell Sort in C#.

This is my code

```static void shellSort(int[] group, int[] h)

{

int step, i, j, x, len, n, k;

n = group.Length;

k = h.Length;

for (step = 0; step < k; step++)

{

len = h[step];

for (i = len; i < n; i++)

{

x = group[i];

j = i - len;

while ((x < group[j]) && (j >= 0)) // <--- Error here "Make sure the index is not a ngative number"

{

group[j + len] = group[j];

j = j - len;

}

group[j + len] = x;

}

}

}

static void Main(string[] args)

{

int[] group, h;

group = new int[8]{6, 7, 15, 8, 18, 25, 4, 0}

h = new int[3] { 5, 3, 1};

shellSort(group,h);

}

```

I did it follow this code that make from C:

```void ShellSort(int a[], int N, int h[], int k)
{
int step,i,j,x,len;
for (step = 0 ; step <k; step ++)
{
len = h[step];
for (i = len; i <N; i++)
{
x = a[i];
j = i-len;
while ((x<a[j])&&(j>=0))
{
a[j+len] = a[j];
j = j - len;
}
a[j+len] = x;
}
}
}```

In C, this code have processed successfull. But in C#, i've got a error: "Make sure the index is not a ngative number" at while ((x < group[j]) && (j >= 0)) . But, I didn't repair it.
How could i repair it ? :(

Sunday, April 03, 2011 2:23 PM

• In response to your post title question, you don't. Negative values are not permitted as index values by the C# compiler, which is more strict than C/C++ compilers which leave it to the programmer to not supply negative index values.

You are attempting to use j as an index before you test for >= 0

((x < group[j]) && (j >= 0))

Have you tried ((j >= 0) && (x < group[j]))?

That way, if j < 0, the second expression is not tested because False && any other result is False.

If that doesn't work, test j for < 0 before starting the while loop.

• Edited by Sunday, April 03, 2011 4:09 PM add info
• Proposed as answer by Monday, April 04, 2011 10:25 AM
• Marked as answer by Thursday, April 07, 2011 12:34 AM
• Unmarked as answer by Thursday, April 07, 2011 12:36 AM
• Marked as answer by Thursday, April 07, 2011 12:36 AM
Sunday, April 03, 2011 4:05 PM

### All replies

• In response to your post title question, you don't. Negative values are not permitted as index values by the C# compiler, which is more strict than C/C++ compilers which leave it to the programmer to not supply negative index values.

You are attempting to use j as an index before you test for >= 0

((x < group[j]) && (j >= 0))

Have you tried ((j >= 0) && (x < group[j]))?

That way, if j < 0, the second expression is not tested because False && any other result is False.

If that doesn't work, test j for < 0 before starting the while loop.

• Edited by Sunday, April 03, 2011 4:09 PM add info
• Proposed as answer by Monday, April 04, 2011 10:25 AM
• Marked as answer by Thursday, April 07, 2011 12:34 AM
• Unmarked as answer by Thursday, April 07, 2011 12:36 AM
• Marked as answer by Thursday, April 07, 2011 12:36 AM
Sunday, April 03, 2011 4:05 PM
• As pvdg42, you cannot set a negative number to the index of an array. The problem in your code is, becuase "j" is negative.

This formula:

``` j = i - len
```

create an j (index of an array= negative. Check why.

Mitja
Sunday, April 03, 2011 4:42 PM
• Hi,

as pvdg42 said  j is negative in your while loop, thus there are two solutions :

1. use lazy bool evaluating as pvdg42 said :

```while (j >= 0 && x < group[j])
{
group[j + len] = group[j];
j = j - len;
}
```

2. Use if statement before the while loop :

``` if (j >= 0)
{
while (x < group[j])
{
group[j + len] = group[j];
j = j - len;
}
}
```
Kind regards,

• Proposed as answer by Monday, April 04, 2011 10:25 AM
Sunday, April 03, 2011 6:53 PM
• Hi,

Thanks for your helps. I have repaired it successful (follow pvdg42 way).

Although I completely learnt it, but I am never attention to the prior level of && operator, it is unexpected.

This point is really a valuable experience for me.

Thank you again

Tuan regards,

Monday, April 04, 2011 1:25 AM
• Hi Seaboy,

Try to avoid this kind of code which is from the time of Algol.

Build in are now all kind of methods to sort any collection which are much faster.

(Or is should be a school assignment how the first sorts were in history done).

Success
Cor
Monday, April 04, 2011 5:29 AM
• Hi Cor Ligthert,

Thanks for your advice. It's just a school assignment of mine in algorithm course.

It's very basic, but it's a base to help me expand my thinking when i programme.

I'm glad to listen you :).

Tuan regards,

Monday, April 04, 2011 10:43 AM
• Hi seaboyvt,

I am glad that it works for you now. Please mark it as answer. Thanks for supporting.

Vin Jin [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Tuesday, April 05, 2011 8:51 AM