I know this problem is easy, but I can't figure it out by myself. I'm trying to generate 100 random numbers and then plot a histogram. For some reason, when I'm trying to run it, my Linux terminal only gives me a "Segmentation error" message.
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
srand((unsigned)time(0));
int j, k, index;
int random_integer[index];
double number[index];
int bin[j];
std::cout.precision(4);
std::cout.width(4);
for(int index=0; index<100; index++) {
random_integer[index] = (rand()%1000) + 1;
number[index] = random_integer[index] / 1000;
cout << random_integer << number << endl; // generate 100 random numbers
for (j=0; j<100; j++)
{ if ((random_integer[j] >= 0.01*j) && (random_integer[j] < 0.01*j+0.01) ) {bin[j]++;}
}; // put numbers into bins, by their values
for (j=0; j<100; j++)
{cout << j*0.01 << "-" << 0.01*j+0.009 << ": ";
for (k=0; k<bin[j]; k++) {cout << "*"; }
cout << "\n"; // create a histogram from *s
By any chance, do you know how to fix it? Or, in general, how to solve segfaults? Thank you very much in advance!
To what value is index initialized when you declare your array of integers for random_integer? Try setting index = 100 before you declare random_integer.