2016-01-04 41 views
-7

我試圖做一個程序,它在0-6之間隨機輸入10000個數字,並檢查每個數字有多少次變形。它變得瘋狂並且打印很多東西。請幫助我謝謝!我的程序變得瘋狂

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#define ARR_SIZE 10000 
#define NUM_OF_FACES 6 

int main() 
{ 
    srand(time(NULL)); 
    int arrCube[ARR_SIZE]; 
    int i=0,counter=0,j=0; 
    for(i = 0; i<ARR_SIZE; i++) 
    { 
     arrCube[i] = rand() % NUM_OF_FACES; 
    } 
    for(i=0;i<ARR_SIZE;i++) 
    { 
     counter=0; 
     for(j=i+1;j<ARR_SIZE -1;j++) 
     { 
      if (arrCube[i]==arrCube[j]) 
      { 
       counter++; 
      } 
     } 
     printf("%d times %d showed up\n",counter,arrCube[i]); 
    }  

    return (0); 
} 

另一件事:我知道我可以用另一個數組來代替嵌套循環來做這個程序。有人知道嗎?

+3

首先,你可以檢查固定值(0-6)而不是嵌套循環。其次,請寫一個有意義的標題。 – Arc676

+2

在您編寫代碼之前請仔細考慮。什麼是嵌套循環意味着計算? –

回答

1

你的嵌套循環確實不是最好的解決方案:你迭代的次數超過了必要的次數,這也導致了比你所需要的更多的輸出:實際上它有10,000行。瘋!

用0以上的循環替換陣列上的外環。→ NUM_OF_FACES

此外,您還可以通過保持一個數組int frequency[NUM_OF_FACES] = {}然後在0 → ARR_SIZE遞增frequency[arrCube[i]]每個i摺疊嵌套循環逼到一個。

然後你只需要在frequency一個新的,最後循環打印六行結果

3

你只需要擁有一個額外的數組,計算頻率:

int main() 
{ 
    srand(time(NULL)); 
    int arrCube[ARR_SIZE]; 
    int freqs[NUM_OF_FACES] = {0}; 
    int i=0,counter=0,j=0, val; 
    for(i = 0; i<ARR_SIZE; i++) 
    { 
     val = rand() % NUM_OF_FACES; 
     arrCube[i] = val; 
     ++freqs[val]; 
    } 

    for(i =0 ; i < NUM_OF_FACES; i++) 
     printf("%d showed up %d times\n",i,freqs[i]); 
} 
1

更換您的嵌套循環用一個簡單的bucket sort

int bucket[NUM_OF_FACES] = { 0 }; 

/* Put the "ruffles" in the right bucket */ 
for (i = 0; i < ARR_SIZE; i++) 
{ 
    bucket[arrCube[i]]++; 
} 

/* Print the result */ 
for (i = 0; i < ARR_SIZE; i++) 
{ 
    printf("%d times %d showed up\n", bucket[i], i); 
} 
1

首先,這是因爲下面的代碼「獲得的瘋狂」簡單地說:

for(i=0;i<ARR_SIZE;i++) 
{ 
    counter=0; 
    ... 
    printf("%d times %d showed up\n",counter,arrCube[i]); 
} 

printf的重複ARR_SIZE次

下面的代碼將最有可能是你想要什麼:

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#define TESTS 10000 
#define NUM_OF_FACES 6 

int main() 
{ 
    srand(time(NULL)); 
    int arrCube[NUM_FACES]; 
    int i; 

    /* init every entry to 0 */ 
    for (i = 0; i < NUM_FACES; i++) 
    { 
     arrCube[i] = 0; 
    } 

    for (i = 0; i < TESTS; i++) 
    { 
     arrCube[ rand() % NUM_OF_FACES ]++; 
    } 

    for (i = 0; i < NUM_FACES; i++) 
    { 
     printf("the number %d showed up %d times\n", i, arrCube[i]); 
    } 
} 
2

這裏是另一個代碼版本,這應該解決您的問題。我試圖保持接近您的原始版本。在舊版本中,您檢查每個隨機數的次數與數組中出現的次數相同。

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#define ARR_SIZE 10000 
#define NUM_OF_FACES 6 

int main() 
{ 
    srand(time(NULL)); 
    int arrCube[ARR_SIZE]; 
    int i=0,counter=0,j=0,all=0; 
    for(i = 0; i<ARR_SIZE; i++) 
    { 
     arrCube[i] = rand() % NUM_OF_FACES; 
    } 
    for(i=0;i<NUM_OF_FACES;i++) 
    { 
     counter = 0; 
     for(j=0;j<ARR_SIZE;j++) 
     { 
      if (i==arrCube[j]) 
      { 
       counter++; 
      } 
     } 
     printf("%d times %d showed up\n",counter,i); 
     all=all+counter; 
    } 
    printf("total %d\n",all); 

    return (0); 
}