I am going through all the assignments in my stupid book from D. Yewick which I will throw away I think. Just (I know "just"...) wanted to do the random walk problem which I find interesting. To be honest I don't understand the program in full but the main problem is that upon compiling I get the message "error: variable sized object 'small histogram' may not be initialized". It's the variable which is introduced in line 43 (the last few code lines). I know you guys have better things to do, but if someone has a idea or two, I would be very grateful (just look over the stuff with the .h and other bad/forbidden things; the dislin.h header is a graphics package which the author included on the book's CD (this works, already checked with a different code than this one)).
//Random Walk Simulator, scatter plot or bar graph
#include<iostream>
#include "dislin.h"
#include<stdlib.h>
#include<math.h>
#include<time.h>
using namespace std;
const int matrixSize = 600;
int main ()
{
float histogram[matrixSize] = {0}; //set histogram values to zero
float gridPositions[matrixSize]; // number of grid points
int numberOfRealizations = 10000; //number of Monte-Carlo steps
int numberOfSteps = matrixSize; //number of steps per run
int offset = matrixSize / 2; //starting point
srand(time(NULL)); //initialize random number generator
for (int loop; loop < matrixSize; loop++)
gridPositions[loop] = loop - offset; //grid points
for(int outerLoop = 0; outerLoop < numberOfRealizations; outerLoop++)
{
double position = offset; //starting point
for(int innerLoop = 0; innerLoop < numberOfSteps; innerLoop++)
{
position = position + 4.0 * (rand()/double(RAND_MAX) - 0.5);
histogram[int(position)] += 1;
}
}
metafl("XWIN");
int iPoints = 0; // 1: scatter plot, 0 or other value: bar plot
if (iPoints == 1)
{
qplsca(gridPositions, histogram, matrixSize); //plots symbols at data values
}
else
{
const int binSize = 20; //number of histogram values in each bin
const int smallMatrixSize = float(matrixSize) / binSize; //number of bars in plot
float smallHistogram[smallMatrixSize] = {0};
cout << smallMatrixSize << endl;