【发布时间】:2015-03-16 01:30:57
【问题描述】:
我是一名学习计算机科学的初学者 c 程序员,我正在尝试创建一个排序程序来对整数数组进行排序,尽管我一直得到错误的结果,这就是我目前得到的结果:
#include <stdio.h>
#include <string.h>
#define TAM 9
int sort_array(int num1[],int num2[]);
int main(){
int i=0;
int num1[TAM] = {5,6,2,4,7,1,3,0};
int num2[TAM] = {0};
int * ptr_num2 = num2;
sort_array(num1,num2);
while(*ptr_num2 != '\0'){
printf("%c",*ptr_num2+48);
ptr_num2++;
}
putchar('\n');
return 0;
}
int sort_array(int num1[],int num2[]){
int min=256,max=0,i,j;
int * ptr_num1 = num1;
int * ptr_max = num1;
int * ptr_num2 = num2;
/* check for max */
while(*ptr_max != '\0'){
if(*ptr_max > max) max = *ptr_max;
ptr_max++;
}
for(i=0;i<TAM-1;i++){
/* check for min */
for(j=0;j<TAM-1;j++){
if(*ptr_num1 < min) min = *ptr_num1;
ptr_num1++;
num1[i] = max;
}
*ptr_num2 = min;
ptr_num2++;
}
return 0;
}
我已经为此绞尽脑汁好几个小时了。
编辑:忘了提到其中一些可能没有意义,因为我只是在尝试一些东西。
【问题讨论】:
-
对于某些特定输入,您的实际输出和预期输出是多少?
-
你的程序中也可能有undefined behavior,因为数组
num1被声明包含九个元素,但你只初始化八,这意味着最后一个元素是不确定的。 -
您为什么会选择如此复杂的排序程序?有什么奇怪的要求吗?
-
另外,为什么要使用 null-at-the-end 技巧?不能用实际长度吗?
-
与论坛网站不同,我们不使用“谢谢”、“感谢任何帮助”或Stack Overflow 上的签名。请参阅“Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?.