Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6768125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:02:11+00:00 2026-05-26T15:02:11+00:00

We’ve had a task today where we had to find all pixels which were

  • 0

We’ve had a task today where we had to find all pixels which were in connection with each other in a picture. The picture is simply green = 1, no color = 0, and can be represented with an array:

    int grid[5][5] = {
    {1, 1, 0, 0, 0},
    {0, 1, 1, 0, 0},
    {0, 0, 1, 0, 1},
    {1, 0, 0, 0, 1},
    {0, 1, 0, 1, 1},
};

We sat for 3 hours in my group and tried to solve this (first lecture about recursion), but we simply cannot get it to work. The issue seems to be that the functions somehow accepts being outside the grid. Any who, here’s the code:

#include <stdio.h>
#include <string.h>

//Prototype
int blob_count(int y, int x, int gridCopy[][5], int sum);

int blob_count(int y, int x, int gridCopy[][5], int sum){

   //Local vars
    int posX, posY;
    printf("\n\nX: %d - Y: %d\nposX: %d - posY: %d\nSum: %d", x, y, posX, posY, sum);
    printf("\n\n{%d - %d - %d - %d - %d}\n{%d - %d - %d - %d - %d}\n{%d - %d - %d - %d - %d}\n{%d - %d - %d - %d - %d}\n{%d - %d - %d - %d - %d}\n",
           gridCopy[0][0],
           gridCopy[0][1],
           gridCopy[0][2],
           gridCopy[0][3],
           gridCopy[0][4],

           gridCopy[1][0],
           gridCopy[1][1],
           gridCopy[1][2],
           gridCopy[1][3],
           gridCopy[1][4],

           gridCopy[2][0],
           gridCopy[2][1],
           gridCopy[2][2],
           gridCopy[2][3],
           gridCopy[2][4],

           gridCopy[3][0],
           gridCopy[3][1],
           gridCopy[3][2],
           gridCopy[3][3],
           gridCopy[3][4],

           gridCopy[4][0],
           gridCopy[4][1],
           gridCopy[4][2],
           gridCopy[4][3],
           gridCopy[4][4]); 

    //Find the position 1 behind and 1 above the starting point, and start the loop there
    for(posX = -1;posX <=1; posX++){
        for(posY = -1; posY <= 1; posY++){
            if((y + posY) >= 0 && (x + posX) >= 0){
                if((y + posY) <= 5 && (x + posX) <= 5){
                    if(gridCopy[posY+y][posX+x] == 1){
                        //Set the starting point to 0 (so it wont get calculated again)
                        gridCopy[posY+y][posX+x] = 0;



                        y = posY+y;
                        x = posX+x;

                        sum++;
                        blob_count(y, x, gridCopy, sum);
                    }
                }
            }
        }
    }

    return sum;



}

int main (void)
{

    int grid[5][5] = {
        {1, 1, 0, 0, 0},
        {0, 1, 1, 0, 0},
        {0, 0, 1, 0, 1},
        {1, 0, 0, 0, 1},
        {0, 1, 0, 1, 1},
    };

    //Create a manipulateable grid
    int gridCopy[5][5];
    memcpy(gridCopy, grid, 5*5*sizeof(int));

    //Start Positions
    int start_x = 0;
    int start_y = 3;

    //If the starting point selected, is not 0, initiate the function:
    if(grid[start_y][start_x] != 0){
        printf("\nSum: %d", blob_count(start_y ,start_x, gridCopy, 0));
    }




}

Edit:

I have updated the code above, and now I have a new problem, it gets the right sum (ammount of pixels in conjunction of each other), however when I print out the sum (in the main();) it says 1, why doesn’t it carry over from the recursive function to the main function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:02:12+00:00Added an answer on May 26, 2026 at 3:02 pm

    You look at all the neighbours of the current pixel:

    for(posX = -1;posX<=1;posX++){
        for(posY = -1; posY<= 1;posY++){
    

    And you check that the coordinates are positive:

            if((y + posY) >= 0 && (x + posX) >= 0){
    

    But you don’t check that the coordinates don’t overflow:

            if((y + posY) < 5 && (x + posX) < 5) {
    

    And access the undefined data outside the grid instead:

                if(gridCopy[posY+y][posX+x] == 1){
    

    It probably still won’t work though, but since this is homework I’ll just give you some tips: you’re ignoring the return value of the recursive call (don’t do that) and you don’t initialize sum (turn on the compiler warnings to get notified of that one). You also don’t copy the complete grid: memcpy(gridCopy, grid, 5*5); copies 25 bytes, not 25 ints. Use memcpy(gridCopy, grid, 5 * 5 * sizeof(int)); instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like to run a str_replace or preg_replace which looks for certain words
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.