Help with Dice Roll!

Ok here goes a better explanation, thought comments would help. I got this exercise I needed to fill in and I'm pretty much done with everything except the last 3 functions. Can someone help please?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void userMenu();
int setDieNumber();
int * prepareMemory(int die_number);
int throwDice();
int getHighestScore(int die_number, int * counters_array);
int throwDie();
int computeStatistics();
int showStatistics();


int main()
{
	// Init the random seed
	srand(time(NULL));

	printf("\n=== PROGRAM FOR DICE THROW STATISTICS ===\n");
	printf("By: {Christopher Amelio}\n");
	userMenu();

	return 0;
}

void userMenu()
{
	char option = 'a';
	int die_number = 0;
	int games_number = 0;
	int * counters_array = NULL;

	while (option != 'q')
	{
		printf("\nUser menu:\n");
		printf("\ta. Select dice number\n");
		printf("\tb. Compute statistics\n");
		printf("\tc. Show graphs\n");
		printf("\tq. Quit program\n");
		printf("Select an option: ");
		scanf(" %c", &option);

		switch (option)
		{
		case 'a':
			if (counters_array != NULL)
				free(counters_array);
			die_number = setDieNumber();
			counters_array = prepareMemory(die_number);
			break;
		case 'b':
			if (die_number > 0)
			{
				printf("Playing with %d die\n", die_number);
				printf("Number of games to be played: ");
				scanf("%d", &games_number);
				computeStatistics(die_number, games_number, counters_array);
			}
			else
			{
				printf("Error: Dice number not yet set!\n");
			}
			break;
		case 'c':
			if (counters_array)
				showStatistics(die_number, games_number, counters_array);
			else
				printf("No data available to display\n");
			break;
		case 'q':
			if (counters_array)
				free(counters_array);
			printf("Thanks for using the program! Good bye\n");
			break;
		default:
			printf("Invalid option! Try again ...\n");
			break;
		}
	}
}

int setDieNumber()
{
	int die_number = 0;
	while (die_number <= 0 || die_number > 10)
	{
		printf("Enter the number of dice you want to use (between 1 and 10): ");
		scanf("%d", &die_number);
		if (die_number <= 0 || die_number > 10)
			printf("Invalid number. Try again ...\n");
	}

	return die_number;
}

int * prepareMemory(int die_number)
{
	int * temp_array = NULL;

	
	temp_array = malloc(sizeof (int)* (die_number * 6 + 1));
	if (temp_array == NULL)
		printf("Error: Not enough memory!\n");
	else
		printf("Memory ready for scores from %d to %d\n", die_number, die_number * 6);

	return temp_array;
}

int throwDice()
{
	return rand() % 6 + 1;
}

int getHighestScore(int die_number, int * counters_array)
{
	int i;
	int max = counters_array[die_number];
	for (i = die_number + 1; i <= (die_number * 6); i++)
	{
		if (counters_array[i] > max)
			max = counters_array[i];
	}
	return max;
}


// I need to throw several die at once and return the sum of their upward faces but this is not working
int throwDie(int setDieNumber, int throwDice)
{
	int dice1 = 0;
	int dice2 = 0;
	int sum = 0;
	int arr[13] = { 0 };

	for (int throws = 0; throws < 20000; ++throws)
	{
		dice1 = throwDice;
		dice2 = throwDice;
		sum = dice1 + dice2;
		arr[sum]++;
	}

	for (int i = 2; i <= 12; ++i)
	{
		printf(": ", arr[1]);
	}


}

// I need to play a given number of games, and count the number of times each score appears and store the results in the counters array but I can't seem to get it
computeStatistics()
{

}

// Need to show a table with each possible result of throwing the number of die specified and print the number of times each result happened, and the percent of times that score appeared
showStatistics()
{

}

Last edited on
Topic archived. No new replies allowed.