Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8173803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:17:24+00:00 2026-06-06T22:17:24+00:00

I have this 2D array (say double[10][10]) which contain some 1.0 and 10.0, rest

  • 0

I have this 2D array (say double[10][10]) which contain some 1.0 and 10.0, rest are all in 0.0s. I am trying to loop through this array to find the 1.0 (start point), from there ‘move’ it randomly (using random.nextInt(4)) up, down, left or right till it gets to 10.0. I created a emptyArray to keep track of how many time it had moved pass each point (or at least I think I did). Nothing came up when I compile but I do not get any result when I try to display it into a frame. Any idea where I had gone wrong or missing?

{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,10.0,10.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,10.0,10.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}

Sample of 2D array.

    double[][] getPath(double[][] dataIn) {
    double[][] emptyArray = new double[dataIn.length][dataIn[0].length];
    double[][] drunkLoc = new double[dataIn.length][dataIn[0].length];
    for (int i = 0; i < dataIn.length; i++) {
        for (int j = 0; j < dataIn[i].length; j++) {
            if (dataIn[i][j] == 1.0) {

                double drunkHome = 10.0;
                drunkLoc[i][j] = dataIn[i][j];
                do {
                    int dir = getDirection();
                    switch(dir) {
                        case 0: 
                            if ((i > 0) && (j > 0)) {
                                drunkLoc[i][j] = drunkLoc[i-1][j];
                                double value = emptyArray[i][j];
                                emptyArray[i][j] = value + 1;
                                emptyArray[i][j] = (255<<24)  | (255<<16) | (255<<8) | 255;
                            } else {
                                break;
                            }
                            break;
                        case 1: 
                            if ((i > 0) && (j > 0)) {
                                drunkLoc[i][j] = drunkLoc[i][j-1];
                                double value = emptyArray[i][j];
                                emptyArray[i][j] = value + 1;
                                emptyArray[i][j] = (255<<24)  | (255<<16) | (255<<8) | 255;
                            } else {
                                break;
                            }
                            break;
                        case 2: 
                            if ((i > 0) && (j > 0)) {
                                drunkLoc[i][j] = drunkLoc[i+1][j];
                                double value = emptyArray[i][j];
                                emptyArray[i][j] = value + 1;
                                emptyArray[i][j] = (255<<24)  | (255<<16) | (255<<8) | 255;
                            } else {
                                break;
                            }
                            break;
                        case 3: 
                            if ((i > 0) && (j > 0)) {
                                drunkLoc[i][j] = drunkLoc[i][j+1];
                                double value = emptyArray[i][j];
                                emptyArray[i][j] = value + 1;
                                emptyArray[i][j] = (255<<24)  | (255<<16) | (255<<8) | 255;
                            } else {
                                break;
                            }
                            break;
                        default:
                    }
                } while (drunkLoc[i][j] != drunkHome);
            }
        }
    }
    return emptyArray;
}

Do let me know if you need any more clarification. Only my second post so still learning on my questioning technique. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:17:27+00:00Added an answer on June 6, 2026 at 10:17 pm

    OK, for you to study, here’s some sample code that will give you the answer you’re looking for (the number of moves to get from (xpos,ypos) to (destX,destY) on a “board” that is penDimension x penDimension in size:

            int penDimension = 10;
            int destX = 2;
            int destY = 2;
            int xpos = 5;
            int ypos = 5;
    
            // Add this to keep track of no moves through each square
            int[][] moveCounts = new int[penDimension][penDimension];
    
            Random r = new SecureRandom();
            long noMoves = 0;
            while (xpos != destX || ypos != destY) {
                switch (r.nextInt(4)) {
                case 0 : xpos++; break;
                case 1 : xpos--; break;
                case 2 : ypos++; break;
                case 3 : ypos--; break;
                }
                if (xpos < 0) xpos = 0;
                if (ypos < 0) ypos = 0;
                if (xpos > penDimension) xpos = penDimension;
                if (ypos > penDimension) ypos = penDimension;
                noMoves++;
    
                // Add this to keep track of no moves through each square
                moveCounts[ypos][xpos]++;
    
            }
            System.out.println("Number of moves: " + noMoves);
    

    Instead of doing ++ or — and then checking the bounds afterwards, you could also write (and in real life probably would write) e.g.:

    xpos = Math.max(0, xpos - 1);
    

    I just wrote it as I did above because I thought it would be easier to understand.

    Instead of writing “new SecureRandom()”, you could also write “new Random()”, which is probably what you’ve learnt. But SecureRandom is a much higher quality (but slower) random number generator. In general when writing ‘simulations’ where you are repeatedly generating a large number of random numbers, it’s better to avoid the standard Random class and use a higher quality generator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let’s say I have this array with shipments ids. s = Shipment.find(:all, :select =>
Let's say I have this array, $array = array( 'name' => 'hermet', 'emails' =>
So let's say I have this array: $requiredFruit= @(apple,pear,nectarine,grape) And I'm given a second
Let's say I have this $(document).ready(function() { var array = $.makeArray($('p')); $(array).appendTo(document.body); }); });
Let's say I have an array like this: $array = array (this,is,me,and,I,am,an,array,hello,beautiful,world); How can
Lets say I have an array with a structure like this: $arr= Array( array(
Say I have an array like this: [white, red, blue, red, white, green, red,
Say I have an object-like data record like this: $article = array( 'title' =>
Let's say I have an array of objects [{href: 'some_uri', 'class': 'this-css-class'}, {href: 'another_uri',
I have a 2d array, let's say like this : 2 0 8 9

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.