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

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

Dear,

I am counting the number of islands in a given matrix and try to find the area of every island.
http://en.wikipedia.org/wiki/Connect…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?

Code:
```#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();
}```

2. I see that cross post Salem, and I raise you one:
Find The Number Of Islands In A Given Matrix And Area Of Every Island - C And C++ | Dream.In.Code

And the link still doesn't work.

3. ## Find The Number Of Islands In A Given Matrix And Area Of Every Island

Connected component (graph theory) - Wikipedia, the free encyclopedia

I see that cross post Salem, and I raise you one:
Find The Number Of Islands In A Given Matrix And Area Of Every Island - C And C++ | Dream.In.Code

And the link still doesn't work.

4. Originally Posted by tahir123

How about reading all of those, making some changes to the code and then posting back (presumably to all the forums you've cross-posted to already) what your new code is.

Or perhaps you're just hoping that if you post the same old rubbish to enough forums, one will eventually post a completely correct answer without you having to do any more work. If this is your idea, please leave.

5. Dear,
Now there is few errors in my code,why they give me TOO FEW ARGYMENTS error?
FOr example:
int count_connected_set(int (*)[5], int, int, int, int, int, int)':
invalid conversion from `int' to `int (*)[5]'
too few arguments to function `int countIslands(int (*)[5], int, int)'
int main()':

Code:
``` #include<stdio.h>
#include<conio.h>
#define ROW 5
#define COL 5

int count_connected_set(int A[][COL], int i, int j,int m,int n,int area,int count);
int countIslands(int A[][COL], int m, int n);
int area=0;
int i=1;
int j=1;
int count=0;
int count_connected_set(int A[][COL],int i,int j,int m,int n,int area,int count)
{
A[i][j] = -1;

{
if(A[i-1][j-1]==1 && i>0 && j>0 && i<=m && j<=n )     //NW

count_connected_set(A,i-1,j-1,m,n,area,count);
area += count_connected_set(A, i-1, j-1, m, n);

if(A[i-1][j]==1 && i>0 && j>0 && i<=m && j<=n)       //N

count_connected_set(A,i-1,j,m,n,area,count);
area += count_connected_set(A, i-1, j, m, n);

if(A[i-1][j+1]==1 && i>0 && j>0 && i<=m && j<=n)//   //NE

count_connected_set(A,i-1,j+1,m,n,area,count);
area += count_connected_set(A, i-1, j+1, m, n);

if(A[i][j-1]==1 && i>0 && j>0 && i<=m && j<=n)       //W

count_connected_set(A,i,j-1,m,n,area,count);
area += count_connected_set(A, i, j-1, m, n);

if(A[i][j+1]==1 && i>0 && j>0 && i<=m && j<=n)       //E

count_connected_set(A,i,j+1,m,n,area,count);
area += count_connected_set(A, i, j+1, m, n);

if(A[i+1][j-1]==1 && i>0 && j>0 && i<=m && j<=n)     //SW

count_connected_set(A,i+1,j-1,m,n,area,count);
area += count_connected_set(A, i+1, j-1, m, n);

if(A[i+1][j]==1 && i>0 && j>0 && i<=m && j<=n)       //S

count_connected_set(A,i+1,j,m,n,area,count);
area += count_connected_set(A, i+1, j, m, n);

if(A[i+1][j+1]==1 && i>0 && j>0 && i<=m && j<=n)     //SE

count_connected_set(A,i+1,j+1,m,n,area,count);
area += count_connected_set(A, i+1, j+1, m, n);

}

return area;
}
// end of count_connected_set()

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

for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
if(A[i][j] == -1)
A[i][j] = 1;

count_connected_set(A, i, j, m, n,area);
count++;

}
}
return count;
}

main()

{

int  i,j;
int m = ROW;
int n = COL;
int A[][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}
};

printf(" %d islands area is %d\n", count_connected_set(count),count_connected_set (area));

printf("Number of islands is: %d\n",countIslands(count));
getch();
}```
Originally Posted by Salem

How about reading all of those, making some changes to the code and then posting back (presumably to all the forums you've cross-posted to already) what your new code is.

Or perhaps you're just hoping that if you post the same old rubbish to enough forums, one will eventually post a completely correct answer without you having to do any more work. If this is your idea, please leave.

6. <BillyMays>Wait, there's more cross-posting
Find the number of islands in a given matrix and area of every island - GeeksforGeeks | GeeksforGeeks
Find the number of islands in a given ma - C++ Forum
</BillyMays>
If you have enjoyed this thread, you can revisit it any time you like on any other forum.

> Now there is few errors in my code,why they give me TOO FEW ARGYMENTS error?
My impression is that you didn't write the original code and that you haven't written the current code either.
It's just a mashup of random ideas you've found scattered across all the forums you've been spamming.

7. @salem bhai.....No i am try to solve the problem and posting to other forum only for getting more ideas for optimize the code.
Therefore I change the code due to errors.
OK,next time I will post only this forum .

8. if you declare a function having 7 arguments, then you must call it with 7, unless it can be overloaded

9. Thanks
but still `int count_connected_set(int (*)[5], int, int, int, int, int, int)'

But I want to call only area,how is it possible program give me area value ?

At
Originally Posted by rogster001
if you declare a function having 7 arguments, then you must call it with 7, unless it can be overloaded

10. is it correct way ?
printf(" %d islands area is %d\n", count_connected_set(A, i, j, m, n,area,count),count_connected_set(A, i, j, m, n,area,count));

printf("Number of islands is: %d\n",countIslands( A, m, n,count));

11. Originally Posted by tahir123
but still `int count_connected_set(int (*)[5], int, int, int, int, int, int)'

But I want to call only area,how is it possible program give me area value ?
You have written the program (haven't you?) so you should know what alle these int arguments should be and how to call the function.

Bye, Andreas

12. Originally Posted by tahir123
is it correct way ?
Does it compile?

If you get errors it can't be correct.

Bye, Andreas

13. @Andreas.........no still i got the error therefore i change the arguments.

14. I think we all knew this, but the OP did not write the code themselves. It looks like they took it from here: CODES: matrix.

@tahir123:
Do your own work, and you might stand a chance of actually understanding what is going wrong, and you just might be able to fix it. We also might be willing to help, if you actually tried anything other than plagiarism.