# 2d Arrays

• October 26th, 2009, 05:13 PM
mgutierrez19
2d Arrays
I have to create a lab that will
Print out the input 2D-array as an image on the console screen.
• Replace 0 by “ ” {space}.
• Replace values between 1 and 99 by “.” {one dot}.
• Replace values between 100 and 199 by “..” {two dots}.
• Replace values between 200 and 254 by “…” {three dots}.
• Replace 255’s by “x”.
- Remember to check if the dimensions of input array are valid numbers.
- Make use of method printf properly to print out the image nicely.

2.
- Change all numbers n in the array by (255-n).
- Print out the new matrix as well as the new image (using method Display).
- Remember to check if the dimensions of input array are valid numbers.

3.- Look for the pattern array inside the image.
- Erase the places where the pattern is found, i.e., change them to black (0).
- Print the 2D-array: before, and after if the pattern has been found.
- Return 1 if pattern is found, 0 otherwise.
- Example:
Image ( Before )

0 0 0 0 0
0 234 123 0 0
78 56 34 234 34
187 0 26 190 0
0 190 36 0 23
Pattern

0 234 123 0
78 56 34 234

Image ( After )

0 0 0 0 0
0 0 0 0 0
0 0 0 0 34
187 0 26 190 0
0 190 36 0 23

Code :

```public class Lab7 { public static void Display(int[][] image) { for(int i = 0; i < image.length; i++) //rows { for(int j = 0; j < image[0].length; j++) //columns { if(image[i][j] == 0) System.out.print(" "); else if(image[i][j] >= 1 && image[i][j] <= 99) System.out.print(" . "); else if(image[i][j] >= 100 && image[i][j] <= 199) System.out.print(" .. "); else if(image[i][j] >= 200 && image[i][j] <= 254) System.out.print(" ... "); else System.out.print(" x "); } System.out.println(); } }//end method public static void Modify(int[][] image) { int[][] modImage = new int[10][10]; int n;   for(int i = 0; i < image.length; i++) //rows { for(int j = 0; j < image[0].length; j++) //columns { n = (255 - image[i][j]);   modImage [i][j]= n; } } Display(modImage); }//end method public static int extractFeature(int[][] image, int[][] pattern) { int[][] extractImage = new int[10][10]; int found = 0;   return found;   }//end method```

I am Having Problems putting together the method extract feature if anyone can guide me in the right direction to have a working method? that is the only one that I need help with.
• October 26th, 2009, 10:29 PM
mgutierrez19
Re: 2d Arrays
I worked on the code and I came up with this but when I run it it does not run properly because it stops checking for the other numbers once it finds the first ones.

Code :

``` public static int extractFeature(int[][] image, int[][] pattern) { int[][] extractImage = new int[10][10]; int found = 0;   if (pattern.length <= image.length && pattern[0].length <= image[0].length){ for (int i = 0; i < pattern.length; i++) { for( int j = 0; j < pattern[0].length; j++) { for (int pcol = 0; pcol < pattern.length; pcol++) { (int prow = 0; prow < pattern[0].length; prow++) { if ( image[i][j] != pattern[prow][pcol]){ break; }else if ((prow == pattern.length - 1) && (pcol == pattern[0].length - 1)) { found = 1; return found; } } } } }   return found; }   }//end method```
• October 27th, 2009, 07:50 AM
literallyjer
Re: 2d Arrays
With out actually running your code, I would assume it has to do with the "return found" inside the last for loop. (Which also seems to be missing the "for" part of it..)
• October 27th, 2009, 09:52 AM
mgutierrez19
Re: 2d Arrays
I added in the for where I was missing it in my loop, but the code is still not working. it only returns 1 to me once it finds the first part of the pattern it doesnt check the pattern completely.
• October 27th, 2009, 11:12 AM
literallyjer
Re: 2d Arrays
You program is returning 1 because that is what you told it to do. It is apparent (based on what you've said) that your code is getting to the first return statement before the last one. Since that return statement will always return 1, that's what you always will get.
• October 27th, 2009, 04:08 PM
mgutierrez19
Re: 2d Arrays
Yeah but I want it to return 1 only if the pattern is found inside my array else it should return 0, my code keeps returning 1 if the first digit in the pattern is found which I dont want it to do, I only want one if the whole pattern is inside the array. I dont know how to scan the whole array for the pattern so that it could return 1 if the pattern is found and 0 if the whole pattern is not found.