2016-02-19 16 views
0

我有一個家庭作業要做:我需要測量一個隨機數組中有100000個數字的時間。當我嘗試隨機生成數字時出現錯誤。如果我不隨機生成數字,則每次都得到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 < n - i -1; ++j){ 
      if (a[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]); 
    } 
} 

正如你看到的,我需要用頭......這實在是無聊...

編輯

現在我完全重寫所有程序沒有錯誤沒有警告,但它不打印數組,排序時間仍然是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 < n - i -1; ++j){ 
      if (a[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; 
} 
+2

用'-Wall -Wtratra'編譯並將每個警告視爲錯誤。例如:'* a = rand()%100;'給**指針分配一個隨機值**(而不是指向的值)。而且,你不會提高你的指針。在上面檢查'NULL'(**!**)也不是檢查分配,而是檢查傳遞的指針指針。 –

+0

關於標題..你沒有正確使用它們。它們應該包含(主要)聲明,而不是函數定義(除了它們爲了某種好的目的而內聯)。 –

+1

您打開一個文件但不使用它。你傳遞一個指向'n'的指針,但不要使用它。你傳遞一個名爲'file'的char指針,但不要使用它。似乎你需要退後一步,並且想想你想做什麼。 – user3386109

回答

1

void beolvas(int **a,int *n,const char * file); 聲明a作爲指針的指針。

,但此行:

*a = rand() % 100;

是解引用它只有一次和值分配給指針(有效地導致,因爲它是malloc之前-ed內存泄漏)

所以你會得到各種未定義的行爲。

0

你不應該使用int ** var,而只是一個int *。事實上,你只需要將int的地址傳遞給你的beolvas函數。然後,不要忘記初始化隨機數感謝:

srand(time(NULL)); 

否則,您將始終生成相同的「隨機」數字。這是由於C編譯器需要時間作爲參考。

不要忘記,C中的.h文件應該只包含函數的頭文件。函數本身應該用同名的.c文件寫入。

其餘的,你的代碼看起來沒問題。嘗試改變你的指針,並給我們的錯誤,如果有的話。

+2

「其餘的,你的代碼看起來沒問題。」我強烈反對。而且,OP已經播種了。 –

0

[..]沒有錯誤沒有警告[..]

您使用哪種編譯器?我高度疑問:

main,你聲明的指針double

double *a = NULL; 

您傳遞指針的地址(這樣一個指針的指針,一個double **)到您的功能,但他們期待一個指針到一個整數:

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

這同樣適用於第二個參數,主要是在叫你的函數n:樂趣ction聲明承擔int,但你通過...

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

......一旦一個指向double,並兩次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 *,以使調用者(和類型系統)明白數組在打印期間未被修改。
  • 不要忘記free陣列一旦完成。