안녕하세요 저는 rand() 및 time.h에 주사위를 굴리는 주사위에 필요한 코드를 작성했습니다. 기본적으로 사용자는 주사위를 굴리기를 원하는 횟수를 입력 한 다음 그 횟수만큼 X를 굴 리도록 요청합니다. 그것은 1-6 숫자의 각 하나가 배열에 롤백 된 시간과 다른 배열에있는 각 숫자의 백분율을 저장합니다.롤링 주사위 히스토그램
이제 원하는 것은 히스토그램을 작성하는 것입니다. 숫자는 1-6이고 - X 축은 백분율과 | Y 축에는 별표 (*)가 표시됩니다. 이 같은
뭔가 :
10% | *
8% |* * *
6% |* * * * *
4% |* * * * * *
2% |* * * * * *
+------------
1 2 3 4 5 6
나는 축복하는으로 시작하는 뭔가를 찾기 위해 구글 검색이 있지만 난 정말거야 날 시작 도움 이잖아 내 경우와 유사한 것을 발견했다. 에 따라서
내 코드는 다음과 같습니다
이#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int random_number();
float calc_percentage(int totals, int nums);
float calc_maxper(float perc[6]);
float calc_minper(float perc[6]);
float permin;
float permax;
int main(void)
{
int nums;
int i;
int totals[6] = {0};
float percentages[6] = {0};
srand(time(NULL));
printf("How many numbers to generate?");
scanf("%d", &nums);
for (i = 1; i <= nums; i++)
{
int x = random_number();
totals[x-1]++;
}
for (i = 0; i<6; i++)
{
percentages[i] = calc_percentage(totals[i],nums);
printf("The percentage of each number is: %.2f%\n", percentages[i]);
}
permin = calc_minper(percentages);
permax = calc_maxper(percentages);
if (((permax) - (permin)) > 5)
printf("The generator is not good.\n");
printf("The percentage difference is:%.1f\n", permax-permin);
system("pause");
return 0;
}
int random_number()
{
int randnum;
randnum = 1 + (rand() % 6);
return randnum;
}
float calc_percentage(int totals, int numbers)
{
float a;
a = (totals * 100)/numbers;
return a;
}
float calc_minper(float perc[6])
{
int i;
float min;
min = perc[0];
for (i=1; i<6; i++)
{
if (perc[i] < min)
min = perc[i];
}
return min;
}
float calc_maxper(float perc[6])
{
int i;
float max;
max = perc[0];
for (i=1; i<6; i++)
{
if (perc[i] > max)
max = perc[i];
}
return max;
}
좋아, 내가 마지막으로이 작업이 완료되었다.
길고 지저분하지만 일을 끝내게됩니다 !!
printf("20%|");
for (i=0; i<6; i++)
{
if (percentages[i] >= 20)
printf("* ");
}
printf("\n");
printf("16%|");
for (i=0; i<6; i++)
{
if (percentages[i] >= 16)
printf("* ");
}
printf("\n");
printf("12%|");
for (i=0; i<6; i++)
{
if (percentages[i] >= 12)
printf("* ");
}
printf("\n");
printf(" 8%|");
for (i=0; i<6; i++)
{
if (percentages[i] >= 8)
printf("* ");
}
printf("\n");
printf(" 4%|");
for (i=0; i<6; i++)
{
if (percentages[i] >= 4)
printf("* ");
}
printf("\n");
printf(" +------------\n");
printf(" 1 2 3 4 5 6\n");
감사합니다. 유익한 아이디어가 있으면 모든 귀가 짧아집니다!
당신이 stucked 정확히 어디를받을 수 있나요? 안녕? 숙제? – Matthias
전체 코드를 디버깅하는 대신 현재 솔루션의 문제점은 무엇입니까? 현재 출력은 무엇입니까? –
calc_percentage 함수는 float로 형 변환 된 int를 반환합니다. 100 대신 100.0을 사용하여 강제로 부동 소수점 연산을 수행하십시오. – Spidey