top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: Write a program to find clusters in a given M*N matrix ?

+1 vote
502 views
C: Write a program to find clusters in a given M*N matrix ?
posted Aug 5, 2014 by Harshita

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes

I think this question of yours is similar to the question have been asked plz check out the link mentioned below.. link to your question.

if the meaning of your question is same please check out the program written below.

Program:

#include<iostream>
#define k 6
void check(int arr[][k], int aux[][k], int i, int j)
{
     if(aux[i][j] == 1)
       return;
     aux[i][j] =1;
     if(i<0 || i>=k || j<0 || j>=k)
        return;
     for(int p=i-1; p<=i+1; p++)
     {
         for(int q = j-1; q<=j+1; q++)
         {
                if(arr[p][q] == 1)
                {
                    if(p==i && q ==j)
                        continue;
                     check(arr, aux, p, q);
                }
         }
     }
}

int main()
{
    int count =0;
    int arr[][k] = {{1, 0, 0, 1, 0, 0},
                    {0, 0, 1, 0, 1, 0},
                    {0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0},
                    {1, 1, 1, 0, 0, 0},
                    {1, 0, 1, 1, 0, 0}};

   int aux[k][k];

  for(int i=0; i<k; i++)
  {
      for(int j=0; j<k; j++)
           aux[i][j]=0;
  }
  for(int i=0; i<k; i++)
  {
    for(int j=0; j<k; j++)
    {
        if(arr[i][j] == 1 && aux[i][j] == 0)
        {
            count++;
            check(arr, aux, i, j);
        }
    }
  }

  cout<<"Number of clusters containing ones :" ;
  cout<<count<<endl;    
}

Output:
Number of clusters containing ones : 3

answer Aug 5, 2014 by Prakash Singh
Similar Questions
+3 votes

Given a 2d array, u need to sort the 2 diagonals independently.
And the elements in diagonal should remain in diagonal only.

INPUT =>
24 2 3 4 7

6 13 8 5 10

11 12 9 14 15

16 21 18 25 20

17 22 23 24 1

OUTPUT =>
1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

+6 votes

Given a matrix with 1s and 0s, please find the number of groups of 1s. A group is defined by horizontally or vertically adjacent 1s.

...