random walk problem

Hi all,

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;

int binNumber = 0;
for(int outerLoop = 0; outerLoop < matrixSize - binSize; outerLoop += binSize)
{
for(int innerLoop = 0; innerLoop < binSize; innerLoop++)
smallHistogram[binNumber] = smallHistogram[binNumber] + histogram[binNumber * binSize + innerLoop];
binNumber++;
}
qplbar(smallHistogram, smallMatrixSize); //bar graph of histogram
}
}









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
52
53
54
55
56
57
//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;

int binNumber = 0;
for(int outerLoop = 0; outerLoop < matrixSize - binSize; outerLoop += binSize)
{
for(int innerLoop = 0; innerLoop < binSize; innerLoop++)
smallHistogram[binNumber] = smallHistogram[binNumber] + histogram[binNumber * binSize + innerLoop];
binNumber++;
}
qplbar(smallHistogram, smallMatrixSize); //bar graph of histogram
}
}





USE THE CODE TAGS!
Don't know how. I only copy my source code and save as .rtf, then paste here, sorry.
It's kind of useless without indentation ;)
Change line 42 by const int smallMatrixSize = matrixSize/binSize;

It seems that the compiler fails to realise that the operation will give a constant result...
Topic archived. No new replies allowed.