它询问用户他们希望如何创建和排序的人随机数,并且由于某种原因,它在列表的末尾打印为零,我不知道为什么......它检查以确保多于1个数字是输入并输入少于10,000个。
#include <stdio.h>
#define MAX 10000
int randu(void);
void bubble(int a[], int num);
void swap(int*,int*);
int main (int argc, char *argv[]) {
int numsor,y;
int randoms[MAX];
int x=0;
if (argc == 1){
printf("How many numbers do you wish to sort? \n");
scanf("%d", &numsor);
}
if (argc == 2){
sscanf(argv[1], "%d", &numsor);
}
if (argc > 2){
printf("how many numbers do you wish to sort? \n");
scanf("%d", &numsor);
}
while (numsor<2 || numsor>10000){
if (numsor < 2){
printf("Error please enter a number more than 2. \n");
}
else{
printf("Error please enter a number less than 10,000. \n");
}
scanf("%d", &numsor);
}
y = randu();
for(x=0; x<numsor; x++){
randoms[x]=randu();
}
bubble(randoms,numsor);
for( x = 0; x <= numsor; x++){
printf( "[%d]\n", randoms[x]);
}
}
int randu(){
static int seed=17;
seed=(25179*seed+13849)%65536;
return seed;
};
void swap(int *a, int *b){
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void bubble(int a[], int num){
int i,j;
for(i=0; i<num-1; i++)
for(j=num-1;i<j;j--)
if(a[j-1]>a[j])
swap(&a[j-1], &a[j]);
}
答案 0 :(得分:2)
此:
for( x = 0; x <= numsor; x++){
应该使用<
,否则你将迭代一次太多。