Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: 2d Arrays

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

    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.


  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

    	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

  3. #3
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default 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..)
    Last edited by literallyjer; October 27th, 2009 at 07:52 AM.

  4. #4
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #5
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default 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.

  6. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Arrays Of Objects?
    By MysticDeath in forum Object Oriented Programming
    Replies: 2
    Last Post: October 20th, 2009, 09:39 PM
  2. Throwing arrays as objects
    By Audemars in forum Collections and Generics
    Replies: 1
    Last Post: September 23rd, 2009, 06:29 PM
  3. Help with Sort arrays
    By drk in forum Collections and Generics
    Replies: 5
    Last Post: September 6th, 2009, 02:48 AM
  4. Elegant and short way to implement my program on 2d Array
    By TheForumLord in forum Collections and Generics
    Replies: 1
    Last Post: December 8th, 2008, 04:56 PM
  5. Java error in using Arrays
    By AnithaBabu1 in forum Collections and Generics
    Replies: 4
    Last Post: November 4th, 2008, 07:50 AM