frequencies in vectors

I am writing a dice game that rolls a dice randomly according to a users input of 1-10000 going into srand. I got through the part where I got the output of the 5 dice numbers 1-6 rolled, but now I want to count the frequencies of each number. For example:
The rolled dice: [3] [3] [1] [1] [3]
The frequencies: 1-> 2 2-> 0 3-> 3 4-> 0 5-> 0 6-> 0
I'm guessing I would use an if statement or for loop to do this. If anyone can help me figure this part out that would greatly appreciated.
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
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<vector>

using namespace std;

void fiveDice(vector<int> & v);
void printDice(vector<int> v);

int main()
{
    int x;
    
    cout << "Enter in an integer between 1-10000: ";
    cin >> x;
    
    srand(x);
    
    vector<int> first(5);
    
    fiveDice(first);
    printDice(first);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


void fiveDice(vector<int> & v)
{
     for(int i=0; i<v.size(); i++)
     {
         v[i] = 1+rand()%6;
     }
}
void printDice(vector<int> v)
{
     vector<int> first(5);
     for(int i=0; i<v.size(); i++)
     {
         cout<<v[i]<<" ";
     }
     cout<<endl;    
}
keep a map, or a vector with six elements in. when you get a number increment the appropriate element by one.
So would I make a separate function that counts each number? I think I could do this using an if statement but I'm not sure exactly how to set it up.

Something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void counter(vector<int> & v)
{
     for(int i=0; i<v.size(); i++)
     {
         if(v[i] == 6){
//I dont know what to put here
}
 if(v[i] == 5){
//I dont know what to put here
}
 if(v[i] == 4){
//I dont know what to put here
}
 if(v[i] == 3){
//I dont know what to put here
}
 if(v[i] == 2){
//I dont know what to put here
}
 if(v[i] == 1){
//I dont know what to put here
}
}


And then how would I output the count of each one after that?
Last edited on
To be honest, I would just do it with an array rather than std::map or std::vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
int array[6] = {0}; //Set all array elements to 0. 

while(n < whatever_number)
{
//Roll di here 

if(v[i] == 1)
{
    ++array[0];
}

etc...
}


Also, if you want a far better random distribution, I recommend using <random> and a mersenne twister.

http://www.cplusplus.com/reference/random/
Last edited on
I would be more inclined to organize the code as in the following, but I'm sure you can adapt this to fit yours.

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

void seedRNG();
int rollDie();
void rollAndReport();

int main()
{
    seedRNG();
    rollAndReport();
}

void rollAndReport()
{
    std::vector<unsigned> frequency(7); // 0-6 are valid indexes.

    std::cout << "The rolled dice: ";
    for (unsigned i = 0; i < 5; ++i)
    {
        int roll = rollDie();
        std::cout << '[' << roll << "] ";
        ++frequency[roll];
    }

    std::cout << "\nThe frequencies: ";

    for (unsigned i = 1; i < frequency.size(); ++i)
        std::cout << i << " -> " << frequency[i] << "  ";
    std::cout << '\n';
}

void seedRNG()
{
    int seed;
    std::cout << "Enter an integer between 1 and 10000: ";
    std::cin >> seed;
    srand(seed);
}

int rollDie()
{
    return (rand() % 6) + 1;
}
cire, Thank you. Your code makes much more sense than mine.

However, what is the purpose of using "std::" compared to not using it?
std:: just tells you where it comes from. He's explicitly stating that he's using a function from the standard library. Otherwise, you might see cout and wonder "what the heck is that?".
Topic archived. No new replies allowed.