This is code called by main to sort an array using selections sort in the C language. I have a file opened in main and put the first ten ints into an array and the 11th into a variable and then a bunch of simple functions are called. The whole thing is repeated three times. For my test file, that last two iterations have the correct printed sort, but the first starts with 0, but I don’t have a 0 in my array. It also drops the last int.
Thanks in advance for all your help!!
Here is my code:
void sortme (int arry [], int last_int)
{
int temp;
int smallest_int;
int current_int;
int target_searcher;
int numPrinted;
for(current_int = 0; current_int < last_int; current_int++)
{
smallest_int = current_int;
for(target_searcher = current_int + 1; target_searcher <= last_int; target_searcher++)
if(arry[target_searcher] < arry[smallest_int])
smallest_int = target_searcher;
temp = arry[current_int];
arry[current_int] = arry[smallest_int];
arry[smallest_int] = temp;
} //end outter loop
numPrinted = 0;
printf("\nThe sorted array is: ");
for(current_int = 0; current_int < SIZE; current_int++)
{
printf("%4d", arry[current_int]);
if(numPrinted < COUNT)
numPrinted++;
else
{
printf("\n");
numPrinted = 0;
}
}
printf("\n");
return;
}
Here is my output for reference (most of the stuff is commenetd out in main.c):
The file opened.
Scanned into a[] and target is 33
ARRAY[1]
The contents in the array are: 40 32 57 27 67 6 3 89 2 99
The sorted array is: 0 2 3 6 27 32 40 57 67 89
The value searched, 33, was not found.
Scanned into a[] and target is 3
ARRAY[2]
The contents in the array are: 86 43 89 32 45 12 1 58 98 4
The sorted array is: 1 4 12 32 43 45 58 86 89 98
The value searched, 3, was not found.
Scanned into a[] and target is 11
ARRAY[3]
The contents in the array are: 1 2 3 4 5 6 7 8 9 10
The sorted array is: 1 2 3 4 5 6 7 8 9 10
The value searched, 11, was not found.
Closing the file.
The file closed.
You allow
target_searcherto be equal tolast_intwhile searching for the minimum. So, sometimes you get a random small value injected into your array (and mess with memory that doesn’t belong to you). Of course, I am assuminglast_intis the length of the array.When you are dealing with “valid indexes only”, the range is from
0tolen-1. You can see that with an array of length 1 (in case you are ever in doubt again). Since there is only 1 element, it is atarray[0], orarray[len-1].Having said that, it is usually customary to pass parameters as array and length instead of array and index of last valid element. It is more natural. Say, you have a large array with two blocks of
len1andlen2, and a function that does something with these partitions. If you use length as parameter, you use:If you were to use last valid index, there would be all these
+/-1terms you’d have to deal with. So it is either:or:
As for the answer of your second question: yes, the problem was caused by accessing an element that is outside the bounds of your array. Since C doesn’t have the safety net of checking array bounds, an error like this usually manifests itself as an unexplained value appearing in your results, or worse, an application crash. In some cases you are not as lucky, and it manifests the problem in a completely unrelated part of your program. So, it is best to be very sure about array element accesses.