Comments on: Islands in a two dimensional array https://demo.ritambhara.in/islands-in-a-two-dimensional-array/ Coding / System Design Interviews Fri, 09 Mar 2018 18:39:35 +0000 hourly 1 https://wordpress.org/?v=6.7.2 By: ABC https://demo.ritambhara.in/islands-in-a-two-dimensional-array/#comment-1935 Fri, 09 Mar 2018 18:39:35 +0000 http://www.ritambhara.in/?p=2407#comment-1935 Thank you so much for the code. The logic is very lucid and effective. Beautifully done. 😀

]]>
By: TekleSadik Tadesse https://demo.ritambhara.in/islands-in-a-two-dimensional-array/#comment-1934 Thu, 06 Aug 2015 14:36:11 +0000 http://www.ritambhara.in/?p=2407#comment-1934 Thank you! but i want a C program that display a snail by the arrangement of numbers. like:-7 8 9
6 1 2
5 4 3

]]>
By: Muhammad Tahir https://demo.ritambhara.in/islands-in-a-two-dimensional-array/#comment-1933 Fri, 15 Feb 2013 17:36:28 +0000 http://www.ritambhara.in/?p=2407#comment-1933 My program is compiling but i cannot got the accourate result because i think so there is some mistake for returining the function value,Please correct it and send back to me.
#include
#include
#include
#include
#include
#define ROW 10
#define COL 10
int markIsland(int M[][COL], int i, int j, int m, int n);
int countIslands(int M[][COL], int m, int n);
int markIsland(int M[][COL], int i, int j, int m, int n)
{
M[i][j] = -1;
if(i-1 >= 0)
{
if(j-1 >= 0 && M[i-1][j-1] == 1)
markIsland(M, i-1, j-1, m, n);
if(M[i-1][j] == 1)
markIsland(M, i-1, j, m, n);
if(j+1 < n && M[i-1][j+1] == 1)
markIsland(M, i-1, j+1, m, n);
}
if(i+1 = 0 && M[i+1][j-1] == 1)
markIsland(M, i+1, j-1, m, n);
if(M[i+1][j] == 1)
markIsland(M, i+1, j, m, n);
if(j+1 = 0 && M[i][j-1] == 1)
markIsland(M, i, j-1, m, n);
if(j+1 < n && M[i][j+1] == 1)
markIsland(M, i, j+1, m, n);
}
int countIslands(int M[][COL], int m, int n)
{
int count = 0;
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
if(M[i][j] == -1)
M[i][j] = 1;
{
count++;
}
return count;
}
int main()
{
int m, i,j,n, c, d,f, matrix[10][10];
m = ROW;
n = COL;
srand(time(NULL));
printf("Enter the number of rows and columns of matrix\n ");
for( i = 0 ; i < m ; i++ )
{
for( j = 0 ; j < n ; j++ )
{
printf("%d\t",rand() % 2);
}
}
printf("Number of islands is: %d ", countIslands(matrix, m, n));
getch();
}

]]>