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 15 of 15

Thread: Help with looking through a two-dimensional array.

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Post Help with looking through a two-dimensional array.

    Hi there, I'm working on a problem that involves finding a largest product of any 4 consecutive numbers in a grid. I don't want the answer to the question, but I can't figure out how to search through the 2-d array diagonally both down to the left and down to the right incrementally by 4s. I don't have any code because I can't figure out how to code an algorithm to sort through. I can figure out how to search diagonally without a increment, but with one, I do not now.

    I'll attach the text file I'm feeding into the array.prob11.txt


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,092
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Help with looking through a two-dimensional array.

    how to search through the 2-d array diagonally both down to the left and down to the right incrementally by 4s.
    Perhaps drawing a grid and labeling the squares with their row/column indexes would show you how the row and column values change as you go from square to square in a diagonal direction.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Help with looking through a two-dimensional array.

    I understand how it flows, for example:

    0,0 0,1 0,2 0,3 0,4
    1,0 1,1 1,2 1,3 1,4
    2,0 2,1 2,2 2,3 2,4
    3,0 3,1 3,2 3,3 3,4
    4,0 4,1 4,2 4,3 4,4

    I just don't understand how I could write an algorithm to do

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,092
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Help with looking through a two-dimensional array.

    How many starting locations have a path through 4 other squares?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Help with looking through a two-dimensional array.

    in a 20x20 grid, there would be about 256 possible start locations going one direction.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,092
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Help with looking through a two-dimensional array.

    Ok, you posted a 4x4 grid. The logic would be the same. What are the changes in row and column for down & left and what are the changes for down & right?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Help with looking through a two-dimensional array.

    I understand the changes, as y goes down, x goes up, and vice versa. I just dont know how to code it.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,092
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Help with looking through a two-dimensional array.

    Add one to row to go down.
    Add one to column to go right.
    Subtract one from column to go left.
    In all cases: Test if past bounds of array.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Help with looking through a two-dimensional array.

    Ok, that makes sense, but what about the other part of my question, how would I do it in increments of fours?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,092
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Help with looking through a two-dimensional array.

    Are you asking how to add 4 to a number instead of adding one?
    row 0, row 4, row 8, row 12 etc
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Help with looking through a two-dimensional array.

    The problem is asking for the greatest product of any four consecutive numbers in a grid. I have the numbers posted in a file above so you can tinker with a program. Increments of four meaning for example:

    1 2 3 4 5 6 7 8 9

    1*2*3*4

    then

    2*3*4*5

    then

    3*4*5*6

    and so on. Let's consider it diagonally, which is where I need the help.

    so something like this diagonal:

    1 2 3 4 5 6 [7] 8 9
    1 2 3 4 5 [6] 7 8 9
    1 2 3 4 [5] 6 7 8 9
    1 2 3 [4] 5 6 7 8 9
    1 2 [3] 4 5 6 7 8 9
    1 [2] 3 4 5 6 7 8 9
    [1] 2 3 4 5 6 7 8 9

    7*6*5*4

    then

    6*5*4*3

    then

    5*4*3*2

    and so one. I don't know how to write code to solve that aspect of the problem. That's also why I gave the text file with all the inputs for the 2-d array.

  12. #12
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Help with looking through a two-dimensional array.

    same thing with vertically, but horizontally and vertically are easy to code.

  13. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,336
    Thanks
    182
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Help with looking through a two-dimensional array.

    Quote Originally Posted by aesguitar View Post
    same thing with vertically, but horizontally and vertically are easy to code.
    If I understand the problem correctly...and making the vertical (or horizontal) is easy, then why not make the diagonal vertical. In the example in post #11, shift each row +1 and calculate the vertical - when diagonal down to the right, shift each row by -1 and calculate the vertical. You can do this by copying the underlying data and physically shift each row, or think hard about how you can do so by modifying your loops to analyze diagonals.
    Last edited by copeg; May 31st, 2012 at 11:11 PM.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,092
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Help with looking through a two-dimensional array.

    What does this mean: in increments of fours? Your example looks like it takes 4 adjacent numbers, not every 4th number.
    Start at the top left, get the 4 numbers down left first and then the four numbers down right. Then move to the next column to the right and do it again until the end of the columns
    Then move to the next row's first column and continue until done.

    Many tests will fail because there are not 4 numbers in that direction. For example there are none for down left from the top left corner.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Help with looking through a two-dimensional array.

    I've actually figure out how I'm going to do the diagonals now.

    here's the code:

    package stuff;
     
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
    import algorithms.StopWatch;
     
    public class tester
    {
     
     
    	public static void main(String args[]) throws FileNotFoundException {
    		StopWatch timer = new StopWatch();
    		timer.start();
     
    		ArrayList<Integer> prods = new ArrayList<Integer>();
    		int[][] nums = new int[20][20];
    		Scanner in = new Scanner(new File("prob11.txt"));
    		for(int i = 0; i < 20; i++)
    		{
    			for(int j = 0; j < 20; j++)
    			{
    				nums[i][j] = in.nextInt();
    			}
    		}
     
     
    		//horizontal products
     
    		for(int i = 0; i<20;i++)
    		{
    			for(int j = 0; j < 17; j++)
    			{
    				int prod = 1;
    				for(int l = j; l < j + 4; l++ )
    				{
    					prod*=nums[i][l];
    				}
    				prods.add(prod);
    			}
    		}
     
    		//vertical products
     
    		for(int i = 0; i<20;i++)
    		{
    			for(int j = 0; j < 17; j++)
    			{
    				int prod = 1;
    				for(int l = j; l < j + 4; l++ )
    				{
    					prod*=nums[l][i];
    				}
    				prods.add(prod);
    			}
    		}
     
    		//diagonal down-left products
    		for(int i = 0; i < 17; i++)
    		{
    			for(int j = 3; j < 20; j++)
    			{
    				int prod = nums[i][j]*nums[i+1][j-1]*nums[i+2][j-2]*nums[i+3][j-3];
    				prods.add(prod);
    			}
    		}
     
    		//diagonal down-right
    		for(int i = 0; i < 17; i++)
    		{
    			for(int j = 0; j < 17; j++)
    			{
    				int prod = nums[i][j]*nums[i+1][j+1]*nums[i+2][j+2]*nums[i+3][j+3];
    				prods.add(prod);
    			}
    		}
     
    		//gets the largest product in the arraylist
    		int largest = 0;
    		for(int i : prods)
    		{
    			if(i>largest)
    			{
    				largest=i;
    			}
    		}
    		System.out.println(largest);
     
     
    		timer.stop();
    		System.out.println("\n" + timer.getElapsedTime());
     
    	}
    }

    Don't worry about the stopwatch stuff, it only times how long it takes, about 50ms.

Similar Threads

  1. Help with one-dimensional array
    By brandon66 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 26th, 2012, 04:59 PM
  2. [SOLVED] 2 dimensional array storing help!
    By jts0541 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 31st, 2012, 02:31 PM
  3. Single Dimensional Array Help!
    By Allicat in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 15th, 2011, 12:01 PM
  4. Two-Dimensional Array and Loops
    By astrojunk in forum Java Theory & Questions
    Replies: 2
    Last Post: February 11th, 2011, 06:18 AM
  5. 2 dimensional array alternative ???
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: February 23rd, 2010, 05:18 PM

Tags for this Thread