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

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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

Posts: 1Member
in C#
Hello everybody,

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

This is my code

[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);

}
[/CODE]

I did it follow this code that make from C:
void ShellSort(int a[], int N, int h[], int k)
[code]
{
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;
}
}
}
[/code]

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 ?