0

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);}
  • 2
    Please don't post the same question twice because you don't like the results you got from the [first one](http://stackoverflow.com/questions/36681292/java-two-dimensional-arrays-plotting-points). Voting to close. – ChiefTwoPencils Apr 17 '16 at 19:56
  • The first one does not contain an image and will soon be deleted. @CheifTwoPencils – computer-programmer Apr 17 '16 at 19:57
  • I think you got Array initialization and Array pointers mixed up. Take a close look at the difference in syntaxes. – n247s Apr 17 '16 at 20:03

2 Answers2

0

There are couple of problems with your code. Firstly, for the fillLocations method, you are expected to return the NUMBER of cells ([row][column]) where the value is divisible by 3. This means that for each value that passes the test: if(coords[row][col] %3 === 0) Then you should increment the counter by 1. So for this part, your code would look like:

int [][] your2DArray = new int[10][10];
...
private int getNumberOfValuesDivisibleByThree(){ 
int numberOfValuesDivisibleByThree = 0;
 for (int row=0; row < your2DArray.length; row++){
  for(int col = 0; col < your2DArray[0].length; col++){
   if(your2DArray[row][col] % 3 === 0){      
     //value at this coordinate or in this cell is divisible by 3
     //here you can 'log' the coordinates as required - 
     System.out.println(row+","+col);
     //increment the count
     numberOfValuesDivisibleByThree  +=1;
   }
  }
 }
//return the final count
return numberOfValuesDivisibleByThree;  
}

Please see how to create and manipulate a 2D array in Java. Syntax for creating a two-dimensional array

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
0

I'm not going to write the code, but pretty much how I would go about doing this is:

I would have a separate 2d boolean array the same size as the 2d int array, auto filled with false. This would go in createCoords.

use nested for loop to cycle through all addresses of the 2d int array, and when i find a number that's divisible by three, i mark the corresponding point on the boolean array as 'true'. This would also go in createCoords.

after the for loops finish, I then look at the original 2d int array and boolean array. i would use the nested for loops again, and when i find a true value on the boolean array, i would mark the corresponding location on the int array as -1. This would go in fillLocations. You should also pass the 2d boolean array to fillLocation along with the 2d int array.

Good luck!

EDIT: this is assuming that the FillLocation function REPLACES values in the 10x10 int array with -1. That's what I interpreted your question to be. Please correct me if i'm wrong. I also added a short description at the end of each paragraph where each segment of code would go.

a.deshpande012
  • 715
  • 7
  • 18
  • That is correct, I am replacing any number divisible by 3 with a -1 value and leaving all other values the same. However, I am unsure how I would code what you are stating and where everything would belong? Thank you so much for your help. – computer-programmer Apr 17 '16 at 20:35
  • @computer-programmer I edited the post, so now at the end of each 'part' it explains which function the code should be placed in. I'm going to leave the actual programming itself to you, though. – a.deshpande012 Apr 17 '16 at 20:42
  • I figured it out and have a working program! Thank you so very much for all of your help! @coolioasjulio – computer-programmer Apr 17 '16 at 21:16
  • @computer-programmer I'm glad you figured it out! Don't forget to mark a correct answer! – a.deshpande012 Apr 17 '16 at 21:43