随机数组有很多数字得到错误

我有一个功课要做:我需要在一个包含100000个数字的随机数组中测量bubblesort的时间。 当我尝试随机生成数字时,我收到错误。如果我不随机生成数字,我每次都会得到0。 到目前为止我这样做了:

main.c int main() { int *a,n = 0; srand(time(0)); beolvas(&a,&n,"be.txt"); clock_t start,stop; start = clock(); bubblesort(a,n); stop = clock(); float timespent = (stop - start)/CLOCKS_PER_SEC; printf("%f\n",timespent); kiir(a,n); free(a); return 0; } kibe.c(sorry I write it bad) void beolvas(int **a,int *n,const char * file) { int i; FILE * fin; fin = fopen("be.txt", "rt"); *a = (int*)malloc(*n*sizeof(int)); if(a == 0){printf("Error");return 0;} for(i = 0; i < 100000; ++i){ *a = rand() % 100; } fclose(fin); } void bubblesort(int *a, int n) { int i,j,csere; for(i = 0; i < n-1; ++i){ for(j = 0; j  a[j + 1]){ csere = a[j]; a[j] = a[j + 1]; a[j + 1] = csere; } } } } void kiir(int *a,int n) { int i; for(i = 0; i < n; ++i){ printf("%i ",a[i]); } } 

如你所见,我需要使用标题……这真的很无聊……

编辑

现在我完全重写所有程序没有错误没有警告,但它不打印出arrays,排序时间仍然是0.我忘了做什么? 为什么我的写函数什么都不做?

sema.c

 void read(int *a,int n) { int i; scanf("%d",&n); a = (int*)malloc(n*sizeof(int)); if(a == 0){printf("Error");return 0;} for(i = 0; i < n; ++i){ a[i] = rand() % 100; } } void bubblesort(int *a,int n) { int i,j,csere; for(i = 0; i < n-1; ++i){ for(j = 0; j  a[j + 1]){ csere = a[j]; a[j] = a[j + 1]; a[j + 1] = csere; } } } } void write(int *a,int n) { int i; for(i = 0; i < n; ++i){ printf("%i ",a[i]); } } 

sema.h

 void read(int*,int*); void write(int*,int); void bubblesort(int*,int); 

main.c中

 int main() { double *a = NULL ,n = 0; read(&a,&n); clock_t start,stop; start = clock(); bubblesort(a,n); stop = clock(); float elapsedTime = (stop - start)/CLOCKS_PER_SEC; printf("%f",elapsedTime); write(a,n); free(a); return 0; } 

void beolvas(int **a,int *n,const char * file);a声明为指针指针。

但这一行:

*a = rand() % 100;

是只取消引用它一次并为指针赋值(有效导致内存泄漏,因为它之前是malloc -ed)

所以你得到了各种未定义的行为。

你不应该使用int ** var而只是使用int *。 实际上,您只需要将int的地址传递给您的beolvas函数。 然后,不要忘记启动随机函数,这要归功于:

 srand(time(NULL)); 

否则,您将始终生成相同的“随机”数字。 这是因为C编译器需要时间作为参考。

不要忘记C中的.h文件应该只包含函数头。 函数本身应该写在同名的.c文件中。

其余的,你的代码看起来没问题。 尝试更改指针,如果有的话,给我们错误。

[..]没有错误没有警告[..]

你在用哪个编译器? 我非常怀疑:

main ,您声明一个指向double的指针:

 double *a = NULL; 

您将该指针的地址(因此指向该指针的指针, double ** )传递给您的函数,但是它们期望指向整数的指针:

 void read(int*,int*); void write(int*,int); void bubblesort(int*,int); 

第二个参数也是如此,在函数中大多称为n :函数声明假定为int ,但是你传递了…

 double n = 0; read(&a,&n); // ... bubblesort(a,n); // ... write(a,n); 

…曾经是指向一个double精度的指针,两倍于你的函数的两倍。

通常,您似乎对传递参数的概念感到困惑,特别是与指针结合使用时。 请记住:传递给C中函数的每个参数都会被复制 。 从而:

 void foo(int a) { a = 42; // a is now 42 for the rest of only this function } void bar(void) { int a = 21; foo(a); // a is still 21, this is not the a from foo. } 

也许你应该阅读词汇范围,它在C(以及大多数当前语言)中使用。 现在考虑传递一个指针:

 static int fred = 42; void foo(int * a) { // Dereferencing a gives us acces to whatever it points to *a = 42; // But modifying the variable a ... a = &fred; // a points to fred for the rest of only this function } void bar(void) { int john = 21; int * a = &john; foo(a); // a still points to john, this is not the a from foo. // but john is now 42! } 

传递指针也只复制指针(将其视为地址)。 但是使用该指针,您可以访问它指向的任何内容。

回到你手头的问题,我认为你应该尝试编写以下函数(假设你仍然需要int数字):

 /** Allocate an array with random integers. This function takes as parameter the count of elements the array is supposed to have. A pointer to the array filled with random integers should be returned. */ int * allocate_random_integers(size_t count); /** Sort the array. This function is supposed to sort the given array (a pointer to its first element is given along with the number of elements). */ void bubblesort(int * array, size_t count); /** Print the array to stdout. This function is supposed to write each element of the given array to stdout. */ void print(int const * array, size_t count); 

有几点需要注意:

  • 我使用size_t来表示“大小”(包括索引)。
  • 除了从allocate_random_integers返回指针之外,还可以将指针传递给指针,并将指向该指针的指针设置为指向数组的指针( void allocate_random_integers(int ** array_ptr, size_t count) )。
  • print采用int const *而不是int *来使调用者(和类型系统)明白在打印期间不修改数组。
  • 一旦完成,不要忘记freearrays。