Vector/Array question

Hi! I have a question about vectors. I am lead to believe that vectors can be resized/added to etc 'on the fly'
What I mean is when you use an array you have to hard code the size of it before you can use it - right?
If you see my code below we had to do a dice simulation using vectors, but I have hardcoded the size.
My question is how do I use vectors without hard coding the size as I have done?

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
  // 7.37 (Dice Rolling) Write a program that simulates the rolling of two dice. The program
// should use rand to roll the first die and should use rand again to roll the second die.
// The sum of the two values should then be calculated. [Note: Each die can show an integer
// value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the
// most frequent sum and 2 and 12 being the least frequent sums.] Figure 7.26 shows the 36
// possible combinations of the two dice. Your pro- gram should roll the
// two dice 36,000 times. Use a one-dimensional vector to tally the numbers of times each
// possible sum appears. Print the results in a tabular format. Also, determine if the totals
// are reasonable (i.e., there are six ways to roll a 7, so approximately
// one-sixth of all the rolls should be 7).

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


int main()
{
    srand( static_cast<unsigned int>( time ( NULL ) ) );//randomise seed
    
    
    const int arraySize = 13;//size of vector to store dice throws
    std::vector <int> sumOfDiceRolls(arraySize, 0);//vector to store data
    
    int firstDice = 0;
    int secondDice = 0;
    int sumOfDice = 0;
    int sum = 0;
    
    for( int i = 0; i < 36000; i++ )//loop to simulate dice throws and sum, increement sum
    {
        firstDice = 1 + rand() % 6;
        secondDice = 1 + rand() % 6;
        sumOfDice = firstDice + secondDice;
        sumOfDiceRolls.at(sumOfDice) += 1;
    }
    
    // shows possible numbers that can be thrown from 2 dice
    std::cout << "Numbers thrown from 2 dice\n";
    for ( int i = 2; i < arraySize; i++ )
    {
        std::cout << i << "\t\t";
    }
    
    std::cout << std::endl;
    
    
    //show amount of times each combinationof numbers thrown
    for ( int i = 2; i < arraySize; i++ )
    {
        std::cout << sumOfDiceRolls[i] << "\t";
        sum += sumOfDiceRolls[i];
        
    }
    
    //find which was the most thrown number of the two dice
    int mostThrown = 0, mostNumber = 0;
    for (int i = 2; i < arraySize; i++ ) {
        if (sumOfDiceRolls.at(i) > mostThrown) {
            mostThrown = sumOfDiceRolls.at(i);
            mostNumber = i;
        }
    }
    
    std::cout << "\nThe most thrown number was: " << mostNumber << " with " << mostThrown << " throws" << std::endl;
    std::cout << "sum of rolls is: " << sum << std::endl;
    
    return 0;
    
}

Everytime you want to add something to the vector, you simple use it's function push_back, you don't need to hardcode a size for it.

http://www.cplusplus.com/reference/vector/vector/push_back/
Hi - thanks! I looked at that but I'm not sure how to use it.
If you look at the first for loop, the dice is thrown 36000 times. The sum of the dice ( 2 - 12 ) is a specific element in the vector which is incremented each time that value is thrown.
I would like to specificy the vector then incremet each element to count the throws...
How would I be able to use push_back?
What is wrong with how you implemented the vector in the first post? For this application that is probably the correct way to go, but remember unlike arrays the variable used to size the vector doesn't need to be a constant.

Last edited on
Hi jib

As far as I know I don't think there is a problem with the way i have done it, but I'm new to c++! I was just wanting to play with vectors and try and get a better understanding of them. Is it fair to say that you can stop using arrays altogether and sue vectors instead?
I was just wanting to play with vectors and try and get a better understanding of them.

Experimenting is a good idea, however you'll probably want to experiment on another aspect of the assignment. For example you could experiment on storing all the dice rolls to a vector and then printing out the vector contents. You may want to reduce the number of rolls or possibly print the results to a file.

Is it fair to say that you can stop using arrays altogether and sue vectors instead?

Yes, you can use std::vector or std::array instead of raw arrays for a lot of things, but remember that there are other containers that may be better suited for the task.
As jlb said, it is good to experiment in order to learn. However, in this instance an array is perfectly acceptable for this application.

Is it fair to say that you can stop using arrays altogether and use vectors instead?

I'm going to disagree with jlb here. A vector is not always the best choice to replace a simple array. If you know in advance the number of elements and the the number of elements is not going to change, a simple array, or a std::array may be a more obvious choice.

Your choice of vector's fill constructor vector<int>(13,0) does eliminate the need to manually initialize an array. std::array does have a fill function, but no fill constructor.


Although the std::array<> doesn't have a fill constructor you can still initialize the array using an initializer list much like the raw array, or std::vector.
1
2
3
4
5
6
7
8
9
10
#include <array>
#include <iostream>

int main()
{
    std::array<int, 10> my_array{1,2,3};
    for(auto& itr : my_array)
        std::cout << itr << ", ";
    return(0);
}


Also although they do have there place in the language I recommend staying away from raw arrays as much as possible. See the following link for some reasons. I also tend to favor std::vector as my container of first choice.

http://www.stroustrup.com/bs_faq2.html#arrays

Topic archived. No new replies allowed.