2nd try Sorting Algorithm.

A

AMP

Hello,
I posted this morning and recieved no responces., so I decided to
simplify my question.
How do I stop an exception from being thrown after the first run
through of the while clause:

private void InsertionSort_Function(uint[] IntCard,int length)
{
for (int j = 1; j <= (length-1); j++)
{
uint CurrentCard=IntCard[j];
int i =j-1;

{
while ((IntCard > CurrentCard) && (i > 1))
{
IntCard[i + 1] = IntCard;
i = i - 1;

}
}
IntCard[i+1]=CurrentCard;
}
}
}

Thanks
Mike
 
R

rossum

Hello,
I posted this morning and recieved no responces., so I decided to
simplify my question.
How do I stop an exception from being thrown after the first run
through of the while clause:

private void InsertionSort_Function(uint[] IntCard,int length)
{
for (int j = 1; j <= (length-1); j++)
{
uint CurrentCard=IntCard[j];
int i =j-1;

{
while ((IntCard > CurrentCard) && (i > 1))
{
IntCard[i + 1] = IntCard;
i = i - 1;

}
}
IntCard[i+1]=CurrentCard;
}
}
}

Thanks
Mike

I would have a look at the order in which you have written your while
statement: while ((IntCard > CurrentCard) && (i > 1))

This will be evaluated left to right, so the first thing it tries is
to access IntCard. If i is an invalid value, this will throw an
exception. By reordering the statement as: while ((i > 1) &&
(IntCard > CurrentCard)), the test on i will come first which
should prevent IntCard ever being accessed with an invalid i. When
you fix this, you should find that you have another problem which the
first problem was masking.

Other comments on your code:

1 C# arrays come with their length built in, so there is no need for a
separate parameter to hold it. With a separate parameter there is
scope for errors, just use IntCard.Length instead.

2 You have an anonymous pair of braces around the while statement.
They do not perform any useful function.

3 As a matter of style, and better maintainability, prefer '<' rather
than '<=' for the control variable test in a for loop:

for (int j = 1; j < length; j++)

Better maintainability because other C# (and C and C++ and Java)
programmers will expect for loops to look like that unless there is a
specific reason not to.

4 You are declaring CurrentCard and i inside a for loop. Probably
better to declare them once just outside the loop rather than each
time inside the loop.

5 Microsoft style guidelines say that you should use an initial lower
case (camelCase) for variables, hence prefer 'currentCard' to
'CurrentCard' etc. An initial capital (PascalCase) is used for
classes, functions and others, so your InsertionSort_Function is OK
except for the underscore which Microsoft standards discourage.
InsertionSortFunction would conform to the standard.

rossum
 
L

Laurent Bugnion

Hi,
Hello,
I posted this morning and recieved no responces., so I decided to
simplify my question.
How do I stop an exception from being thrown after the first run
through of the while clause:

It would be helpful to know which exception, but I think that it's
because you're modifying an array in a loop iterating the same array.

The solution is to create a temporary array with the same length and
type, copy the cards to that temporary array without modifying the
original one, and then copying the temp array's reference to the
original one.

That said, what does your method do? The IntCard array is not a "ref"
parameter, so the one outside of the method will not be modified.

HTH,
Laurent
private void InsertionSort_Function(uint[] IntCard,int length)
{
for (int j = 1; j <= (length-1); j++)
{
uint CurrentCard=IntCard[j];
int i =j-1;

{
while ((IntCard > CurrentCard) && (i > 1))
{
IntCard[i + 1] = IntCard;
i = i - 1;

}
}
IntCard[i+1]=CurrentCard;
}
}
}

Thanks
Mike
 
A

AMP

rossum,
Thanks,(And I did find the other problem)
Mike said:
Hello,
I posted this morning and recieved no responces., so I decided to
simplify my question.
How do I stop an exception from being thrown after the first run
through of the while clause:

private void InsertionSort_Function(uint[] IntCard,int length)
{
for (int j = 1; j <= (length-1); j++)
{
uint CurrentCard=IntCard[j];
int i =j-1;

{
while ((IntCard > CurrentCard) && (i > 1))
{
IntCard[i + 1] = IntCard;
i = i - 1;

}
}
IntCard[i+1]=CurrentCard;
}
}
}

Thanks
Mike

I would have a look at the order in which you have written your while
statement: while ((IntCard > CurrentCard) && (i > 1))

This will be evaluated left to right, so the first thing it tries is
to access IntCard. If i is an invalid value, this will throw an
exception. By reordering the statement as: while ((i > 1) &&
(IntCard > CurrentCard)), the test on i will come first which
should prevent IntCard ever being accessed with an invalid i. When
you fix this, you should find that you have another problem which the
first problem was masking.

Other comments on your code:

1 C# arrays come with their length built in, so there is no need for a
separate parameter to hold it. With a separate parameter there is
scope for errors, just use IntCard.Length instead.

2 You have an anonymous pair of braces around the while statement.
They do not perform any useful function.

3 As a matter of style, and better maintainability, prefer '<' rather
than '<=' for the control variable test in a for loop:

for (int j = 1; j < length; j++)

Better maintainability because other C# (and C and C++ and Java)
programmers will expect for loops to look like that unless there is a
specific reason not to.

4 You are declaring CurrentCard and i inside a for loop. Probably
better to declare them once just outside the loop rather than each
time inside the loop.

5 Microsoft style guidelines say that you should use an initial lower
case (camelCase) for variables, hence prefer 'currentCard' to
'CurrentCard' etc. An initial capital (PascalCase) is used for
classes, functions and others, so your InsertionSort_Function is OK
except for the underscore which Microsoft standards discourage.
InsertionSortFunction would conform to the standard.

rossum
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top