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: Method to solve a word search in Java using 2d arrays

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Method to solve a word search in Java using 2d arrays

    I'm trying to create a method that will search through a 2d array of numbers. If the numbers add up to a certain sum, those numbers should remain and all of the other numbers should be changed to a 0. For example, if the desired sum is 7 and a row contains 2 5 1 2, the result should be 2 5 0 0 after the method is implemented. I have everything functioning but instead of keeping all of the numbers that add up to the sum, only the last number is retained. So, I am left with 0 5 0 0 . I think I need another array somewhere but not sure exactly how to go about implementing it. Any ideas?
        public static int[][] horizontalSums(int[][] a, int sumToFind) {
    		int[][] b = new int[a.length][a[0].length];
    		int columnStart = 0;
    		while (columnStart < a[0].length) {
    			for (int row = 0; row < a.length; row++) {
    				int sum = 0;
    				for (int column = columnStart; column < a[row].length; column++) {
    					sum += a[row][column];
    					if (sum == sumToFind) {
    						b[row][column] = a[row][column];
    					}
    				}
     
    			}
    			columnStart++;
    		}
    		return b;
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Method to solve a word search in Java using 2d arrays

    Can you explain the algorithm you are using for this program? What steps does the code need to take to get the desired results?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Method to solve a word search in Java using 2d arrays

    It need go through a horizontal array checking if consecutive numbers add up to a desired sum. If they do, those numbers remain, if they don't, the numbers become zeros.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Method to solve a word search in Java using 2d arrays

    checking if consecutive numbers add up to a desired sum
    Does that mean the code must restart the search when the sum gets too large? How will the code handle that? For example if the target is 7 and a row has: 4 5 5 2 there will need to be some restarts.

    What steps must the code take to do this: those numbers remain
    or this: the numbers become zeros.

    Those are high level requirements for the program. What logic/steps does the code need to take to do them?

    There are two arrays: a and b
    how are they used?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Method to solve a word search in Java using 2d arrays

    if its 4 5 5 2. its supposed to add 4 and 5, then add 5 and 5, then and 5 and 2.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Method to solve a word search in Java using 2d arrays

    How will the code do that? What is the logic?
    What does the code need to remember to be able to restart the summing for the next numbers in the row?
    What should it do when some where in the search through the values in a row it finds a series of numbers with the right sum?

    You should take a piece of paper and a pencil, write a series of numbers in a row and then work out the logic. Draw pointers under the numbers to represent the indexes into the array. Label them and see how the indexes need to be moved and used to solve the problem.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Word Search
    By jamie1234 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: April 9th, 2013, 06:03 PM
  2. Replies: 7
    Last Post: March 29th, 2013, 07:38 AM
  3. Java Word Scrambling Game Method Help!
    By youreperfect in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 2nd, 2012, 05:31 PM
  4. Word Search Help
    By Tanner in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 3rd, 2011, 01:39 AM
  5. Search Word puzzle game
    By lew1s in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 9th, 2011, 04:23 AM

Tags for this Thread