2015-04-29 52 views
2

我現在面臨一個問題,似乎無法解決它。每當我開始我的代碼時,我都會遇到分段錯誤。我的目標是擲兩個骰子。該值由隨機數生成。我想滾動10000次並將值保存在一個數組中,所以我可以在最後創建一個小圖來顯示切塊的值。我很感謝有關如何解決問題的任何幫助和提示。這裏是我的代碼:創建一個骰子圖

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

#define DICE  2 
#define DICEEYES (DICE * 6) -DICE +1 
#define COUNT  10000 

int diceRoll(int dice); 

int main(void) 
{ 
    srand(time(0)); 
    int valuesOfRoll[DICEEYES] = { 0 }; 
    for(int i = 0; i < COUNT; i++) 
    { 
    int index = diceRoll(DICE) - DICE; 
    valuesOfRoll[index]++; 
    } 
    for(int i = 0; i < DICEEYES; i++) 
    { 
    if(valuesOfRoll[i] < 1) continue; 
    printf("The number %2d was rolled %4d times\r\n", i + DICE, valuesOfRoll[i]); 
    } 
    return 0; 
} 

int diceRoll(int dice) 
{ 
    int sum; 
    for(int i = 0; i < dice; i++) 
    { 
    sum += rand() % 6 + 1; 
    } 
    return sum; 
} 
+2

C = C++,再加上你嘗試過調試和* *知道它崩潰? – crashmstr

+0

我不得不承認我是新手。到目前爲止還沒有嘗試過。 – Valinho

+2

請打開編譯器的完整警告(嘗試進行優化或不進行優化),它會告訴您至少有一個問題。並請編輯您的標籤,這看起來和感覺像C,而不是C++。 – Mat

回答

1

初始化的sum值在函數(優選爲0),否則將保持一些垃圾值。

int diceRoll(int dice) 
{ 
int sum; 
for(int i = 0; i < dice; i++) 
    { 
    sum += rand() % 6 + 1; 
} 
return sum; 
} 

垃圾值可能是一個非常大的數字,所以會是函數的返回值。所以,當所返回的值是在該線中使用:

int index = diceRoll(DICE) - DICE; 

則陣列將具有大量,valuesOfRoll[some_large_integer]++;的索引。這會導致數組超出邊界的情況,並導致段錯誤。

+0

也感謝你的努力和解釋。這解決了這個問題。 :) – Valinho

4

你應該在功能diceRoll(),你正在使用的索引值,可以初始化總和,

/* always return value between [dice..dice*6]*/ 
int diceRoll(int dice) 
{ 
    int sum=0; 
    for(int i = 0; i < dice; i++) 
    { 
    sum += rand() % 6 + 1; 
    } 
    return sum; 
} 

因爲錢還可以爲任意值(堆棧上)不屬於ValuesOfRoll []數組。避免的一種方法是使用模運算符將返回的索引限制在有效範圍內。

int main(void) 
{ 
    srand(time(0)); 
    int valuesOfRoll[DICEEYES] = { 0 }; 
    for(int i = 0; i < COUNT; i++) 
    { 
    int index = diceRoll(DICE) - DICE; 
    /* limit index to [0..DICEEYES] */ 
    index %= DICEEYES; 
    valuesOfRoll[index]++; 
    } 
    for(int i = 0; i < DICEEYES; i++) 
    { 
    if(valuesOfRoll[i] < 1) continue; 
    printf("The number %d was rolled %4d times", i + DICE, valuesOfRoll[i]); 
    } 
    return 0; 
} 
+0

我還沒有看到這個錯誤。這解決了這個問題。非常感謝你。 – Valinho

1

有點遲了,故障沒有初始化sumDICEEYES

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

#define DICE  2 
#define DICEEYES (DICE * 6) 
#define COUNT  10000 

int diceRoll(int dice) 
{ 
    int i, sum = 0;       // need to initialise! 
    for(i = 0; i < dice; i++) 
     sum += rand() % 6 + 1; 
    return sum; 
} 

int main(void) 
{ 
    int i; 
    int valuesOfRoll[DICEEYS+1] = { 0 };  // size of array 
    srand((unsigned)time(0)); 

    for(i = 0; i < COUNT; i++) 
     valuesOfRoll[diceRoll(DICE)]++; 

    for(i = DICE; i <= DICEEYES; i++) 
     printf("The number %2d was rolled %4d times\n", i, valuesOfRoll[i]); 
    return 0; 
} 

程序輸出:

The number 2 was rolled 264 times 
The number 3 was rolled 573 times 
The number 4 was rolled 798 times 
The number 5 was rolled 1145 times 
The number 6 was rolled 1373 times 
The number 7 was rolled 1697 times 
The number 8 was rolled 1385 times 
The number 9 was rolled 1143 times 
The number 10 was rolled 835 times 
The number 11 was rolled 528 times 
The number 12 was rolled 259 times 
+0

但在這種情況下,您正在創建兩個數組,它們並不真正使用[0]和[1]元素。 – Valinho

+0

只有一個數組。 –

+0

你說得對,我的意思是沒有使用的兩個元素。 – Valinho

0

下面的代碼

1) cleanly compiles 
2) has corrections to the output formatting 
3) properly initializes values (good case for always initialize all values) 
4) outputs the proper data 
5) does not try to fiddle offset/indexes, etc 
6) uses meaningful #define names, function parameter names, etc 

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

#define NUM_DICE   (2) 
#define NUM_ROLLS   (10000) 
#define MIN_DICEROLL_VALUE (2) 
#define MAX_DICEROLL_VALUE (12) 

int diceRoll(int numDice); 

int main(void) 
{ 
    srand(time(NULL)); 

    int valuesOfRoll[MAX_DICEROLL_VALUE+1] = { 0 }; // notice the +1 

    for(int i = 0; i < NUM_ROLLS; i++) 
    { 
     int index = diceRoll(NUM_DICE); 
     valuesOfRoll[index]++; 
    } 

    for(int i = MIN_DICEROLL_VALUE; i <= MAX_DICEROLL_VALUE; i++) 
    { 
     // 2d for values 2...12 5d for values 0...10000 \n so each output on anew line 
     printf("The number %2d was rolled %5d times\n", i, valuesOfRoll[i]); 
    } 
    return 0; 
} // end function: main 


int diceRoll(int numDice) 
{ 
    int sum = 0; // be sure to initialize value 

    for(int i = 0; i < numDice; i++) 
    { 
     sum += (rand() % 6) + 1; 
    } 
    return sum; 
} // end of function: diceRoll 
+0

請注意,'valueOfRolls [] '數組實際上沒有使用。不使用這兩個值使得代碼更容易編碼/讀取/理解 – user3629249

+0

您應該從'time'中施加返回值,以證明您聲明的乾淨編譯。 –

+0

作爲一個通用的解決方案,應將MIN_DICEROLL_VALUE和MAX_DICEROLL_VALUE分別定義爲DICE和DICE * 6:錯誤的編碼實踐。 –