简体   繁体   中英

"If statement" is not working properly in c

I have written a program with a function that looks at a two dimensional array ir_data[60][768].

The While loop should call the "Hotspotberechnung" function only when there is a value that equals or is over 30 in a given row. "Hotspotberechnung" should store the given row in an array[24][32] and then print the array.

The problem is that the program prints an array without any values equal or over 30.

Unfortunately, I cannot find the mistake on my own.

The While Loop:

int8_t Temperatur[768] = {0};

while (get_ir_data( &Temperatur[0], sizeof(Temperatur)/sizeof(Temperatur[0])) == 0)
{
   for(int k=0; k<768; k++)
   {
       if(Temperatur[k] >= 30)
       {
          Hotspotberechnung(Temperatur,bild);
       }

    }
}

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++)                             //Tabellen werden gefüllt
        {
            for(i=0; i<32; i++)
            {
                tabelle2[j][i] = tabelle1[(j*24)+i];
            }
        }
        for(j=0; j<24; j++)
        {
            for(i=0; i<32; i++)
            {
                printf("%d.%d=%d\n",j,i,tabelle2[j][i]);
            }
        }

get_ir_data :

int8_t get_ir_data(int8_t* data, int n)
{
    static uint8_t idx = 0;

    if (n != 24 * 32) {
        printf("Invalid size of array data!\n");
        return -1;
    }

    memcpy(data, ir_data[idx], n);
    idx++;

    if (idx >= sizeof(ir_data) / sizeof(*ir_data))
        return -2;

    return 0;
}
tabelle1[(j*24)+i]

This index calculation is wrong. You need

tabelle1[(j*32)+i]

To verify, calculate the maximal value of (j*24)+i, given the ranges of j (0 to 23) and j (0 to 31). Is it 767?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM