Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the function: use Java and add comments to the program static int[] cou

ID: 3736784 • Letter: C

Question

Complete the function: use Java and add comments to the program

static int[] countGroups(int[][] m, int[] t)

{

}

For this question, you will be tinding groups ot adjacent cells in a two-dimensional array and determining how many groups of each specified size exist. As part of the input, you will be provided with a two-dimensional array where each cell has a value of either 0 or 1. Cells are considered to be in the same group if they both have a value of 1 and they are adjacent an adjacent cell is directly to the left, to the nght, up or down; diagonal is not considered Qdjacent lo define it strictly: any two cells M y) and 2 y2 fall into the same group f X, X2 y, y 7 and both cells have a value of 1 The group-size of a group is the number of cells in tht roup. Complete the countGroups function in your editor. It has 2 parameters: 1. An n x n two-dimensional array ot integers, m, where the value of each element miy (where j

Explanation / Answer

CODE:

class FindGroups

{

       

                static boolean is_visited[][];

                static int count = 0;

                public static void main(String args[])

                {

                                int row, col, no_of_t;

                                int m[][], t[];

                                Scanner in = new Scanner(System.in);

                                //Get no of rows

                                System.out.println("Enter no of rows : ");

                                row = in.nextInt();

                             

                                System.out.println("Enter no of cols : ");

                                col = in.nextInt();

                           

                                m = new int[row][col];

                                is_visited = new boolean[row][col];

                            

                                System.out.println("Enter elements : ");

                                for(int i=0; i<row; i++) {

                                                for(int j=0; j<col; j++) {

                                                                m[i][j] = in.nextInt();

                                                                is_visited[i][j] = false;

                                                }

                              }

                              

                                System.out.println("Enter no of t : ");

                                no_of_t = in.nextInt();

                             

                                t = new int[no_of_t];

                             

                                System.out.println("Enter no of groups : ");

                                for(int i=0; i<no_of_t; i++) {

                                                t[i] = in.nextInt();

                              }

                            

                                System.out.println("-------------------------");

                                System.out.println("Input :: ");

                                for(int i=0; i<row; i++) {

                                                for(int j=0; j<col; j++) {

                                                                System.out.print(m[i][j] + " ");

                                                }

                                                System.out.println("");

                                }

                              

                                int[] count_groups = countGroups(m, t);

                              

                                System.out.println(" -------------------------");

                                System.out.println("Output :: ");

                                for(int k=0; k<no_of_t; k++) {

                                                System.out.println(t[k] + " : " + count_groups[k]);

                                }

                              

                }

              

                static void isConnected( int numb, int m[][], int y, int x, boolean is_visited[][]) {

                                if(y != 0) {

                                                if ( m[y-1][x] == numb && !is_visited[y-1][x]) {

                                                                is_visited[y-1][x] = true;

                                                                count++;

                                                                isConnected( numb, m, y-1, x, is_visited);

                                                }

                                }

                                if(y < (m.length - 1)) {

                                                if ( m[y+1][x] == numb && !is_visited[y+1][x]) {

                                                                is_visited[y+1][x] = true;

                                                                count++;

                                                                isConnected( numb, m, y+1, x, is_visited);

                                                }

                                }

                                if(x != 0) {

                                                if ( m[y][x-1] == numb && !is_visited[y][x-1]) {

                                                                is_visited[y][x-1] = true;

                                                                count++;

                                                                isConnected( numb, m, y, x-1, is_visited);

                                                }

                                }

                                if(x < (m.length - 1)) {

                                                if ( m[y][x+1] == numb && !is_visited[y][x+1]) {

                                                                is_visited[y][x+1] = true;

                                                                count++;

                                                                isConnected( numb, m, y, x+1, is_visited);

                                                }

                                }

                }

              

                static int[] countGroups(int[][] m, int[] t) {

                                int res_len = t.length;

                                int ret[] = new int[res_len];

                              

                                for(int i=0; i<m.length; i++) {

                                                for(int j=0; j<m[0].length; j++) {

                                                                if(!is_visited[i][j]) {

                                                                                count = 0;

                                                                                isConnected( m[i][j], m, i, j, is_visited);

                                                                                if(count == 0)

                                                                                                count = 1;

                                                                              

                                                                                for(int k=0; k<res_len; k++) {

                                                                                                if(t[k] == count)

                                                                                                                ret[k]++;

                                                                                }

                                                                }

                                                }

                                }

                              

                                return ret;

                }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote