2013-10-21 55 views
0
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <string.h> 

int main() 
{ 
int i; 
int d1, d2, d3; 
int a[16]; 
    srand(time(NULL)); 

for(i = 0; i <= 15; i++) 
    a[i] = 0; 
for(i = 0; i < 1000; i = i + 1) 
{ 
    d1 = rand() % 6 + 1; 
    d2 = rand() % 6 + 1; 
    d3 = rand() % 6 + 1; 
    ++a[d1 + d2 + d3 - 3]; 
} 
char asterisks[0x400]; 
memset(asterisks, '*', sizeof(asterisks)); 
for(i = 0; i <= 15; i = i + 1) 
{ 
    printf("%3d - ", i+3); 

    for(j=0;j<a[i];j++) 
    { 
    printf("%c ",'*'); 

    } 
    printf("\n"); 
} 

return 0; 
}  

更新的代碼。
目標是要有一個星號直方圖判斷3個擲骰子滾動1000次。這是3和18 直方圖輸出之間的計數總和的多少組合應該是這樣的:3個骰子的隨機骰子卷的星號直方圖

Frequency of Results for 3d6 
    3 - * 
    4 - ** 
    5 - **** 
    6 - ******* 
    7 - ********* 
    8 - *********** 
    9 - ************ 
10 - ************ 
11 - ************* 
12 - ********** 
13 - ************* 
14 - ******** 
15 - ****** 
16 - *** 
17 - *** 
18 - ** 

這是我到現在爲止輸出:

3 - * * 
4 - * * * * * * * * * * * * * * * * * * * * 
5 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
6 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * 
7 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * *  * * * * * * * * * * * * 
8 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * 
9 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
10 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
11 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * 
12 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
13 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * 
14 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * 
15 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * 
16 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
17 - * * * * * * * * * 
18 - * * * * * * * * 
+2

這是什麼問題? –

+3

如果a被定義爲13個條目的數組,那麼如何訪問最多18個?我只計算2個骰子.. – Leeor

+0

@Leeor:堆棧溢出...緩衝區溢出...發佈的代碼似乎與網站名稱相當吻合:p –

回答

1

3-18你需要陣列的16

int a[16]; 

a[0] indicates Number of 3 counts and 
a[1] indicates Number of 4 counts and 
.... 

a[15] indicates Number of 18 counts. 

大小您需要更改這個for循環

for(i = 0; i <= 15; i++) 
    a[i] = 0; 

及本聲明

a[d1 + d2+d3] = a[d1 + d2 + d3] + 1; 

修改這樣

 ++a[d1 + d2 + d3 - 3]; 

而最後的for循環也需要兩個循環。

for(i = 0; i <= 15; i = i + 1) 
    { 
    printf("%3d - ", i+3); 

    for(j=0;j<a[i];j++) 
     { 
     printf("%c ",'*'); 

     } 
     printf("\n"); 
    } 
+0

j從哪裏來? – user2306249

+0

@ user2306249您需要聲明附加變量。 – Gangadhar

1

您可以使用memset填寫用星號和"%*s"printf格式的緩衝打印多達你需要:

char asterisks[0x400]; 
memset(asterisks, '*', sizeof(asterisks)); 

for(i = 3; i <= 18; i = i + 1) 
{ 
    printf("%d - %*s\n", i, a[i], asterisks); 
} 

和是爲在評論中提到的,你應該定義a足夠長:

int a[19]; 
1

填充字符數組用星號可以通過多種方式來實現的方式,其中的一些已經解釋here,這是發佈了一個問題,但一幾個小時前...

在一個不同的問題:
你調用rand功能,無需提供種子... rand隨後將在默認情況下,表現爲雖然它是通過1作爲種子,並且你編譯的程序會產生不是很隨機的結果。
考慮加入這樣的:

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

int main() 
{ 
    int i; 
int d1, d2; 
int a[13]; 
    srand(time(NULL));//seed current time 
} 

也就是說,正如Jongware指出:

d1 = rand() % 6 + 1; 
d2 = rand() %6 + 1; 

只是沒有任何意義:

d1 = rand() % 18 + 1; 

不只是同樣的事情,需要只有1個函數調用。但是,仍然存在與此int值在做什麼有關的另一個問題:

您還使用了一個整數數組(a[13]),其聲明意味着您可以使用的最高偏移量爲12,最低(如以往)爲0.然而,你的循環開始在偏移量3,並一直到18 ...
我知道這個網站被稱爲stackoverflow,但你的代碼似乎是積極努力Buffer overflow。修復,請...

+0

不太明顯的優化:調用'rand'一次,範圍爲3..18。 – usr2564301

+0

@Jongware:你說得對(我已經把這個添加到我的答案中,記入你了),但是OP有更大的魚去炸IMO ......沒什麼問題:溢出問題 –

+0

呃,我的建議是開始在** 3 **處 - 你至少可以扔三個:-)(正如其他線程所評論的,「0..15」的範圍*完全相同。) – usr2564301