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

Thread: (Comparing and) Printing 2D Array value on each direction (Down and Right)

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default (Comparing and) Printing 2D Array value on each direction (Down and Right)

    Hello. I have a 2D Array/Matrix and I want to write a code where I can compare each value in both directions. Let me explain myself I wrote a method that prints each value of the Array with the right direction and another one with the down direction. But my problem is how to compare each value? I have a hard time implementing a code about it and I need help.

    Here are the two methods
    //Driver 
    public static void main(String[] args) {
            int[][] grid = new int[][] { { 7, 4, 2 },
                    { 0, 5, 6 },
                    { 3, 1, 2 } };
     
    int arraySize = grid.length;
     
      for (int i = 0; i < grid.length; i++) {
                for (int j = 0; j < grid.length; j++) {
    // I can't solve my second issue to two the first issue with dead code
                }
            }
     
    }
     
    // goes to right direction - increment j first
        public static int nextRightElementMatrix(int[][] array, int arraySize) {
            System.out.println("Printing via right direction : ");
            for (int i = 0; i < arraySize; i++) {
                for (int j = 0; j < arraySize; j++) {
                    return array[i][j];
                }
            }
        }
     
    //  goes to down direction - increment i first
    public static int nextDownElementMatrix(int[][] array, int arraySize) {
            System.out.println("Printing via down direction : ");
            for (int j = 0; j < arraySize; j++) {
                for (int i = 0; i < arraySize; i++) {
                    return array[i][j];
                }
            }
        }

    The first issue is that I get a dead code on j++ if I try to return the value of the element to the method. The second issue will be how to compare each value of those two methods. (I was thinking to include both of them in another a compare?)
    This is where I need help, thank you.

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

    Default Re: (Comparing and) Printing 2D Array value on each direction (Down and Right)

    I have several questions about your post:
    how to compare each value?
    Can you explain what values are to be compared and in what order you want to compare them?
    I get a dead code
    What is dead code?

    Notes on the posted methods. Both have a return statement that will always return on the first execution of the loop.
    What do are those methods supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: (Comparing and) Printing 2D Array value on each direction (Down and Right)

    OK. I will give you the instructions for this exercise to better understand me.
    Here it is: Write a Java program to find a path from top left to bottom in the right direction which minimizes the sum of all numbers along its path - Note: Move either down or right at any point in time.

    So in that case, my approach is to to start from the top left of the grid so 7, and compare his adjacent value so 4 and 0 in that case. 0<4 so the sum will be 7 + 0. Then, we move to the adjacent value of 0 and compare them, and so on.
    So in my approach - I made two functions to give the next Right element of the grid and the next Down element of the grid. Here you will have a visual understanding of my explanation : https://www.w3resource.com/java-exer...ercise-133.php

    Dead code is any code that's never executed, or if executed, the execution has no effect on the application's behaviour.

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

    Default Re: (Comparing and) Printing 2D Array value on each direction (Down and Right)

    Is this a new program? The original question is different from this description:
    find a path from top left to bottom in the right direction which minimizes the sum of all numbers along its path
    So the program is to find the path with the smallest sum? ==> does that mean all the paths need to be summed and then sorted to find the one with the smallest value?
    What are the rules for the getting the next number? You can only go the right or down. Never up or left.
    The end of the path is the lower right corner?

    two functions(methods) to give the next Right element of the grid and the next Down element of the grid
    To do that the methods need to know the row and column of the current element.
    What do they do when there is no next element?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: (Comparing and) Printing 2D Array value on each direction (Down and Right)

    New program? Are you talking about the solutions of the link I provided. I haven't look at it. I'm working on my own solution first before checking. Just for the image representation I shared that link : https://www.w3resource.com/java-exer...ercise-133.php
    From my understanding, the rules is to go only right or down. The goal is to get to the "from top left to bottom in the right direction which minimizes the sum of all numbers along its path" - so in others words, from 7 (top left) to 2 (right bottom). So the sum starts at 7. You check the right number and down number to 7 and choose the smallest one and add it to the sum, then you move to the smallest number between both and check the right and down number from it and choose the smallest to add to sum and so on

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

    Default Re: (Comparing and) Printing 2D Array value on each direction (Down and Right)

    What about the other issues I asked about?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: (Comparing and) Printing 2D Array value on each direction (Down and Right)

    The case of no next element should come when we reach the right bottom. But True, I haven't take care of that case yet

    --- Update ---

    Maybe my approach is not the best? And I should reconsider?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: (Comparing and) Printing 2D Array value on each direction (Down and Right)

    case of no next element
    If the current position is in the right most column and not on the bottom row, what would happen if the nextRightElement method is called?

    How do the methods know the current location when they are asked for the value of the next element?

    I should reconsider?
    You need to spend more time designing how the program will function before writing any more code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Comparing Input to array
    By hbonh in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 28th, 2011, 10:43 AM
  2. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  3. URGENT!!!! help with array element comparing
    By Neo in forum Java Theory & Questions
    Replies: 3
    Last Post: March 3rd, 2011, 08:52 AM
  4. Comparing two files and printing out matching words
    By sport10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 3rd, 2010, 09:10 PM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM

Tags for this Thread