Data set

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.
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
i understand the process in creating the data set,what about the visualization using a histogram?
You should use a graphics library that supports primitives and text to create a histogram.
Last edited on
http://www.cplusplus.com/reference/random/?kw=random

There are also several threads in the forum about similar problems. I suggest you look at those and try to code it yourself. If you run into problems, post your code and ask questions.
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]
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
Got it :) appreciate the help and tips.
thanks alot
main.cpp line 18: No need to call your destructor. It will be called automatically

data.cpp line 71: The bars array is initialized to zeroes, but you don't seem to set it anywhere. This results in hi remaining at 0.

data.cpp line 76: You have a problem in the calculation of length because hi is zero. Therefore, the for loop never executes.

PLEASE LEARN TO USE CODE TAGS CORRECTLY.
http://www.cplusplus.com/articles/jEywvCM9/



Topic archived. No new replies allowed.