In: Computer Science
I have not tested it completely due to some technical issues but it should work:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int N, totalMoves;
float averageMoves, averageHome;
int totalHome=0;
int totalPub=0;
int x, probHome;
int Prob(int &probHome);
int StartPos(int &N, int &x, void Walk(int x, int probHome,
int &totalMoves, int &totalHome, int &totalPub, int
Prob(int &probHome), void Print(float &averageMoves, float
&fAveragHome, int totalMoves, int totalHome)));
void Walk(int probHome, int x, int &totalMoves, int
&totalHome, int &totalPub, float &averageMoves, float
&averageHome, int Prob(int &probHome),
void Print(float &averageMoves, float &averageHome, int
totalMoves, int totalHome));
void Print(float &averageMoves, float &averageHome, int
totalMoves, int totalHome);
srand(time(0));
StartPos(N, x, Walk(N, probHome, x, totalMoves, totalHome,
totalPub, averageMoves, averageHome,
Prob(probHome)));
return 0;
}
int StartPos(int &N, int &x, void Walk(int x, int probHome,
int &totalMoves, int &totalHome,
int &totalPub, int Prob(int &probHome), void Print(float
&averageMoves, float &averageHome,
int totalMoves, int totalHome)))
{
cin >> N;
x=N;
if(2<=N&& N<=7)
{
Walk(x, probHome, totalMoves, totalHome, totalPub, averageMoves,
averageHome, Prob(probHome),
Print(averageMoves, averageHome, totalMoves, totalHome) );
}
return N;
}
int Prob(int &probHome)
{
probHome = rand()%6;
cout<< probHome<< endl;
return probHome;
}
void Walk(int probHome, int x, int &totalMoves, int
&totalHome, int &totalPub,
int Prob(int &probHome), void Print(float &averageMoves,
float &averageHome,
int totalMoves, int totalHome))
{
int T;
T=1000000;
do
{
Prob(probHome);
if(probHome == 1 || probHome == 2 || probHome == 3 || probHome
==5)
{
x=x+1;
totalMoves++;
}
if(probHome == 0 || probHome == 4)
{
x=x-1;
totalMoves++;
}
if(x==1)
{
totalHome++;
}
if(x==8)
{
totalPub++;
}
T--;
}while(T>= 0);
Print(averageMoves, averageHome, totalMoves, totalHome);
}
void Print(float &averageMoves, float &averageHome, int
totalMoves, int totalHome)
{
averageMoves= totalMoves/1000000;
averageHome = totalHome/1000000;
cout << "I = " << N << endl;
cout << "Average Moves = " << averageMoves <<
endl;
cout << "Total Moves = " << totalMoves <<
endl;
cout << "AverageHome = " << averageHome <<
endl;
cout << "TotalHome = " << totalHome <<
endl;
return;
}