The main program reads the name of the output file from screen and sets up the arrays containing the walker's position after a given number of steps. The corresponding program for a two-dimensional random walk (not listed in the main text) is found under programs/chapter12/program2.cpp
/*
1-dim random walk program.
A walker makes several trials steps with
a given number of walks per trial
*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include "lib.h"
using namespace std;
// Function to read in data from screen, note call by reference
void initialise(int&, int&, double&) ;
// The Mc sampling for random walks
void mc_sampling(int, int, double, int *, int *);
// prints to screen the results of the calculations
void output(int, int, int *, int *);
int main()
{
int max_trials, number_walks;
double move_probability;
// Read in data
initialise(max_trials, number_walks, move_probability) ;
int *walk_cumulative = new int [number_walks+1];
int *walk2_cumulative = new int [number_walks+1];
for (int walks = 1; walks <= number_walks; walks++){
walk_cumulative[walks] = walk2_cumulative[walks] = 0;
} // end initialization of vectors
// Do the mc sampling
mc_sampling(max_trials, number_walks, move_probability,
walk_cumulative, walk2_cumulative);
// Print out results
output(max_trials, number_walks, walk_cumulative,
walk2_cumulative);
delete [] walk_cumulative; // free memory
delete [] walk2_cumulative;
return 0;
} // end main function
\end{lstlisting}
The input and output functions are
\begin{lstlisting}
void initialise(int& max_trials, int& number_walks, double& move_probability)
{
cout << "Number of Monte Carlo trials =";
cin >> max_trials;
cout << "Number of attempted walks=";
cin >> number_walks;
cout << "Move probability=";
cin >> move_probability;
} // end of function initialise