# Find the number of islands in a given matrix and area of every island

Page 1 of 1

## 4 Replies - 837 Views - Last Post: 18 February 2013 - 04:09 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=312541&amp;s=3f4393f23c6fb8a4a97ca5d0818980f0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tahirfattani

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 17-February 13

# Find the number of islands in a given matrix and area of every island

Posted 17 February 2013 - 04:03 PM

Dear,

I am counting the number of islands in a given matrix and try to find the area of every island.
http://en.wikipedia....…raph_theory%29

Now finally remove the errors and my result is
1 islands area is 1
2 islands area is 1
3 islands area is 1
4 islands area is 1
5 islands area is 1
Number of islands is: 5

But I need a result according to my matrix is

1 islands area is 4
2 islands area is 3
3 islands area is 1
4 islands area is 1
5 islands area is 1
Number of islands is: 5

1-I am thinking that there is some problem for calling the area.

what do you think?

```#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include<conio.h>
#define ROW 5
#define COL 5
int markIsland(int arr[][COL], int i, int j, int m, int n,int area);
int countIslands(int arr[][COL], int m, int n);

int markIsland(int arr[][COL], int i, int j, int m, int n,int area)
{

// area=1;
{
arr[i][j] = -1;

if(i-1 >= 0)
{

if(j-1 >= 0 && arr[i-1][j-1] == 1)
markIsland(arr, i-1, j-1, m, n,area);

if(arr[i-1][j] == 1)
markIsland(arr, i-1, j, m, n,area);

if(j+1 < n && arr[i-1][j+1] == 1)
markIsland(arr, i-1, j+1, m, n,area);

}

if(i+1 < m)
{

if(j-1 >= 0 && arr[i+1][j-1] == 1)
markIsland(arr, i+1, j-1, m, n,area);

if(arr[i+1][j] == 1)
markIsland(arr, i+1, j, m, n,area);

if(j+1 < n && arr[i+1][j+1] == 1)
markIsland(arr, i+1, j+1, m, n,area);

}

if(j-1 >= 0 && arr[i][j-1] == 1)
markIsland(arr, i, j-1, m, n,area);

if(j+1 < n && arr[i][j+1] == 1)
markIsland(arr, i, j+1, m, n,area);

area++;

}
return area;
}

int countIslands(int arr[][COL], int m, int n)
{
int count = 0;

for(int i=0; i&lt;m; i++)
{
for(int j=0; j&lt;n; j++)

if(arr[i][j] == -1)
arr[i][j] = 1;

count++;

}

return count;
}

main()

{
int area=1,count=1;
int v, i,j;
int m = ROW;
int n = COL;
int arr[][COL]= { {1, 1, 0, 0, 0},
{0, 1, 0, 0, 1},
{1, 0, 0, 1, 1},
{0, 0, 0, 0, 0},
{1, 0, 1, 0, 1}
};
for(v=0;v&lt; countIslands(arr,m,n);v++)
{
printf(&quot; %d islands area is %d\n&quot;, count++,area);
// printf(&quot; %d islands area is %d\n&quot;, countIslands(arr,m,n),markIsland(arr,i,j,m,n,area) );
}
printf(&quot;Number of islands is: %d\n&quot;, countIslands(arr,m,n));

getch();
}

```

Is This A Good Question/Topic? 0

## Replies To: Find the number of islands in a given matrix and area of every island

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: Find the number of islands in a given matrix and area of every island

Posted 17 February 2013 - 08:16 PM

Your link doesn't work. Wiki says it doesn't have a page for that.

### #3 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: Find the number of islands in a given matrix and area of every island

Posted 17 February 2013 - 09:51 PM

You are calling countIslands again and again. The number of islands doesn't change, does it? You should call that function only once and assign the value it returns to a variable.

On the other hand, as things now stand you aren't calling markIsland at all. And if you did, what would be the values of i and j that you send it. You haven't assigned any initial values to i and j in your main function.

### #4 Salem_c

• void main'ers are DOOMED

Reputation: 2030
• Posts: 3,997
• Joined: 30-May 10

## Re: Find the number of islands in a given matrix and area of every island

Posted 18 February 2013 - 02:01 AM

Massive cross-poster (verging on spammer)
http://forums.devshe...and-940083.html
http://cboard.cprogr...ery-island.html
http://c-lovers.9678...td4023673.html#

Here's some medicine.
http://www.catb.org/...ions.html#forum
http://www.catb.org/...ons.html#urgent

### #5 tahirfattani

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 17-February 13

## Re: Find the number of islands in a given matrix and area of every island

Posted 18 February 2013 - 04:09 AM

Dear,
Here is the link.Thanks

http://en.wikipedia....nt_graph_theory