471,610 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,610 software developers and data experts.

2nd try Sorting Algorithm.

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[i] CurrentCard) && (i 1))
{
IntCard[i + 1] = IntCard[i];
i = i - 1;

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

Thanks
Mike

Nov 29 '06 #1
3 1726
On 29 Nov 2006 11:00:26 -0800, "AMP" <am******@gmail.comwrote:
>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[i] CurrentCard) && (i 1))
{
IntCard[i + 1] = IntCard[i];
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[i] CurrentCard) && (i 1))

This will be evaluated left to right, so the first thing it tries is
to access IntCard[i]. If i is an invalid value, this will throw an
exception. By reordering the statement as: while ((i 1) &&
(IntCard[i] CurrentCard)), the test on i will come first which
should prevent IntCard[i] 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

Nov 29 '06 #2
Hi,

AMP wrote:
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[i] CurrentCard) && (i 1))
{
IntCard[i + 1] = IntCard[i];
i = i - 1;

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

Thanks
Mike

--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Nov 29 '06 #3
AMP
rossum,
Thanks,(And I did find the other problem)
Mike
rossum wrote:
On 29 Nov 2006 11:00:26 -0800, "AMP" <am******@gmail.comwrote:
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[i] CurrentCard) && (i 1))
{
IntCard[i + 1] = IntCard[i];
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[i] CurrentCard) && (i 1))

This will be evaluated left to right, so the first thing it tries is
to access IntCard[i]. If i is an invalid value, this will throw an
exception. By reordering the statement as: while ((i 1) &&
(IntCard[i] CurrentCard)), the test on i will come first which
should prevent IntCard[i] 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
Nov 29 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Shaunak Kashyap | last post: by
22 posts views Thread by mike | last post: by
25 posts views Thread by Allie | last post: by
4 posts views Thread by rushik | last post: by
11 posts views Thread by Registered User | last post: by
5 posts views Thread by chrisguest | last post: by
17 posts views Thread by Umesh | last post: by
5 posts views Thread by lemlimlee | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.