Some C programming recursion help is needed.

Status
Not open for further replies.

RoadRoller

Seniorius Lurkius
13
Hi guys. I'm new here.

I've joined these forums due to a recommendation from a friend of mine. He says that you guys here can offer some good programming repair work if guys like me are in trouble.

Anyways, the programming dilemma that I'm facing right now has to do with recursion in C. While I got a few programs working good with recursion, there are still a few others that are giving me trouble. Before I get down to providing details on the programs that I am having trouble with, can I count on all of your support? :scared:
 

RoadRoller

Seniorius Lurkius
13
Thanks. :) The first program that I've been working on involves counting "pixels" belonging to an object in a photograph. The program is designed to only count the pixels that make up the chosen object blob. My program involves the user entering the desired x and y coordinates for the picture cell. From there, the program will count each existing filled pixel cell that make up the object blob and afterwards, return the total number of filled cells. When a cell is counted, the value for the cell will be 0 in order to prevent multiple counts of the same cell. Pixel cells that make up the object blob are designated with the number "1".

Here is my C code thus far:

Code:
#include <stdio.h>

int blob_check(int [][], int, int);             /* Checks for blob in the grid */

int main(void)
{
    /* Declare variables */
    int picture[4][4],                          /* The picture grid */
        x_count,                                /* Counts x coordinates */
        y_count,                                /* Counts y coordinates */
        x_coord,                                /* Specified x coordinate */
        y_coord,                                /* Specified y coordinate */
        total_cells;                            /* The total number of cells that make up the picture blob */
    
    printf("\n");
    /* Populate picture grid with blobs */
    for (x_count = 0; x_count < 4; x_count++)
     for (y_count = 0; y_count < 4; y_count++)
     {
      printf("Enter 1 for pixel or any other key for blank: ");
      scanf("%d", &picture[x_count][y_count]);
      
      if (picture[x_count][y_count] == 1)
       picture[x_count][y_count] = 1;
      else
       picture[x_count][y_count] = 0;
     }
    
    /* Print picture grid */
    for (x_count = 0; x_count < 4; x_count++)
    {
     printf("\n");
     for (y_count = 0; y_count < 4; y_count++)
      printf("%d", picture[x_count][y_count]);
    }
    printf("\n");
    
    /* Prompt for coordinates to help determine the number of pixel cells that make up the blob */
    printf("\nEnter the x and y coordinates for the picture blob: ");
    scanf("%d %d", &x_coord, &y_coord);
    
    /* Count the cells within the coordinates that make up the picture blob */
    total_cells = blob_check(picture, x_coord, y_coord);
    
    /* Display the total */
    printf("\n%d cell(s) make up the picture blob.\n", total_cells);
    
    /* Print picture grid */
    for (x_count = 0; x_count < 4; x_count++)
    {
     printf("\n");
     for (y_count = 0; y_count < 4; y_count++)
      printf("%d", picture[y_count][x_count]);
    }
    printf("\n");
    
    return 0;   
}

int blob_check(int pic_grid[4][4], int x, int y)
{   
    /* Increment cell total for every successful instance of a blob */
    if (x > 3 || x < 0 || y > 3 || y < 0)
     return 0;
    else
    {
     if ((x > 0 && x < 4) && (y > 0 && y < 4))
     {
      if (pic_grid[x][y] == 1)
      {
       pic_grid[x][y] = 0;
       return 1 + blob_check(pic_grid, x, y);
      }
      if (pic_grid[x - 1][y + 1] == 1)
      {
       pic_grid[x][y] = pic_grid[x - 1][y + 1];
       return blob_check(pic_grid, x, y);
      }
      if (pic_grid[x][y + 1] == 1)
      {
       pic_grid[x][y] = pic_grid[x][y + 1];
       return blob_check(pic_grid, x, y);
      }
      if (pic_grid[x + 1][y + 1] == 1)
      {
       pic_grid[x][y] = pic_grid[x + 1][y + 1];
       return blob_check(pic_grid, x, y);
      }
      if (pic_grid[x - 1][y] == 1)
      {
       pic_grid[x][y] = pic_grid[x - 1][y];
       return blob_check(pic_grid, x, y);
      }
      if (pic_grid[x + 1][y] == 1)
      {
       pic_grid[x][y] = pic_grid[x + 1][y];
       return blob_check(pic_grid, x, y);
      }
      if (pic_grid[x - 1][y - 1] == 1)
      {
       pic_grid[x][y] = pic_grid[x - 1][y - 1];
       return blob_check(pic_grid, x, y);
      }
      if (pic_grid[x][y - 1] == 1)
      {
       pic_grid[x][y] = pic_grid[x][y - 1];
       return blob_check(pic_grid, x, y);
      }
      if (pic_grid[x + 1][y - 1] == 1)
      { 
       pic_grid[x][y] = pic_grid[x + 1][y - 1];
       return blob_check(pic_grid, x, y);
      }
      else
       return 0;
     }
     else
      return 0;
    }
}
The problem that I have been having with the program is that it keeps crashing and I'm just not sure how to fix it. Any help is appreciated. Thanks. :)
 
This line

if ((x > 0 && x < 4) && (y > 0 && y < 4))

is causing you to only check x and y for 1, 2 and 3. You never can test x[0] or y[0]. Also, as hamstro pointed out when looking at index 3 and doing 3+1 you are going to exceed the bounds of your array. Same for index 0 when checking cell 0-1 (once you fix your conditional to consider that case at least).

The most glaring issue is within blob_check() itself. You never update the values for x and y so your program never considers any cell beyond the one initially supplied. It just looks at that first cell a LOT of times before eventually running out of memory and crashing.

You also need to rethink your changes to pic_grid within that mass of if blocks (pic_grid[x][y] = pic_grid[x][y - 1]; pic_grid[x][y] = pic_grid[x + 1][y - 1]; etc...) along with what you are doing with the results from each call to blob_check(). A lot of the information is being found (or will be once you fix the recursive calling) then thrown away once it is returned and I don't think you are updating your pic_grid array in the way you are intending.
 

topham

Ars Legatus Legionis
13,756
Subscriptor
All the checks with 0, -1 and +1 can be readily converted to a double loop, with an expectation that the compiler will unroll it. I expect this is a homework assignment and has specific criteria the program is intended to teach, otherwise the real question is whether this is even a good algorithm to use.

(While I may bitch in other threads about people posting homework assignments, it looks to me like you already did most of the work)


Fixed minor typo
 

RoadRoller

Seniorius Lurkius
13
Hi guys.

Sorry to bump an old thread but after spending months on this program, it looks like I finally have it working. Here is the completed result.
Code:
#include <stdio.h>

int blob_check(int [][], int, int);           /* Checks for picture blob size in the grid */

int main(void)
{
    /* Declare variables */
    int grid[5][5],                           /* The picture grid */
        x_count,                              /* Counts the rows in the grid */
        y_count,                              /* Counts the columns in the grid */
        x_inp,                                /* The X co-ordinate entered by the user */
        y_inp,                                /* The Y co-ordinate entered by the user */
        blob_size;                            /* The cell number size of the picture blob */
    
    printf("\nEnter 1 to fill cell. Pressing any other key will leave cell empty.\n");
        
    /* Fill grid */
    for (y_count = 0; y_count < 5; y_count++)
     for (x_count = 0; x_count < 5; x_count++)
     {
      printf("\nX {%d} Y {%d}: ", x_count, y_count);
      scanf("%d", &grid[x_count][y_count]);
      
      if (grid[x_count][y_count] == 1)
       grid[x_count][y_count] = 1;
      else
       grid[x_count][y_count] = 0;
     }
    
    /* Display grid */
    for (y_count = 4; y_count >= 0; y_count--)
    {
     if (y_count == 2)
      printf("\n\ty  %d  ", y_count);
     else
      printf("\n\t   %d  ", y_count);
     for (x_count = 0; x_count < 5; x_count++)
      printf("%d ", grid[y_count][x_count]);
    }
    
    printf("\n\n\t      ");
    for (x_count = 0; x_count < 5; x_count++)
     printf("%d ", x_count);
    printf("\n\n\t          x\n");
    
    /* Get desired co-ordinates */
    printf("\nEnter X and Y co-ordinates for picture blob: ");
    scanf("%d%d", &y_inp, &x_inp);
    
    /* Count the size of the picture blob */
    blob_size = blob_check(grid, x_inp, y_inp);
    
    /* Display the size number */
    printf("\nThe picture blob has a cell size of %d\n", blob_size);
    
    return 0;   
}

int blob_check(int pic_grid[5][5], int x, int y)
{
    int total_size;                           /* The cell number size of the picture blob */
    
    /* Count size of picture blob */
    if (pic_grid[x][y] == 0 || (x < 0 || x > 4) || (y < 0 || y > 4))
     total_size = 0;
    else
    {
     pic_grid[x][y] = 0;
     total_size = 1 + blob_check(pic_grid, x, y) + blob_check(pic_grid, x + 1, y + 1) + blob_check(pic_grid, x - 1, y - 1) + 
     blob_check(pic_grid, x + 1, y) + blob_check(pic_grid, x - 1, y) + blob_check(pic_grid, x, y + 1) + blob_check(pic_grid, x, y - 1) + 
     blob_check(pic_grid, x + 1, y - 1) + blob_check(pic_grid, x - 1, y + 1);
    }
    
    return total_size;   
}
Still appreciate all of your help. :)
 
Status
Not open for further replies.