I wasn't able to replicate your problems on my system, but I see that your program has a few problems:
You've assumed your array to always contain 25 items, even though you don't enter 25 animals all the time. Your
getInput()
function should count how many animals are entered and return the value. On my system, all the other values in the array are uninitialized and print out as garbage. Maybe that's what's causing the crash on your system.
A second problem here is your comparison in
bubbleSort()
:
if(animals[j] < animals[j+1])
. Your comparison here isn't actually comparing strings, but comparing addresses. To compare strings, you have to use
strcmp()
,
A third problem is that your original and sorted list are the same array, and so when you finally get to printing you'll print the sorted list twice. If you want to preserve the original array, you'll have to make a copy of it and call
bubbleSort()
on the copy
Finally, your bubblesort implementation automatically assumes a worst-case number of passes over your array. I suggest replacing your outer
for
loop with a
while
loop that checks whether anything was swapped in the last pass over the array:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
bool swapped = true;
while (swapped)
{
swapped = false;
for (int i = 1; i < n; ++i)
{
if (outOfOrderCheck)
{
Swap();
swapped = true;
}
}
}
|
Sorry I can't directly explain the problems you're having.