Homework Help

I was suppose to use a vector instead of an array. I got points taken off because of this and have to fix it since out last program is just a continuation of this.

I'm not sure how to use a vector instead... Any sort of guidance at this point will help.

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
#ifndef aHistogram_h
#define aHistogram_h

class aHistogram {
public:
	aHistogram();
	~aHistogram();
	void update(int face);
	void display(int maxLengthOfLine);
	int count(int face) const;
	void clear();

private:
	int NumOfXs[6];	//Array with 6 elements (faces of the die)
	int max;
};

aHistogram::aHistogram() {
	//Default constructor
	for (int i = 0; i < 6; i++) {
		NumOfXs[i] = 0;	//Sets each element of the array to zero.
		max = 0;
	}
}

aHistogram::~aHistogram() {
	//Destructor
}

void aHistogram::update(int face) {
	NumOfXs[face - 1]++;
	if (NumOfXs[face - 1] > max) {
		max = NumOfXs[face - 1];
	}
}

void aHistogram::display(int maxLengthOfLine) {
	for (int i = 0; i < 6; i++) {
		cout << (i + 1) << ": " << NumOfXs[i] << " ";

		//maxLengthOfLine is divided by the max faces
		for (int k = 0; k < (NumOfXs[i] * maxLengthOfLine / max); k++) {
			cout << 'X';
		}
		cout << endl;
	}
}

int aHistogram::count(int face) const {
	//Counts the number of occurances
	return NumOfXs[face - 1];
}

void aHistogram::clear() {
	//Clears each element of the array.
	for (int i = 0; i < 6; i++) {
		NumOfXs[i] = 0;
	}
}

#endif 


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

using namespace std;

#include "aDie.h"
#include "aHistogram.h"

int main() {
	int seed_number;	// User inputs Seed Number
	char accept = '?';

	cout << "Enter a value to seed random number generator: ";
	cin >> seed_number;
	cout << endl;

	if (seed_number >= 1){	//Makes sure the user inputs an integer.

		cout << "Do you accept this input? (Y/N): ";
		cin >> accept;
		cout << endl;

			if (accept == 'Y' || accept == 'y') {
				srand(seed_number);	//If user accpets seed number it will be used.
			}
			else {
				srand(time(NULL));	//A random seed number will be 
									//generated if user does not want theirs.
			};
	
		aDie die;
		aHistogram histogram;
	
		for (int i = 0; i < 12000; i++) {	
			//The Die will be rolled 12,000 times.
			int rolled_value = die.roll();
			histogram.update(rolled_value);
		}
		histogram.display(60);	//A max of 60 X's will be printed 
	
		cout << endl;
	}

	else {
		cout << "Invalid input. Try again." << endl << endl;
	};

	system("pause");	//Pauses program to view results
	
	return 0;

}
Last edited on
First, you have to get the definition of vector before you use it, so in your header, do:

#include <vector>

Then just use vector,

std::vector<int> NumOfXs;

Check out this site for all the operations a vector can do. For example, instead of looping through, assigning 0 to each element, you can just do
NumOfXs.clear();

In general, it grows in size dynamically, and you can still use "[]" to access elements just like an array, but this is not the best approach.

For your needs, I thnk you need to find out about vector::push_back and vector::resize at least.
Topic archived. No new replies allowed.