[英]Asterisk Histogram for random dice rolls of 3 dice
#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之間的和總數。直方圖輸出應如下所示:
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 - * * * * * * * *
您可以使用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];
用星號填充char數組的方法可以通過多種方式完成,其中一些已在此處進行了說明,但幾個小時前就發布了一個問題...
在另一件事上:
您正在調用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積極工作 。 解決這個問題,請...
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");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.