samedi 27 juin 2020

Avoiding incrementing in if statement in C++

I would like to avoid incrementing and decrementing in if-statement since there is a segmentation fault error in the following code while checking conditions (if we start with p = 1 and k = 1 for example):

if (((heights[k--][p--] < heights[k][p]) || (heights[k--][p--] == heights[k][p])) &&
        ((heights[k--][p++] < heights[k][p]) || (heights[k--][p++] == heights[k][p])) &&
        ((heights[k++][p--] < heights[k][p]) || (heights[k++][p--] == heights[k][p])) &&
        ((heights[k++][p++] < heights[k][p]) || (heights[k++][p++] == heights[k][p]))){
                    width[k][p] = 3;
    }

For example, the second check fails with k = -1. I would like to check neighbouring elements of a two-dimensional array heights in an if-statement and than run some logic in case it was true.

How can I optimise it and generally rewrite it to make it look (and work) better? I haven't found any information on it.

Aucun commentaire:

Enregistrer un commentaire