4 Replies - 417 Views - Last Post: 18 February 2013 - 04:09 AM Rate Topic: -----

#1 tahirfattani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?

Please reply me

#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

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1628
  • View blog
  • Posts: 3,090
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 tahirfattani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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


My link
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1