0

I'm trying to print a 2d grid with random numbers and another equally sized grid with -1s in place of the numbers that are evenly divisible by 3. I finished pretty much everything, but I cant figure out how to make the following line work: Arraycopy = c.createCoords({10,10});

I'm know I'm supposed to call the method with an int[][], I just cant remember how.

Here's all of the code just in-case:

public class practice
{

   public int [][] createArray(int rSize, int cSize)
    {
        Random r = new Random();
        int[][] array = new int [rSize] [cSize];
         for (int row = 0; row <array.length; row++)  
       {
           for(int col = 0; col < array[0].length; col++)
           {
             array[row][col] = r.nextInt(25);

           }

       }
         return array;
    }


    public void print2DArray(int[][] Array)
    {
       for (int row = 0; row <Array.length; row++)  
       {
           for(int col = 0; col < Array[0].length; col++)
           {
               System.out.print(Array[row][col] + "\t");
           }
            System.out.println("\n");
       } 
    }
public int[][] createCoords(int[][] Array)
{
    int [] [] coord = {
    {-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
    {-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
    {-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
    {-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
    {-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
    {-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
    {-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
    {-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
    {-1,-1,-1, -1, -1, -1, -1, -1, -1, -1},
    {-1,-1,-1, -1, -1, -1, -1, -1, -1, -1}

};
    for (int row = 0; row <coord.length; row++)  
       {
           for(int col = 0; col < coord[0].length; col++)
           {
               System.out.print(coord[row][col] + "\t");
           }
            System.out.println("\n");
}

    for (int row = 0; row <coord.length; row++)  
       {
           for(int col = 0; col < Array[row].length; col++)
           {
               if(Array[row][col] %3 == 0)
               coord[row][col] = -1;  

       }

       }
    return coord;

}

    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        practice c = new practice();

      int [][] myArray; 
      int [][] Arraycopy;
      myArray = c.createArray(10, 10); 
     Arraycopy = c.createCoords({10,10});
     c.print2DArray(Arraycopy);
      c.print2DArray(myArray);




    }


}
1
  • print2DArray(new int[][]{{1,2,2},{3,3,4}}); Commented Nov 16, 2015 at 0:53

1 Answer 1

1

Since your method takes an int[][] as parameter, {{10,10}} could work because it is an int[1][2] but it is apparently not your goal.

I'd suggest to use myArray directly here.


Solution

Replace

c.createCoords({10, 10});

with

c.createCoords(myArray);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, that worked. All of the numbers in the second grid are -1s though. What am I doing wrong? How can I make it so the -1s only appear when the number is evenly divisible by 3?
I think you can but am not sure. But I do know that asking a new question on an existing one is not a good idea at all. You'll narrow the scope of the question and change the context of the original one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.