2013-05-27 155 views
0

我有這個代碼的問題,因爲當我運行它時,我得到一個數字隨機生成器的無限循環。我想要做的是分配給數組,從1到9的99個數字,然後進行一些數學簡單的操作。生成一個範圍內的隨機整數(無限循環)

#include <stdio.h> 
#include <stdlib.h> 
#define SIZE 99 
void mean(const int a[]); 
void median(int a[]); 
void mode(int freq[] , const int a[]); 

int main (void) { 
    int response[SIZE]; 
    int frequency [10]; 
    int i; 
    srand(time(NULL)); 
    for (i = 0; i<= SIZE ; i++) { 
     response[i] = (rand() % 6) +1 ; 
     printf("%d", response[i]); 
    } 
    mean(response); 
    median(response); 
    mode(frequency , response); 

return 0; 
} 


void mean(const int a[]){ 
    int j , total = 0; 
    float mean; 
    printf("********\n%6s\n********\n\nThe mean is the average value of the data\nitems.", "Mean"); 
    printf("The mean is equal to the total of\n all the data items"); 
    printf("divided by the number\n of data items (%d):", SIZE); 
    for(j = 0 ; j <= SIZE ; j++){ 
     total += a[j]; 
    } 
    mean = (float) total/SIZE; 
    printf("The mean value for\nthis run is %d/%d = %f", total, SIZE, mean); 
} 

void median(int a[]){ 
    int i, j, n, median, hold; 
    n=1; 
    hold = 0; 
    printf("********\n%7s\n********\n\nThe unsorted array of responses is\n", "Median"); 
    for (i=0;i<=SIZE;i++){ 
     if ((i/10) <= n){ 
     printf("%d", a[i]); 
     } 
     else{ 
     printf("\n"); 
     n++; 
     } 
    } 
    printf("The sorted array is\n"); 
    for(i=0;i<=SIZE;i++){ 
     for(j=0;j<=SIZE-1;j++){ 
     if (a[j]>a[(j+1)]){ 
      hold = a[j]; 
      a[j] = a[ (j + 1)]; 
      a[ (j + 1)] = hold; 
     } 
     } 
    if ((i/10) <= n){ 
     printf("%d", a[i]); 
     } 
     else{ 
     printf("\n"); 
     n++; 
     } 
    } 
    median = a[SIZE/2]; 
    printf("The median is element %d of\nthe stored %d element array.\n", SIZE/2 , SIZE); 
    printf("For this run the median is %d", median); 
} 

void mode (int freq [] , const int a[]){ 
    int j, o, mode , i, rating; 
    printf("********\n%6s\n********\n\n%10s%12s%12s", "Mode" ,"Response" ,"Frequency", "Histogram"); 
    for(j=0; j<= SIZE ; j++){ 
     ++freq[a[j]]; 
    } 
    for (i=0 ; i <= 10 ; i++){ 
     printf("%10d%12d   ", i, freq[i]); 
     for (o=0; o<=freq[i];o++){ 
     printf("*"); 
     } 
     printf("\n"); 
     if (freq[i] > freq[i+1]){ 
     mode = freq[i]; 
     rating = i; 
     } 
    } 
    printf("The mode is the most frequent value.\n"); 
    printf("For this run the mode is %d which occured %d times", rating ,mode); 
} 
+1

爲什麼'rand()%6'當你想要1到9之間的數字。它應該是'rand()%9 + 1' ... – ShuklaSannidhya

+0

注意[modulo bias](http:// stackoverflow .COM /問題/ 10984974 /爲什麼-DO-人說,有 - 是 - 模偏置時 - 使用 - 一個隨機數發生器?LQ = 1)。 –

+0

'frequency'數組應該在'mode'函數的本地。你不要在別的地方使用它。在main中聲明並將其傳遞給'mode'沒有意義。相反,將它聲明爲'mode'的本地。 – ShuklaSannidhya

回答

3

C數組基於零的,以便有效索引

int response[SIZE]; 

是[0..SIZE-1]。您的循環寫入response[SIZE],這超出了分配給response的內存末尾。這導致未定義的行爲。

如果你得到一個無限循環,聽起來好像response[SIZE]的地址和循環計數器的地址i一樣。 (rand() % 6) +1將在[1..6]範圍內,因此退出前循環的最終迭代將始終將i重置爲較低值。

您可以通過更改您的循環以更快地退出一個迭代來解決此問題。即改變

for (i = 0; i<= SIZE ; i++) { 

for (i = 0; i< SIZE ; i++) { 

請注意,您的其他功能都具有類似的錯誤。所有for環路應替換它們的<=退出條件與<

+0

那麼爲什麼會導致無限循環呢? – ShuklaSannidhya

+2

@ShuklaSannidhya我已經更新了我的答案。現在讓我知道它是否更清晰。 – simonc

1

當您訪問array[SIZE]時,您寫入數組的末尾。 任何數組聲明

type_t array[SIZE]; 

不具有元件array[SIZE]。所以所有循環必須從0到< SIZE,而不是< = SIZE。 在計算機文獻中,這被稱爲偏移錯誤。你不是第一個,也不會是最後一個,如果它是任何安慰:-)

從技術上講,這調用未定義的行爲,其中一個無限循環是一種方式。但請參閱下面的評論,對這裏真正發生的事情進行瘋狂猜測。

+0

爲什麼會導致無限循環? – ShuklaSannidhya

+0

很可能是因爲在一些函數(mean(),median(),mode())中你有'a []'作爲最後一個參數,所以第一個聲明的局部變量是一個循環變量'i'或'j'。然後寫入'a []'然後循環變量。 – Jens

+0

這不是唯一的問題。即使修復一個也不會停止無限循環。代碼中有很多問題。 –

相關問題