Jul 30, 2014 at 7:32pm UTC
I am looking for some help on creating a data set of random positive integer values,the data set should be visualized using a histogram.
Jul 30, 2014 at 7:36pm UTC
You could try using an external library to get the random integers or just create your own by using time.h and a series of algorithms. Or just use <random>. Look under reference
Last edited on Jul 30, 2014 at 7:37pm UTC
Jul 30, 2014 at 7:37pm UTC
i understand the process in creating the data set,what about the visualization using a histogram?
Jul 30, 2014 at 7:41pm UTC
You should use a graphics library that supports primitives and text to create a histogram.
Last edited on Jul 30, 2014 at 7:42pm UTC
Jul 31, 2014 at 7:34pm UTC
below is my code. i am getting a blank output
[main.cpp]
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "data.h"
using namespace std;
int main()
{
srand(time(0));
cout << "CLASS" << endl;
dataClass data(30);
data.makeSet(20,300);
data.displaySet();
data.~dataClass();
return 0;
}
[main.cpp]
[data.h]
#ifndef DATA_H_
#define DATA_H_
const int INVALID_CONST=-2;
class dataClass
{
public:
dataClass();
dataClass(int);
~dataClass();
void makeSet(int,int);
void arrangeSet(int,int);
void displaySet();
private:
int getSet() const;
void setSet(int);
int rangedRandom(int,int);
int *data;
int bars[10];
int xVals;
};
#endif /* DATA_H_ */
[data.h]
[data.cpp]
#include "data.h"
#include <cstdlib>
#include <iostream>
#include <cassert>
using namespace std;
dataClass:: dataClass()
{
this->data = NULL;
}
dataClass::dataClass(int xVals)
{
for(int n=0; n<10; n++)
{
bars[n]=0;
}
setSet(xVals);
this -> data =new int [xVals];
}
dataClass::~dataClass()
{
delete data;
}
int dataClass::rangedRandom(int lo,int hi)
{
return lo + (rand()%(hi-lo+1)) ;
}
int dataClass::getSet() const
{
return xVals;
}
void dataClass::setSet(int xVals)
{
this->xVals=xVals;
}
void dataClass::makeSet(int lo,int hi)
{
if(data ==NULL)
{
cerr << "CONSTRUCTOR ERROR" << endl;
exit(INVALID_CONST);
}
else
{
for(int n=0; n<getSet(); n++)
{
data[n]=rangedRandom(lo,hi);
}
}
}
void dataClass::arrangeSet(int lo,int hi)
{
for(int n=0; n<getSet(); n++)
{
int bar = (data[n]-lo) %10;
bars[bar]+= 1;
}
}
void dataClass::displaySet()
{
int hi=0;
for(int n=0; n<10; n++)
{
if(bars[n]>hi)
{
hi=bars[n];
}
}
for(int n=0; n<10; n++)
{
int length= static_cast<int>((static_cast<double>(data[n])/static_cast<double>(hi))*10);
for(int a=0; a<(length); a++)
{
cout << "*";
}
cout << endl;
}
}
[data.cpp]
Jul 31, 2014 at 8:00pm UTC
The bars array still has 0 values in it when you call displaySet. You have a function arrangeSet that looks like it should populate bars, but it isn't getting called.
1 2 3 4 5 6 7 8 9 10
void dataClass::displaySet()
{
int hi=0;
for (int n=0; n<10; n++)
{
if (bars[n]>hi)
{
hi=bars[n];
}
}
Edit: If you edit your post, highlight the code and click the <> button in the format palette at the right, it'll format your code and make it easier to read on the forum.
Last edited on Jul 31, 2014 at 8:00pm UTC
Jul 31, 2014 at 8:05pm UTC
Got it :) appreciate the help and tips.
thanks alot