I am new to C++ programming and I am still trying to grasp the concept of vectors, but my current project requires me to use the vector<> function to create a histogram for a dice roll(rng) program and that is where I have gotten stuck. Any help would be greatly appreciated... PS. I will probably ask a lot of questions if I still do not quite understand the help.
there should be an x for every 60 occurences. I have to do it without using if/else, so i am assuming that I have to use a for loop but I am not sure how to set it up so that it prints in that format.
Try using http://www.cplusplus.com/reference/string/string/assign/
I'll give you a clue:
- declare a string str, then str.assign(faceX, '?')
- don't forget #??? at the top
- you'll have to play around with faceX otherwise you'll display about a thousand ?'s for each face.
-you won't need any if's etc etc just cout << and your string for each face.
OKay I think I understand where you are going with the str.assign, once I get a value for faceX would i just use the modulo or division operator to cut down my number of ?'s for each face?
something like:
face1 = face1 % 60;
then str.assign(face1, '?');
and I think I am supposed to have used the vector function instead of the switch function when finding the number of occurrences, the instructions were a little unclear. Is there a way to do that?
the modulo or division operator to cut down my number of ?'s for each face?
You could do that or, because the number of occurrences will be nearly the same (ideally 1/6th) you might just take off 900 or so. Up to you.
and I think I am supposed to have used the vector function instead of the switch function when finding the number of occurrences, the instructions were a little unclear. Is there a way to do that?
I thought that might be the case. You can also use an array, one element for each face. This, whatever you use wil make your code much simpler. You refer to face[i], i = 1 to 6, instead of face1, face2, etc etc. Keep in mind if you use a vector, which is better than array, you must declare it before you use it.
so to declare a vector I would go about it like this: ?
1 2 3 4 5 6
constint NUM_FACES = 6;
int face;
vector<int> face(NUM_FACES);
if so, how would I go about looping it so that it generates random numbers for each face?
do i create a for loop? for (i = 1; i < NUM_FACES; i++) {}
am I even in the ball park as to what I need to do?
I do apologize if it seems like I am trying to get you to do the work for me, I really am trying to understand and figure it out myself with the tips you are providing.
I don't mind helping, You're on the right track and above all write just a few lines each time and test it before moving on. There's nothing worse than trying to fathom out more than a few lines of code.
Best is to read the tutorials and this might help.
http://www.cplusplus.com/reference/vector/vector/vector/
http://www.cplusplus.com/reference/vector/vector/insert/
How would I proceed from the for statement?
Like to get results for 6000 rolls and then have them printed like they would have been with the switch statement I was previously using. In this format:
You know that for 6000 rolls that each face should appear 1000 times so maybe just subtract say 950 from each face count. (you can't have -ve asterisks.
From there something like str.assign(face[i] - 950, '*') might be useful