我撰寫了一個函式,其中將一維陣列 (int8_t tabelle1[768]) 轉換為二維陣列 (int8_t tabelle[24][32])。
我現在想在二維陣列中找到一個“熱點”。“熱點”是陣列中的一個 2x2 塊(可以位于任何位置),其中塊的每個值必須等于或大于 30。
我的問題是我的程式沒有進入我的 if 陳述句。未列印“檢查點”。
功能:
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;
}
uj5u.com熱心網友回復:
它永遠不會到達印刷品,因為您的許多returns 中的一個比您想象的要早得多地結束該功能。特別是在wich 中的那個包含列印else。
使用除錯器,你會看到。
或者在每個 旁邊放一個列印件,確保使用它。在呼叫顯示的函式之后還要列印。thenelse{}
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 */
}
}
uj5u.com熱心網友回復:
一些東西 ...
- 內核寬度/高度為 2,因此我們必須在陣列寬度/高度之外停止,否則我們將超出陣列的末尾
- 一旦我們找到匹配的元素,我們必須使用該元素作為 2x2 框的左上角錨點
這是重構的代碼(它可以編譯,但未經測驗):
#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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/487582.html
