Hello all
So what am trying to do is write a program that will simulate the rolling of a single dice using rand() and time.h.
1)The user will input the number of times he wants the dice to be rolled.
2)The generator will roll the dice X times and count the times each one of 1-6 number was generated and save them in an array.
3)It well then count the percentage each of the 6 numbers was generated and save it in another array.
4)It will then find the minimum and maximum percentage and subtract max-min and if its more than 5% it will print a message that says the generator is faulty.
I need to make a histogram for each number with +,- axis and stars but I really haven't gotten there yet cause it definitely sounds scary.
So I hope I made everything clear.
This is my code which gives awkward results.
P.S It compiles ok but the results are strange.
Hope I get some ideas from all you.
-Thanks in advance!!
So what am trying to do is write a program that will simulate the rolling of a single dice using rand() and time.h.
1)The user will input the number of times he wants the dice to be rolled.
2)The generator will roll the dice X times and count the times each one of 1-6 number was generated and save them in an array.
3)It well then count the percentage each of the 6 numbers was generated and save it in another array.
4)It will then find the minimum and maximum percentage and subtract max-min and if its more than 5% it will print a message that says the generator is faulty.
I need to make a histogram for each number with +,- axis and stars but I really haven't gotten there yet cause it definitely sounds scary.
So I hope I made everything clear.
This is my code which gives awkward results.
P.S It compiles ok but the results are strange.
Code:
#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]++;
printf("%d", x);
}
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("According to the percentages of each number the generator is diss-functional\n");
printf("%.2f\n", permin);
printf("%.2f\n", permax);
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;
}
-Thanks in advance!!
Comment