Class Implementation
Dec 5, 2014 at 9:49pm UTC
Just looking to be pointed in the right direction here, I cannot get my update function to work correctly. It builds but I cannot get it to get the results to the display function for output. Any guidance would be greatly appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#ifndef ADIE_H
#define ADIE_H
#include <iostream>
class aDie
{
public :
aDie(); // default constructor
void Roll();
private :
int die;
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "aDie.h"
using namespace std;
aDie::aDie()
{
die = 0;
return ;
} //default constructor
void aDie::Roll() //generating a random number from 1-6
{
die = 0;
die = ((rand() % 6) + 1);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#ifndef AHISTOGRAM_H
#define AHISTOGRAM_H
#include <iostream>
#include <vector>
using namespace std;
class aHistogram
{
public :
aHistogram(); // default constructor
void update(int face); // update histogram counts
void display(int maxLengthOfLine); // displays histogram
void clear(); // clears counts
vector<int > graph;
private :
int face;
int maxLengthOfLine;
};
#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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "aDie.h"
#include "aHistogram.h"
using namespace std;
aHistogram::aHistogram()
{
face = ((rand() % 6) + 1);
maxLengthOfLine = 60;
graph.resize(6)
}
void aHistogram::update(int face) // updates roll count
{
this -> face = graph.at(face++);
}
void aHistogram::display(int maxLengthOfLine)
{
int i;
cout << "Face Appearances" << endl; // displays the appearances of each face
cout << endl;
for (i = 0; i < graph.size(); ++i)
{
cout << i + 1 << "---" << graph.at(i) << endl;
}
cout << endl;
cout << "Histogram" << endl; // displays the histogram count
cout << endl;
for (i = 0; i < graph.size(); ++i)
{
cout << i + 1 << " - " ;
while (graph.at(i) > maxLengthOfLine)
{
cout << "X" ;
graph.at(i) -= maxLengthOfLine;
}
cout << endl;
}
}
void aHistogram::clear() // clears the counts for the appearances of each face
{
face = 0;
maxLengthOfLine = 0;
}
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "aDie.h"
#include "aHistogram.h"
using namespace std;
int main()
{
int i;
srand(time(0)); // seeding RNG
aDie Die1;
aHistogram Die2;
for (i = 0; i < 12000; ++i)
{
Die1.Roll();
}
Die2.update(6);
Die2.display(60);
cin.ignore();
return 0;
}
Dec 5, 2014 at 10:11pm UTC
main.cpp line 19: You roll Die1 12000 times. Hows does this update your histogram?
Dec 5, 2014 at 10:27pm UTC
That is a good point there, I cant call the roll function for Die2 though since it is not a part of the ahistogram class. would I have ahistogram inherit aDie and call it that way?
Dec 5, 2014 at 11:59pm UTC
okay I changed it to Die2.Roll();
and it is still not update the histogram counts.
Dec 6, 2014 at 1:57am UTC
You don't simply roll a dice. You need to look the result and annotate it.
1 2 3 4
void aHistogram::update(int face)
{
this -> face = graph.at(face++); //¿what do you think this line is doing?
}
Dec 6, 2014 at 2:04am UTC
I think I may have realized my mistake there, it is now:
1 2 3 4
void aHistogram::update(int face) // updates roll count
{
this ->face++; //increments the appearance of each face
}
Dec 6, 2014 at 2:07am UTC
Topic archived. No new replies allowed.