help please! stuck on a program

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.

Here is what I have so far:

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
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;

int main()
{
	int face1 = 0;
	int face2 = 0;
	int face3 = 0;
	int face4 = 0;
	int face5 = 0;
	int face6 = 0;
	int numRoll = 0;
	int diceFace = 0;

	for (numRoll = 1; numRoll <= 6000; numRoll++)
	{
		diceFace = ((rand() % 6) + 1);

		switch (diceFace)
		{
		case 1:
			++face1;
			break;
		case 2:
			++face2;
			break;
		case 3:
			++face3;
			break;
		case 4:
			++face4;
			break;
		case 5:
			++face5;
			break;
		case 6:
			++face6;
			break;
		}
	}
	cout << "1 ----- " << face1 << endl;
	cout << "2 ----- " << face2 << endl;
	cout << "3 ----- " << face3 << endl;
	cout << "4 ----- " << face4 << endl;
	cout << "5 ----- " << face5 << endl;
	cout << "6 ----- " << face6 << endl;

	const int NUM_FACES = 6;
	vector<int> dieShows(NUM_FACES);

	
}


It prints the number of occurrences correctly, But that is as far as I have gotten in my histogram attempt.
closed account (48T7M4Gy)
So what's the problem? Maybe you could print an astejavascript:editbox1.editSend()risk for every 50 counts on each face?
I am also supposed to print a histogram in the format:

1 XXXXXXXXXXX
2 XXXXXXXXXX
3 XXXXXXXXXXXXX
4 XXXXXXXXXXXXXX
5 XXXXXXXXX
6 XXXXXXXXXXXX

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.
closed account (48T7M4Gy)
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.

BTW your vector is doing zip. :)


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?
Last edited on
closed account (48T7M4Gy)
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
 

const int 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.
closed account (48T7M4Gy)
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/
Last edited on
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:

1 ----- 1000
2 ----- 1030
etc.
closed account (48T7M4Gy)
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
Topic archived. No new replies allowed.