Question

In: Computer Science

------------------------------------------------------------------------- The classic drunkard's walk problem: Over an eight block line, the home of an intoxicated...

-------------------------------------------------------------------------

The classic drunkard's walk problem: Over an eight block line,
the home of an intoxicated chap is at block 8, and a pub is at block 1.
Our poor friend starts at block n, 2 <= n <= 7, and wanders at random,
one block at a time, either toward or away from home. At any intersection,
he moves toward the pub with a certain probability, say 2/3, and towards
home with a certain probability, say 1/3. Having gotten either home or to
the pub, he remains there. Write a program to simulate 1000000 trips in which
he starts at block 2, another 1000000 in which he starts at block 3, and so
forth up through block 7. For each starting point, calculate and print the
proportion of the time he ends up at home and the average number of blocks
he walked on each trip.

For the random number generation...

Use the 'srand' function to seed the random number generator.
Use the 'rand' function (and mod as needed) to get random probabilities
for a given step.


************************************************************************
* *
* To do each single walk, call a function that runs through a loop *
* until the walk is done. *
* *
************************************************************************



-------------------------------------------------------------------------


A sample run might look something like...

lab6

I = 2
TotalMoves = 2834110
Avg moves = 2.834110
total Home = 7820
avg home = 0.0078200


I = 3
TotalMoves = 5503639
Avg moves = 5.503639
total Home = 23621
avg home = 0.0236210


I = 4
TotalMoves = 7830959
Avg moves = 7.830959
total Home = 54851
avg home = 0.0548510


I = 5
TotalMoves = 9519658
Avg moves = 9.519658
total Home = 118166
avg home = 0.1181660


I = 6
TotalMoves = 9875095
Avg moves = 9.875095
total Home = 243413
avg home = 0.2434130


I = 7
TotalMoves = 7581532
Avg moves = 7.581532
total Home = 496420
avg home = 0.4964200




-------------------------------------------------------------------------
Please write the program in computer and test it, and copy the program and poste it in the answer
Thank you

Solutions

Expert Solution

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;
}


Related Solutions

Film /. Walk the Line (2005) Walk the Line, starring Reese Witherspoon and Joaquin Phoenix and...
Film /. Walk the Line (2005) Walk the Line, starring Reese Witherspoon and Joaquin Phoenix and centering on life and times of Johnny Cash. 1) Provide a brief synopsis of the film? 2) Describe Johnny Cash lifestyle as depicted in the film? 3) Address the rockabilly style of Cash music in regards to lyrical content, rhythm, instrumentation, harmony, and melody?
Block A is connected to block B by a string that goes over an ideal pulley...
Block A is connected to block B by a string that goes over an ideal pulley as shown in the figure. Block A has a mass of 5.00 kg and can slide over a rough plane inclined 30.0° to the horizontal. The coefficient of kinetic friction between block A and the plane is 0.400. Block B has a mass of 3.77 kg. (a) Draw the free body diagram (b) What is the reaction of the surface on block A? (c)...
Assigned Problem: On New Year's Eve, the probability of a person driving while intoxicated or having...
Assigned Problem: On New Year's Eve, the probability of a person driving while intoxicated or having a driving accident is 0.38. If the probability of driving while intoxicated is 0.31 and the probability of having a driving accident is 0.11, find the probability of a person having a driving accident while intoxicated. show work.
**SHOW ALL WORK IN EXCEL QM** Problem-4: Every home football game for the past eight years...
**SHOW ALL WORK IN EXCEL QM** Problem-4: Every home football game for the past eight years at Eastern State University has been sold out. The revenues from ticket sales are significant, but the sale of food, beverages, and souvenirs has contributed greatly to the overall profitability of the football program. One particular souvenir is the football program for each game. The number of programs sold at each game is described by the following probability distribution: Number (in 100s) of Programs...
A study of the ability of individuals to walk in a straight line reported the accompanying...
A study of the ability of individuals to walk in a straight line reported the accompanying data on cadence (strides per second) for a sample of n = 20 randomly selected healthy men. 0.95 0.85 0.92 0.95 0.93 0.87 1.00 0.92 0.85 0.81 0.76 0.93 0.93 1.03 0.93 1.06 1.08 0.96 0.81 0.95 A normal probability plot gives substantial support to the assumption that the population distribution of cadence is approximately normal. A descriptive summary of the data from Minitab...
A study of the ability of individuals to walk in a straight line reported the accompanying...
A study of the ability of individuals to walk in a straight line reported the accompanying data on cadence (strides per second) for a sample of n = 20 randomly selected healthy men. 0.95 0.85 0.92 0.95 0.93 0.87 1.00 0.92 0.85 0.81 0.76 0.93 0.93 1.03 0.93 1.06 1.08 0.96 0.81 0.95 A normal probability plot gives substantial support to the assumption that the population distribution of cadence is approximately normal. A descriptive summary of the data from Minitab...
What evidence is there to show that Asch’s classic studies on line judgement are experiments on...
What evidence is there to show that Asch’s classic studies on line judgement are experiments on conformity?
The following grammar for a program has a problem with semicolons: Program ::= Block "." Block...
The following grammar for a program has a problem with semicolons: Program ::= Block "." Block ::= "{" Statement (";" Statement )* "}" Statement ::= Assignment | IfStatement | Block Assignment ::= "=" IfStatement ::= "if" "(" Assignment ")" Statement Use JavaCC to update When an if statement has a block after it, the block sometimes must end with a semicolon sometimes but not all the time. Write an example showing a required semicolon after a "}" Write an example...
Telephone is a classic kids game where the first person in line is told a message....
Telephone is a classic kids game where the first person in line is told a message. That person whispers the word to the one next to them, and so on. The last person in line then announces the message as they understand it. Describe this game as a networking protocol in terms of the OSI model. Be specific, and include whatever layers you see as important. How could you change the protocol (and ruin the game) to ensure that the...
Without external cues such as the sun, people attempting to walk in a straight line tend...
Without external cues such as the sun, people attempting to walk in a straight line tend to walk in circles. It has been suggested that this tendency is due to internal asymmetries between individuals, or that individuals' legs differ in length or strength. Souman et al. (2009) tested for differences in individuals' tendencies to change direction by blindfolding 15 participants and asking them to walk in a straight line in an empty field. The numbers in this dataset represent median...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT