-4

I have an assignment for a JAVA class I am taking. We are discussing two-dimensional arrays, however on this particular assignment, I can not figure out how to return back specific points and set those points with a specific value. Here is the assignment:

Write a method called create2DArray that will fill, create, and return a 10 x 10 2d array with random numbers in the range of 1 to 100. Write a method called print2DArray that will print a 10 x 10 2D array in row column fashion. Write a method called createCoords that will search the 2D array looking for any value that is evenly divisible by 3. Once you have found a number you should log the row, column location. This means when your method finishes it should produce a list of coordinates that I can use to plot my graph. This method must also return the number of coordinates that are divisible by 3 so that I know how many points there are to plot. I am not particular as to how the coordinates are returned back as long as I get a list of the row, column locations. So, I will leave it to you to work out a mechanism for returning the values. To test that you have logged the coordinates create another function called fillLocations that will fill the locations in the array you have logged with -1. So, your program should flow in this order

  1. create2DArray
  2. print2DArray
  3. createCoords
  4. fillLocations
  5. print2DArray

I understand and have completed create2DArray and print2DArray, but I can not figure out createCoords and fillLocations. Here is what I have so far, but it does not work and there are errors present:

public int createCoords(int row1, int col1){
int[][] coords = new int[row1][col1];  
int[][] count = new int[0][0];
int co = 0;
for(int row = 0; row < 10; row++)
{
    for(int col = 0; col < 10; col++)
    { 
    if(coords[row][col] % 3 == 0)
        co++;
        return count[row][col];
    }
}
return co;}
public int fillLocations(int[][] count){
int x = 0;
int y = 0;
for(int row = 0; row < 10; row++)
{
    for(int col = 0; col < 10; col++)
    {
    if(count[row][col] % 3 == 0)
        x = row;
        y = col;
        break;
    }
}
return (x, y);}
  • 1
    Where is "what you have so far"? – Ali Seyedi Apr 17 '16 at 19:40
  • a hint, to find coordinates divisibles by 3 you can use `%`. – Dinsdale Apr 17 '16 at 19:45
  • This looks like an assignment question. You must attempt to solve it yourself first and if you face a problem there then you should ask the question here with the relevant code. Then we could help you. – Blip Apr 17 '16 at 19:50
  • 2
    @computer-programmer Follow the flow that your teacher has provided step by step. and if in any step you had any specific problem which you couldn't solve yourself (even by using a debugger), then you can ask your question here and expect good answers to come. – Ali Seyedi Apr 17 '16 at 20:00
  • I have added my current code to the question. I can not figure out how to return coordinates and assign the coordinates a -1 value. Please help. @AliSeyedi – computer-programmer Apr 17 '16 at 20:05
  • Maybe you should think about what your code is supposed do. There are a few questions that you might want to answer to yourself before asking others: Why do you have a second two-dimensional array (count) that has a fixed size of 0 x 0? Why do you expect this zero-size-array to have a valid index between 0:0 and 9:9? Why do you return the value of "co" only when "co" is 0? What is the "co" variable supposed to do anyway? Why do you think that your method will assign some coordinate a value of -1 when you never set any value to -1? – Frank May 12 '16 at 08:33
  • Do you know that an array of int always initializes with all values set to 0 and that this means that in your case "count[row][col]" is always 0? Why do you test if the remainder of 0/3 is 0 and return 0 if it's not? (hint: it will never be anything but 0) – Frank May 12 '16 at 08:51

1 Answers1

1

As a programmer you'll nearly always need to research for doing different things. This research will be easier when you divide your problem to smaller problems.

For example you need to generate random numbers? So search on google that and you'll find this: How do I generate random integers within a specific range in Java?.

You need to create and return a 2D array? Google and see Syntax for creating a two-dimensional array

And with your art, put the pieces of the puzzle together in a way that gives your desired result.

public int[][] create2DArray() {        
    int[][] newArray = new int[10][10];
    Random random = new Random();
    int range = 100;
    for(int i = 0; i < 10; i++)
    {
        for(int j = 0;j<arr[0].length;j++)
        {
            newArray[i][j]= (random.nextInt(range) + 1);
        }
    }
    return newArray;
 }

This method, creates and returns a 10*10 2D array filled with random generated numbers between 1-100. I'm sure you can write the rest of your program and enjoy from it by yourself.

Community
  • 1
  • 1
Ali Seyedi
  • 1,758
  • 1
  • 19
  • 24