Dice Roll problem using arrays?

So I managed to get this simple code to work. Rolling a dice 6000 times and then recording and displaying how many times each number was rolled.

Now I'm trying to get this to work using an array instead.

I know I could start out with declaring an array like

int frequency[6] // to store the frequency of how many times a number was rolled

But I'm not to sure how to implement this in a loop and then output the results at the end.

Any help would be nice.

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
#include <iostream>
#include <cstdlib>
#include <ctime>

int main ()

{
    int random_num;
    std::srand(std::time(0));
    int roll1 = 0;
    int roll2 = 0;
    int roll3 = 0;
    int roll4 = 0;
    int roll5 = 0;
    int roll6 = 0;
    int i;

    for (int i=0; i < 6000; i++){
            int random_num = std::rand() % 6 + 1;


    if (random_num == 1)
        roll1 = roll1 + 1;
    else if (random_num == 2)
        roll2 = roll2 + 1;
    else if (random_num == 3)
        roll3 = roll3 + 1;
    else if (random_num == 4)
        roll4 = roll4 +1;
    else if (random_num == 5)
        roll5 = roll5 + 1;
    else if (random_num == 6)
        roll6 = roll6 + 1;
    }



    std::cout << "1 was rolled: " << roll1 << " times " << std::endl;
    std::cout << "2 was rolled: " << roll2 << " times " << std::endl;
    std::cout << "3 was rolled: " << roll3 << " times " << std::endl;
    std::cout << "4 was rolled: " << roll4 << " times " << std::endl;
    std::cout << "5 was rolled: " << roll5 << " times " << std::endl;
    std::cout << "6 was rolled: " << roll6 << " times " << std::endl;



    return 0;
}
@Chedderlord

Here is one way, though I'm sure there are more elegant ways of doing it. I commented out some ( most ) of your code, to better compare your code to the corrected code.

Hope this helps you out.

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
// Dice Rolls.cpp : main project file.

#include <iostream>
#include <cstdlib>
#include <ctime>

int main ()
{
    int random_num, roll_ck=0;
    srand((unsigned) time(0));
	int roll[6] = {0};// Declare and initialize to 0
	/*
    int roll1 = 0;
    int roll2 = 0;
    int roll3 = 0;
    int roll4 = 0;
    int roll5 = 0;
    int roll6 = 0;
    int i;
	*/
    for (int i=0; i < 6000; i++)
	{
          random_num = 1+ rand()% 6;
	  roll[random_num-1]++; // Since numbers are 1 to 6, reduce by one to increase correct 0 to 5 array
	}

	for (int i=0;i<6;i++)
	{
	  std::cout << "\tA " << i+1 << " was rolled " << roll[i] << " times." << std::endl;
	  roll_ck+=roll[i];
	}
	std::cout << "Rolls = " << roll_ck << std::endl; // To verify amount of dice rolls
	/*
    if (random_num == 1)
        roll1 = roll1 + 1;
    else if (random_num == 2)
        roll2 = roll2 + 1;
    else if (random_num == 3)
        roll3 = roll3 + 1;
    else if (random_num == 4)
        roll4 = roll4 +1;
    else if (random_num == 5)
        roll5 = roll5 + 1;
    else if (random_num == 6)
        roll6 = roll6 + 1;
    }
	std::cout << "1 was rolled: " << roll1 << " times " << std::endl;
    std::cout << "2 was rolled: " << roll2 << " times " << std::endl;
    std::cout << "3 was rolled: " << roll3 << " times " << std::endl;
    std::cout << "4 was rolled: " << roll4 << " times " << std::endl;
    std::cout << "5 was rolled: " << roll5 << " times " << std::endl;
    std::cout << "6 was rolled: " << roll6 << " times " << std::endl;

	*/

    return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    srand(time(0));
    int frequency[6]={0};
    for(int i=0;i<6000;i++)
        frequency[rand()%6]++;
    for(int i=0;i<6;i++)
        cout<<i+1<<" was rolled "<<frequency[i]<<" times."<<endl;
return 0;
}
Topic archived. No new replies allowed.