Question

It is java Drunken Cockroach Problem Expand the 1-dimension drunken cockroach problem to 2 dimensions. The...

It is java

Drunken Cockroach Problem

Expand the 1-dimension drunken cockroach problem to 2 dimensions. The cockroach can move left or right or up or down. The cockroach can move horizontally – left or right or virtically – up or down. Again the cockroach moves until it visits all the tiles at least once.

using this code

#include <ctime>

#include <iomanip>

#include <iostream>

#include <random>

#include <string>

using namespace std;

int main()

{

    const int worldsize = 10;

    default_random_engine e(1776);

    uniform_int_distribution<int> ustagger(-1, 1);

    uniform_int_distribution<int> uplace(0, worldsize);

    

    int visits[worldsize];

    for (int k = 0; k < worldsize; ++k)

        visits[k] = 0;

    int unvisited = worldsize;

    int initialpos = uplace(e);

    visits[initialpos] = 1;

    unvisited--;

    for (int k = 0; k < worldsize; ++k)

        cout << visits[k] << ' ';

    cout << endl;

    system("pause");

    int pos = initialpos;

    while (unvisited > 0)

    {

        int newpos = pos + ustagger(e);

        while (newpos < 0 || worldsize <= newpos || newpos == pos)

            newpos = pos + ustagger(e);

        pos = newpos;

        visits[pos]++;

        if (visits[pos] == 1)

            unvisited--;

        

        for (int k = 0; k < worldsize; ++k)

            cout << visits[k] << ' ';

        cout << endl;

        system("pause");

    }


}

Homework Answers

Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

#include <ctime>
#include <iomanip>
#include <iostream>
#include <random>
#include <string>
using namespace std;

//defining worldsize as a global constant
//note: now that the world is 2 dimensional, use smaller values for worldsize
//otherwise it will take too much time to visit all tiles. here, I'm setting 
//it as 3, which means our world is a 3*3 matrix
#define worldsize 3

//method to print the 2D array of dimensions: worldsize x worldsize
void printArr(int arr[worldsize][worldsize]){
        for (int i = 0; i < worldsize; ++i){
                for (int j = 0; j < worldsize; ++j){
                        cout << arr[i][j] << ' ';
                }
                cout<<endl;
        }
}

int main()
{
    default_random_engine e(1776);
    uniform_int_distribution<int> ustagger(-1, 1);
    uniform_int_distribution<int> uplace(0, worldsize);
    //declaring 2d array instead of 1D
    int visits[worldsize][worldsize];
    //filling with 0
    for (int i = 0; i < worldsize; ++i)
                for (int j = 0; j < worldsize; ++j)
                visits[i][j] = 0;
    int unvisited = worldsize*worldsize; //3*3=9 unvisited locations
    int r = uplace(e); //starting row
    int c = uplace(e); //starting column
    visits[r][c] = 1; //marking start location
    unvisited--;
    printArr(visits); //printing world
    system("pause");
    //looping
    while (unvisited > 0)
    {
        //using ustagger to determine where we want to go (up/down or right/left)
        if(ustagger(e)==0){
                //going up or down
                int newr = r + ustagger(e);
                while (newr < 0 || worldsize <= newr || newr == r)
                    newr = r + ustagger(e);
                r=newr;
                }else{
                        //going left or right
                        int newc = c + ustagger(e);
                while (newc < 0 || worldsize <= newc || newc == c)
                    newc = c + ustagger(e);
                c=newc;
                }
                //marking visits to new location
        visits[r][c]++;
        if (visits[r][c] == 1)
            unvisited--; //one more unvisited location is now visited
        printArr(visits); //displaying world
        system("pause");
    }
}

/*OUTPUT (partial)*/

0 0 1
0 0 0
0 0 0
Press any key to continue . . .
0 1 1
0 0 0
0 0 0
Press any key to continue . . .
1 1 1
0 0 0
0 0 0
Press any key to continue . . .
1 2 1
0 0 0
0 0 0
Press any key to continue . . .
1 2 1
0 1 0
0 0 0
Press any key to continue . . .
1 2 1
0 1 1
0 0 0
Press any key to continue . . .

...
...
...


8 17 9
9 19 8
3 5 1
Press any key to continue . . .
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 +...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n. Let’s estimate natural using n = 20. sum_thread.cpp #include <chrono> #include <iostream> #include <mutex> #include <random> #include <utility> #include <vector> #include <thread> using namespace std; constexpr long long size= 1000000; mutex myMutex; void sumUp(unsigned long long& sum, const vector<int>& val, unsigned long long beg, unsigned long long end){ long long localSum = 0; for (auto it= beg; it < end; ++it){ localSum+=...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 +...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n. Let’s estimate natural using n = 20. sum_thread.cpp #include <chrono> #include <iostream> #include <mutex> #include <random> #include <utility> #include <vector> #include <thread> using namespace std; constexpr long long size= 1000000; mutex myMutex; void sumUp(unsigned long long& sum, const vector<int>& val, unsigned long long beg, unsigned long long end){ long long localSum = 0; for (auto it= beg; it < end; ++it){ localSum+=...
Modify the sum_thread.cpp program to compute harmonic sum = 1 + 1/2 + 1/3 + 1/4...
Modify the sum_thread.cpp program to compute harmonic sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n. Let’s estimate natural using n = 20. sum_thread.cpp #include <chrono> #include <iostream> #include <mutex> #include <random> #include <utility> #include <vector> #include <thread> using namespace std; constexpr long long size= 1000000; mutex myMutex; void sumUp(unsigned long long& sum, const vector<int>& val, unsigned long long beg, unsigned long long end){ long long localSum = 0; for (auto it= beg; it < end; ++it){...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow...
Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow each number by a space. Example output for userNum = 40: 20 10 5 2 1 ..... my code: #include <iostream> using namespace std; int main() { int userNum; userNum = 40; /* Your solution goes here */ while (userNum != 1){ userNum = userNum/2; cout << userNum << " ";   } cout << endl; return 0; } ........ but as a result i...
Menu.cpp isn't working. C++  utilize inheritance to create a banking app. The architecture of the app is...
Menu.cpp isn't working. C++  utilize inheritance to create a banking app. The architecture of the app is up to you as long as you implement some form of inheritance. Below is my code however the credit and the actual menu isn't working. Can i get some help on getting the menu to work properly. // BankAccount.h #ifndef ACCOUNT_H #define ACCOUNT_H #include <string> #include <iostream> using namespace std; class BankAccount { protected : int noOfWithdrawls; private: // Declaring variables    float balance;...
Programming Exercise 2: implement the member function moveToNth(...) that removes the item marked by the cursor...
Programming Exercise 2: implement the member function moveToNth(...) that removes the item marked by the cursor and inserts it as the nth element of the list; test your implementation by turning the flag LAB3_TEST2 from 0 to 1 in config.h; - Programming Exercise 3: implement the ListArray member function find(...) that searches for the element given as a parameter; the search starts at the cursor and stops when it finds the element or at the end of the list; the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
The program shown below reads in (N) values of time (hours, minutes, and seconds). If the...
The program shown below reads in (N) values of time (hours, minutes, and seconds). If the values for hours, minutes and seconds are a legal military time (i.e. 00 00 00 to 23 59 59) the program should display the formatted results (i.e. 12 34 56 should be displayed as 12:34:56). If there's an error for any of the entered values, an exception should be thrown and an error message should be displayed. Note, there are three exception conditions, one...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT