Like Tree7Likes

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

This is a discussion on Find the number of islands in a given matrix and area of every island within the C Programming forums, part of the General Programming Boards category; Dear, I am counting the number of islands in a given matrix and try to find the area of every ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    10

    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?
    Please reply me





    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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,534
    Crossfire likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    Crossfire likes this.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    10

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

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

    what about the code?

    Quote Originally Posted by Adak View Post
    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.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,534
    Quote Originally Posted by tahir123 View Post
    what about the code?
    What about the code!?

    What about all the replies you've already got on other forums?
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    Dear,
    Thanks for ur reply,
    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();
          }
    Quote Originally Posted by Salem View Post
    What about the code!?

    What about all the replies you've already got on other forums?
    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.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,534
    <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.
    whiteflags likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    @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 .
    rogster001 likes this.

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    if you declare a function having 7 arguments, then you must call it with 7, unless it can be overloaded
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  10. #10
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    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
    Quote Originally Posted by rogster001 View Post
    if you declare a function having 7 arguments, then you must call it with 7, unless it can be overloaded

  11. #11
    Registered User
    Join Date
    Feb 2013
    Posts
    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));

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by tahir123 View Post
    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

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by tahir123 View Post
    is it correct way ?
    Does it compile?

    If you get errors it can't be correct.

    Bye, Andreas

  14. #14
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    @Andreas.........no still i got the error therefore i change the arguments.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,488
    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.
    Salem, TheBigH and tahir123 like this.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-21-2010, 12:09 PM
  2. help: function, find area of triangle
    By Niz in forum C Programming
    Replies: 20
    Last Post: 01-17-2007, 01:36 PM
  3. Hawaiian Islands
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 06-25-2004, 01:45 PM
  4. Whats wrong with my find Circle Area program?[compiles fine]
    By Golden Bunny in forum C++ Programming
    Replies: 22
    Last Post: 06-16-2002, 02:49 PM
  5. Island In The Sun
    By gnu-ehacks in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 11-27-2001, 08:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21