jeudi 10 octobre 2019

Printing Integer Contents of a Vector as a String are Leading to Segmentation Fault

I'm trying to print my normal distribution by using consecutive '*' characters to reflect the bars of the normal distribution. The program crashes often before it is printed, but sometimes after. Any idea what is going on?

int main() {
std::normal_distribution<double> normDist(10.0, 4.0); 
    vector<int> v(20); // All zeros.
    for (int i{}; i < 500; i++) {
        int num = normDist(eng3);
        if (num >= 0 && num < 20);
            v[num]++;
    }

    for (int i{}; i < 20; i++) {
        cout << i << ": " << std::string(v[i], '*') << endl;
    }
}

Results:

0: *****
1: ***
2: *******
3: *******
4: ***********************
5: *********************
6: **********************************
7: **********************************
8: ********************************************
9: ********************************************
10: ****************************************************
11: **********************************************
12: *****************************************
13: **************************************************
14: ************************************
15: *******************
16: ************
17: ********
18: *******
19: **
double free or corruption (out)
Aborted (core dumped)

Thanks for your help.




Aucun commentaire:

Enregistrer un commentaire