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

Thread: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

  1. #1
    Junior Member
    Join Date
    Jan 2021
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    The method will receive a matrix array of truth and false (the array can be changed by the method), a group is the same figures that are by the sides and not across.
    for example 1 equals truth and zero equals false

    100
    110
    001

    in this matrix there are two groups.
    sorry for the long introduction but can some one help me write this method, I tried for hours to understand recursion but still haven't managed to succeed.
    Thank you.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    What have you tried?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2021
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    Thank you for the reply.
    //1s.
    //************************************************** ******************
    public class Maze
    {
    private boolean [][] booleanGrid = { {true,true,true},
    {false,false,false},
    {true,true,false},
    {false,false,false},
    {false,false,false} };


    private int counterOfTruthAreas = 0;
    private final int TRIED = 3;
    private final int PATH = 7;
    private int[][] grid = new int[booleanGrid.length][booleanGrid[0].length];
    // { {0,0,0,0,0},
    //{0,0,0,0,0},
    // {1,0,1,1,0},
    // {0,0,0,0,0},
    // {1,1,1,1,0} };

    public Maze( boolean [][] grid) {

    ConvertGridFromBooleanToInt (0,0);

    }
    //---------------------------------------------------------
    //--------
    // Attempts to recursively traverse the maze. Inserts
    //special
    // characters indicating locations that have been tried
    //and that
    // eventually become part of the solution.
    //---------------------------------------------------------
    //--------
    public boolean traverse(int row, int column)
    {
    boolean done = false;
    System.out.println("starts traverse row "+ row +" and column " + column);
    if (valid(row, column))
    {
    //grid[row][column] = TRIED; // this cell has been
    //tried
    validGroup(row, column);

    if (row == grid.length-1 && column == grid[0].length-1) {

    done = true; // the maze is solved
    }
    else
    {
    System.out.println("traverse colum" + column + " and row" + row);

    done = traverse(row, column+1); // right

    if (done == false) {
    done = traverse(row+1, 0); // down
    }
    if (done == false) {
    done = traverse(row-1, column); // up
    }
    if (done == false) {
    done = traverse(row, column-1); // left
    }
    }
    if (done) { // this location is part of the final
    //path
    //grid[row][column] = PATH;
    }
    }
    return done;
    }
    //---------------------------------------------------------
    //--------
    // Determines if a specific location is valid.
    //---------------------------------------------------------
    //--------
    private boolean valid(int row, int column)
    {

    boolean result = false;
    // check if cell is in the bounds of the matrix
    System.out.println("cheak if valid in row " + row + " and column " + column);

    if (row >= 0 && row < grid.length && column >= 0 && column < grid[row].length) {
    System.out.println("valid");
    // check if cell is not blocked and not previously tried
    if ((grid[row][column] == 1)) {
    grid[row][column] = 2;
    System.out.println("there is 1 in row " + row + " and column " + column);

    System.out.println("check if right");

    if(( column + 1 <= grid[0].length-1) && (grid[row][column+1] == 1)) {


    System.out.println("right");

    result = valid(row, column+1); // right
    }
    System.out.println("check if down");

    if (( row + 1 <= grid.length - 1) && (grid[row+1][column] == 1)) {


    System.out.println("down");
    result = valid(row+1, 0); // down
    }
    System.out.println("check if up");

    if (( row - 1 > 0) && (grid[row-1][column] == 1)) {


    System.out.println("up");
    result = valid(row-1, column); // up
    }

    System.out.println("check if left");

    if (( column-1 > 0) && (grid[row][column-1] == 1)) {


    System.out.println("left");
    result = valid(row, column-1); // left
    }
    }
    System.out.println("true");

    result = true;
    //}
    }
    System.out.println("?");
    return result;
    }
    //---------------------------------------------------------
    //--------
    // Returns the maze as a string.
    //---------------------------------------------------------
    //--------
    public String toString()//delet as it contains loop
    {
    String result = "\n";
    for (int row=0; row < grid.length; row++)
    {
    for (int column=0; column < grid[row].length;
    column++)
    result += grid[row][column] + "";
    result += "\n";
    }
    return result;
    }

    public void ShowTruthAreas() {
    System.out.println(counterOfTruthAreas);
    }


    public boolean ConvertGridFromBooleanToInt (int row, int column){

    boolean done = false;
    if (validForConversion(row, column)) {

    if (row == grid.length-1 && column == grid[0].length-1) {

    done = true; // the grid is converted from boolean to 1 for truth and 0 for false
    row = 0;
    column = 0;
    }
    else
    {
    done = ConvertGridFromBooleanToInt(row, column+1); // right

    if (done == false) {
    done = ConvertGridFromBooleanToInt(row+1, 0); // down
    System.out.println("down");

    }
    }
    }
    return done;
    }

    private boolean validForConversion(int row, int column)
    {
    boolean result = false;
    // check if cell is in the bounds of the matrix
    if (row >= 0 && row < booleanGrid.length && column >= 0 && column < booleanGrid[row].length) {

    // check if cell is not blocked and not previously tried
    if ((booleanGrid[row][column] == true)) {

    System.out.println(row + " , " + column + "true");

    grid[row][column] = 1;

    }

    else if ((booleanGrid[row][column] == false)) {

    System.out.println(row + " , " + column + "false" );

    grid[row][column] = 0;

    }

    result = true;
    //}
    }
    return result;
    }

    public void validGroup () {


    }

    }

    --- Update ---

    This is modification to an example that I thought was similar to my problem, so not everything there makes sense
    I got stuck in the operation of, after the maze was converted from true and false to 1 and 0, to collect all the groups of 1's

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    What does the posted code do that is useful for solving your problem?
    Do you have any specific questions about the posted code?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2021
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    //1s.
    //************************************************** ******************
    public class Maze
    {
    private boolean [][] booleanGrid = { {true,true,true},
    {false,false,false},
    {true,true,false},
    {false,false,false},
    {false,false,false} };
     
     
    private int counterOfTruthAreas = 0;
    private final int TRIED = 3;
    private final int PATH = 7;
    private int[][] grid = new int[booleanGrid.length][booleanGrid[0].length];
    // { {0,0,0,0,0},
    //{0,0,0,0,0},
    // {1,0,1,1,0},
    // {0,0,0,0,0},
    // {1,1,1,1,0} };
     
    public Maze( boolean [][] grid) {
     
    ConvertGridFromBooleanToInt (0,0);
     
    }
    //---------------------------------------------------------
    //--------
    // Attempts to recursively traverse the maze. Inserts
    //special
    // characters indicating locations that have been tried
    //and that
    // eventually become part of the solution.
    //---------------------------------------------------------
    //--------
    public boolean traverse(int row, int column)
    {
    boolean done = false;
    System.out.println("starts traverse row "+ row +" and column " + column);
    if (valid(row, column))
    {
    //grid[row][column] = TRIED; // this cell has been
    //tried
    validGroup(row, column);
     
    if (row == grid.length-1 && column == grid[0].length-1) {
     
    done = true; // the maze is solved
    }
    else
    {
    System.out.println("traverse colum" + column + " and row" + row);
     
    done = traverse(row, column+1); // right
     
    if (done == false) {
    done = traverse(row+1, 0); // down
    }
    if (done == false) {
    done = traverse(row-1, column); // up
    }
    if (done == false) {
    done = traverse(row, column-1); // left
    }
    }
    if (done) { // this location is part of the final
    //path
    //grid[row][column] = PATH;
    }
    }
    return done;
    }
    //---------------------------------------------------------
    //--------
    // Determines if a specific location is valid.
    //---------------------------------------------------------
    //--------
    private boolean valid(int row, int column)
    {
     
    boolean result = false;
    // check if cell is in the bounds of the matrix
    System.out.println("cheak if valid in row " + row + " and column " + column);
     
    if (row >= 0 && row < grid.length && column >= 0 && column < grid[row].length) {
    System.out.println("valid");
    // check if cell is not blocked and not previously tried
    if ((grid[row][column] == 1)) {
    grid[row][column] = 2;
    System.out.println("there is 1 in row " + row + " and column " + column);
     
    System.out.println("check if right");
     
    if(( column + 1 <= grid[0].length-1) && (grid[row][column+1] == 1)) {
     
     
    System.out.println("right");
     
    result = valid(row, column+1); // right
    }
    System.out.println("check if down");
     
    if (( row + 1 <= grid.length - 1) && (grid[row+1][column] == 1)) {
     
     
    System.out.println("down");
    result = valid(row+1, 0); // down
    }
    System.out.println("check if up");
     
    if (( row - 1 > 0) && (grid[row-1][column] == 1)) {
     
     
    System.out.println("up");
    result = valid(row-1, column); // up
    }
     
    System.out.println("check if left");
     
    if (( column-1 > 0) && (grid[row][column-1] == 1)) {
     
     
    System.out.println("left");
    result = valid(row, column-1); // left
    }
    }
    System.out.println("true");
     
    result = true;
    //}
    }
    System.out.println("?");
    return result;
    }
    //---------------------------------------------------------
    //--------
    // Returns the maze as a string.
    //---------------------------------------------------------
    //--------
    public String toString()//delet as it contains loop
    {
    String result = "\n";
    for (int row=0; row < grid.length; row++)
    {
    for (int column=0; column < grid[row].length;
    column++)
    result += grid[row][column] + "";
    result += "\n";
    }
    return result;
    }
     
    public void ShowTruthAreas() {
    System.out.println(counterOfTruthAreas);
    }
     
     
    public boolean ConvertGridFromBooleanToInt (int row, int column){
     
    boolean done = false;
    if (validForConversion(row, column)) {
     
    if (row == grid.length-1 && column == grid[0].length-1) {
     
    done = true; // the grid is converted from boolean to 1 for truth and 0 for false
    row = 0;
    column = 0;
    }
    else
    {
    done = ConvertGridFromBooleanToInt(row, column+1); // right
     
    if (done == false) {
    done = ConvertGridFromBooleanToInt(row+1, 0); // down
    System.out.println("down");
     
    }
    }
    }
    return done;
    }
     
    private boolean validForConversion(int row, int column)
    {
    boolean result = false;
    // check if cell is in the bounds of the matrix
    if (row >= 0 && row < booleanGrid.length && column >= 0 && column < booleanGrid[row].length) {
     
    // check if cell is not blocked and not previously tried
    if ((booleanGrid[row][column] == true)) {
     
    System.out.println(row + " , " + column + "true");
     
    grid[row][column] = 1;
     
    }
     
    else if ((booleanGrid[row][column] == false)) {
     
    System.out.println(row + " , " + column + "false" );
     
    grid[row][column] = 0;
     
    }
     
    result = true;
    //}
    }
    return result;
    }
     
    public void validGroup () {
     
     
    }
     
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    The posted code has lost all its indentations making it much harder to read and understand.
    Please restore the code's proper indentations.

    Also where is the main method for executing the code?

    What happens when the posted code is compiled and executed?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2021
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    everything is useful except the finals.
    I would like to know how to build a recursive method that receives an matrix array and computes, without loops, how many groups of 1 it contains, group does not include across.
    for example: 101
    001
    110
    this matrix has 3 groups

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    matrix array
    Is that a two dimensional array? Do the number of rows need to be the same as the number of columns?
    groups of 1
    Can you define what a "group of 1" is?

    Here is a discussion on how it might be done:
    The recursive method would take a reference to the array and some other value(s)
    and return the count. The method would continue to call itself until there were no more possible groups to be found.
    Can there be more than one method?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jan 2021
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    "Is that a two dimensional array? Do the number of rows need to be the same as the number of columns?"
    yes it is two dimensional array, the numbers and rows are the same.

    "Can you define what a "group of 1" is?"
    group of 1 means that the groups I am searching for only contain the number 1.

    you can have as many method as you would like to.

    Thank you and sorry for my English.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    the groups I am searching
    What is a "group"? Can you give an example?

    only contain the number 1.
    Can you post an example of that?
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    godmars20 (January 16th, 2021)

  12. #11
    Junior Member
    Join Date
    Jan 2021
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    110
    110
    000

    this two dimensional array has 1 group

    010
    010
    001

    this two dimensional array has 2 group

    --- Update ---

    there are only two possible values in the array 1 and 0

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    Sorry, I do not understand what a group is. You give two examples but do not explain what a group is.
    For example, are the 4 1s in red in one group?
    110
    110
    000

    Here one group is red and one group is blue:
    010
    010
    001
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Junior Member
    Join Date
    Jan 2021
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    Sorry I tried my best.
    You understood correctly.

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    Do you have an algorithm for solving the problem? Did your instructor give you any hints for solving the problem?
    I do not have any ideas.
    You will need a design or algorithm before you can write a program
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Junior Member
    Join Date
    Jan 2021
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    Given a square two-dimensional array (the number of rows is equal to the number of columns (which contains Boolean values ​​-
    true / false.
    We will define: true region in the array (true region), as a maximum collection of adjacent cells that all have true value.
    Cells located diagonally to each other are not considered adjacent.
    For example, for the array on the right (here we mark the value true as 1 and the value false as 0 (, there are 3
    True areas and are marked in the array on the left:

    You must write a recursive method that accepts as a parameter a Boolean square matrix and returns some
    Different true regions exist in the matrix. If no true zones exist 0 will be returned.
    Note that the true region consists of at least one cell.
    The signature of the method is:
    public static int cntTrueReg (boolean [] [] mat)
    The method you write should be recursive with no use of loops at all. So is all
    The auxiliary methods you write (if you write) cannot contain loops.
    Overloading can be used and the array can be changed.

    --- Update ---

    This is the exact wording of the question(google translated)

    --- Update ---

    This is the exact wording of the question(google translated)

    --- Update ---

    This is the exact wording of the question(google translated)

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    Did your instructor give you any hints to help you solve the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Junior Member
    Join Date
    Jan 2021
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    Unfortunately no.

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello, I need to build a recursive method, without using loops, that will say how many groups of truth's there are

    Ok, this problem is about finding an algorithm to solve the problem. The algorithm has nothing to do with the java language. It is the steps the computer needs to take.
    You need to find the algorithm before trying the code in any language.
    Once you have the algorithm, you can try writing the code for it.
    If you have any specific questions about how to write java code for the algorithm, post them to get help.

    There are many more programmers on this site: http://www.coderanch.com/forums
    try asking the question there.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 4
    Last Post: December 9th, 2013, 10:40 AM
  2. How to create this recursive method.
    By exodus2041 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2013, 10:26 AM
  3. help with recursive method
    By mflb94 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2012, 06:30 PM
  4. Problem with recursive method. Can you help?
    By TFLeGacY in forum Algorithms & Recursion
    Replies: 6
    Last Post: December 7th, 2011, 05:44 PM
  5. [SOLVED] StackOverflowError with recursive method
    By samfin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 2nd, 2010, 04:05 PM