How do I avoid infinite loopings in an array?

Asked

Viewed 90 times

2

"Mount an F matrix of size n x n representing a forest. Each element of this matrix can have 3 values:

0 - if empty 1 - contains a tree 2 - contains fire

With probability p, given by the user, put a tree in each position of F (use Math.random()). Choose a tree to set on fire (change its value to 2).

Apply the following rule until there are no more items with a value of 2:

  • If there is a tree in position (i,j) and there is a fire in some neighboring position, change tree to fire.
  • If there is fire in a position (i,j) and there is no tree in the vicinity, change fire to void.

Determine the percentage of trees that survived."

I dare say I got it working. Here’s my code:

package aula10;
import java.util.*;
public class aula10_1 {
    public static void main(String[] args) {
        int n, i, j;
        double p;
        Scanner ent = new Scanner(System.in);
        ent.useLocale(Locale.US);
        System.out.println("Digite a dimensão da matriz (que é quadrada):");
        n = ent.nextInt();
        System.out.println("Digite a probabilidade de haver uma árvore em qualquer ponto da floresta:");
        p = ent.nextDouble();
        int [][] F = new int[n][n];
        Random rnd = new Random();        
        for (i=0; i<n; i++) {
            for (j=0; j<n; j++) {
                F[i][j] = rnd.nextInt();
                if (F[i][j] < p) {
                    F[i][j] = 1;
                } else {
                    F[i][j] = 0;
                }
                System.out.print(F[i][j]+" ");
            }
        }
        int k = rnd.nextInt(n-1);
        int l = rnd.nextInt(n-1);
        while (true) {
        if (F[k][l] == 1) {
            F[k][l] = 2;
            break;
        }
    }
        System.out.println(Arrays.deepToString(F));

    }

}

I tested it several times and went into infinite looping some. With Debug I was able to identify and correct some errors, but even so sometimes gives problem with small probabilities. I don’t know exactly what happens... My knowledge in Java is very limited, so I was satisfied with this code. But I still accept suggestions for improvement.

1 answer

1

Given a position i,j in the forest, the neighbors of that position are:

F[i-1][j-1]
F[i-1][j]
F[i-1][j+1]
F[i][j-1]
F[i][j+1]
F[i+1][j-1]
F[i+1][j]
F[i+1][j+1]

Every position you travel through the forest, that is, for every (i,j) within the for more internal (the for j), you have to check each of the neighbors by the conditions proposed in the problem (taking care not to check outside the forest area, ie limiting yourself to i-1 >=0, j-1 >= 0, i+1 < n and j+1 < n).

You must repeat these for i and for j as long as there are any trees on fire.

  • But then in case I should do is (i=-1; i>=0; i++) and the same for for for j? Or does this instruction enter into any if?

  • Create a function that receives x, y, valorASubstituir, valorNovo. Inside it test if x, y come out of the forest area, if they come out you do nothing but change the item of position x, y of valueSubstitute to valueNew.

  • I have not yet learned functions in the course, so I do not know if the teacher would accept a resolution using a resource that we have not learned in class. My knowledge in java is very limited and the classes are very trivial, but the exercises are monstrous (the opinion of all the students in the class). Still, thanks for your help.

  • In this case the way is to do without functions even. Before trying to change a position next to the current position, test with ifs if the neighbor’s coordinates leave the forest area.

  • I think I did, after days of working on it... it’s not perfect, much less practical, but I think it works. Feel free to check if it really worked and if the code can be improved. Thank you so much for your help! :)

  • Post the code to look. If the answer provided was satisfactory, do not forget to accept it as shown here.

  • I changed the code on the question itself. It is updated.

  • The problem is that sometimes it goes into infinite looping for some reason...

  • I don’t think this is how the program should work. You started well, with a for j within a for i to build the forest. But you need to run this double for chained to the other two steps of the program, which are to spread the fire and test if the fire has gone out. And the choice of the initial tree that will catch fire should not be made within a while as you did (fixing this will also avoid the infinite loop, which is due to the condition of the break never occur).

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.