Home > Mobile >  "If statement" whith array is not working
"If statement" whith array is not working

Time:06-04

I wrote a function where a 1-dimensional array (int8_t tabelle1[768]) is transferred into a 2-dimensional array (int8_t tabelle[24][32]).

I now want to find a "Hotspot" in the 2-dimensional array. A "Hotspot" is an 2x2 block in the array (can be located anywhere) where each value of the block has to be equal or over 30.

My Problem is now that my program is not going into my if statement. The "Checkpoint" is not being printed.

The function:

bool Hotspotberechnung(int8_t tabelle1[768],int8_t tabelle2[24][32])
{
    int i=0, j=0, x=0, y=0;

    for(j=0; j<24; j  )                             //Transfer from 1D into 2D
    {
        for(i=0; i<32; i  )
        {
            tabelle2[j][i] = tabelle1[(j*32) i];
        }
    }

    for(y=0; y<24; y  )                            //search for hotspot
    {
        for(x=0; x<32; x  )
        {
            if(tabelle2[y][x]>=30)
            {
                     printf ("1\n");       // checkpoint 
                if(tabelle2[y][x 1]>=30)
                {
                    if(tabelle2[y 1][x]>=30)
                    {
                        if(tabelle2[y 1][x 1]>=30)
                        {
                            for(j=0; j<24; j  )
                            {
                                for(i=0; i<32; i  )
                                {
                                    printf("%d.%d=%d\n",j 1,i 1,tabelle2[j][i]);
                                }
                            }
                            printf ("Hotspot!");    
                            return true;


                        }
                        else
                            return false;
                    }
                    else
                        return false;
                }
                else
                    return false;
            }
            else
                return false;
        }
    }

    return 0;
}

CodePudding user response:

It never reaches the print, because one of your many returns ends the function much sooner than you think. Specifically the one in the else of the then wich contains the print.
Use a debugger and you will see that.
Alternatively put a print next to each else, make sure to use {} for that. Also put a print after the call to the shown function.

for(y=0; y<24; y  )                            //search for hotspot
    {
        for(x=0; x<32; x  )
        {
            if(tabelle2[y][x]>=30)
            {
                /* this is not reached for y=0 and x=0, i.e. first */
                printf ("1\n");       // checkpoint 
                /* much code deleted */
            }
            else
                return false;
                /* this is reached first,
                   end of the function,
                   end of the loop,
                   no printing ever */
        }
    }

CodePudding user response:

A few things ...

  1. The kernal width/height is 2, so we have to stop short of the array width/height or we'll go beyond the end of the array
  2. Once we find an element that matches, we have to use that element as the upper left anchor of the 2x2 box

Here's the refactored code (it compiles, but is not tested):

#include <stdio.h>
#include <stdint.h>

#define YMAX    24
#define XMAX    32

#define BOXWID  2
#define MARG    (BOXWID - 1)

int
Hotspotberechnung(int8_t tabelle1[YMAX * XMAX], int8_t tabelle2[YMAX][XMAX])
{
    int i = 0,
        j = 0,
        x = 0,
        y = 0;

    // Transfer from 1D into 2D
    for (j = 0; j < YMAX; j  ) {
        for (i = 0; i < XMAX; i  )
            tabelle2[j][i] = tabelle1[(j * XMAX)   i];
    }

    for (y = 0; y < (YMAX - MARG); y  ) {
        int8_t *row = tabelle2[y];

        for (x = 0; x < (XMAX - MARG); x  ) {
            if (row[x] < 30)
                continue;

            int8_t *box = &row[x];
            int match = 1;

            for (int yoff = 0;  yoff < BOXWID;    yoff) {
                for (int xoff = 0;  xoff < BOXWID;    xoff) {
                    if (box[(yoff * XMAX)   xoff] < 30) {
                        match = 0;
                        break;
                    }
                }
                if (! match)
                    break;
            }

            if (! match)
                continue;

            for (int yoff = 0;  yoff < BOXWID;    yoff) {
                for (int xoff = 0;  xoff < BOXWID;    xoff) {
                    int8_t val = box[(yoff * XMAX)   xoff];
                    printf("[%d,%d] = %d\n",y   yoff,x   xoff,val);
                }
            }
        }
    }

    return 0;
}
  • Related