对于此选择排序,当我的数组中从未使用 0 时,为什么第一个数字是 0? (在c中)
这是 main 调用的代码,用于在 C 语言中使用选择排序对数组进行排序。我在 main 中打开了一个文件,并将前 10 个整数放入数组中,将第 11 个整数放入变量中,然后调用一堆简单的函数。整个事情重复了三遍。对于我的测试文件,最后两次迭代具有正确的打印排序,但第一次以 0 开头,但我的数组中没有 0。它还会删除最后一个 int。
预先感谢您的所有帮助!
这是我的代码:
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;
}
这是我的输出供参考(大部分内容都在 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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在搜索最小值时,您允许
target_searcher
等于last_int
。因此,有时您会在数组中注入一个随机的小值(并弄乱不属于您的内存)。当然,我假设last_int
是数组的长度。当您处理“仅有效索引”时,范围是从
0
到len-1
。您可以通过长度为 1 的数组看到这一点(以防您再次有疑问)。由于只有 1 个元素,因此它位于array[0]
或array[len-1]
处。话虽如此,通常习惯以数组和长度的形式传递参数,而不是数组和最后一个有效元素的索引。这是更自然的。假设您有一个包含
len1
和len2
两个块的大型数组,以及一个对这些分区执行某些操作的函数。如果您使用长度作为参数,则可以使用:如果您要使用最后一个有效索引,则您必须处理所有这些
+/-1
术语。所以它是:或:
至于第二个问题的答案:是的,问题是由于访问数组边界之外的元素引起的。由于 C 没有检查数组边界的安全网,因此这样的错误通常表现为结果中出现无法解释的值,或更糟糕的是应用程序崩溃。在某些情况下,您没有那么幸运,它会在程序中完全不相关的部分体现问题。因此,最好非常了解数组元素的访问。
You allow
target_searcher
to be equal tolast_int
while 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_int
is the length of the array.When you are dealing with "valid indexes only", the range is from
0
tolen-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
len1
andlen2
, 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
+/-1
terms 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.