Hello I'm Mike and I'm not great at C and I need help with one of my programs. I've done the first part of the task which works fine. This is the first task :
"Design and write a program which throws two dice 300 times. Each time total the number of spots and count how many times each number of spots was thrown. Use a 12 element array to hold the spot counts."
And this is the code for it.
Code:
#include <stdio.h>#include <stdlib.h>
#include <time.h>
void initarray(int d[]);
void printstar(int n);
void fancyprint(int d[]);
int roll(void);
int numrolls=300;
int main(void) {
int d[13];
int i;
initarray(d);
printf("Total rolls: %1d\n\n", numrolls);
for (i = 0; i < numrolls; i++)
d[roll() + roll()]++;
fancyprint(d);
return 0;
}
void initarray(int d[]) {
int i;
for (i = 2; i < 13; i++)
d[i] = 0;
}
void printstar(int n) {
while (n > 0) {
printf("*");
n--;
}
}
void fancyprint(int d[]) {
int i;
printf("\n");
for (i = 2; i < 13; i++) {
printf("Sum:%3d |", i);
printstar(300*d[i]/numrolls);
printf("\n");
}
printf("\n");
}
int roll(void) {
return (int) (6.0*(rand()/(double)RAND_MAX)
+ 1.0);
}
I now have a problem with the second part.
This is the second part of the task:
"Extend program 3 to ask the user whether they want to throw 2, 3, or 4 dice. Use a 24 element array to hold the results, but only display either 12, 18 or 24 lines on the bar chart, depending on the number of dice chosen."
Please Help as I have no idea how to do the second part.
Thank you.