简体   繁体   English

C: Function 当矩阵维度为奇数时,以矩阵为参数打印错误

[英]C: Function with matrix as argument prints out wrong when the matrix dimensions are odd numbers

so I created a function that has a matrix as an argument.所以我创建了一个以矩阵为参数的 function。 The matrix is made out of 0's and 1's.矩阵由 0 和 1 组成。 The objective of this function to read the matrix values, one by one and in case they are a 0 it prints out a "~", in case they are a 1 it prints "B" and in case they are a 0 next to a 1 horizontaly it prints a "]".此 function 的目的是逐个读取矩阵值,如果它们是 0,则打印出“~”,如果它们是 1,则打印“B”,如果它们是 0,则1水平它打印一个“]”。

Here's the function:这是 function:

void canvas(int m[t1][t2]){

    for (int i = t1 - 1; i > -1; i--)
    {
        for (int j = 0; j < t2; j++)
        {
            if (m[i][j] == 1){
                printf("B ");
            }
            else if (m[i][j] == 0 && (m[i][j + 1] == 1 || m[i][j - 1] == 1)){
                printf("] ");
            }
            else {
                printf("~ ");
            }
        }
        printf("\n");
    }
}

This is what the matrix used as an argument looks like (example of 13 x 13):这是用作参数的矩阵的样子(13 x 13 的示例):

0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 1 1 1 1 1 1 1 1 0 0 0 
0 1 0 0 0 0 0 0 0 1 0 0 0 
0 1 0 0 0 0 0 0 0 1 0 0 0 
0 1 0 0 0 0 0 0 0 1 0 0 0 
0 1 0 0 0 0 0 0 0 1 0 0 0 
0 1 0 0 0 0 0 0 0 1 0 0 0 
0 1 0 0 0 0 0 0 0 1 0 0 0 
0 1 0 0 0 0 0 0 0 1 0 0 0 
0 1 0 0 0 0 0 0 0 1 0 0 0 
0 1 0 0 0 0 0 0 0 1 0 0 0 
0 1 1 1 1 1 1 1 1 1 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 

This is the output I expected:这是我预期的 output:

~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 
] B B B B B B B B B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B B B B B B B B B ] ~ ~ 
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 

And this is what I'm getting:这就是我得到的:

~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ] 
] B B B B B B B B B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B ] ~ ~ ~ ~ ~ ] B ] ~ ~ 
] B B B B B B B B B ] ~ ~ 
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 

That last character on the first line always appears wrong when both dimensions of the matrix are odd numbers and I can't figure out why.当矩阵的两个维度都是奇数时,第一行的最后一个字符总是出现错误,我不知道为什么。 I think it might have to do with the logical expression but can't find a reason for this to be happening with the odd numbers.我认为这可能与逻辑表达式有关,但找不到奇数发生这种情况的原因。

When you do this check:当您执行此检查时:

else if (m[i][j] == 0 && (m[i][j + 1] == 1 || m[i][j - 1] == 1)){

You read off the end of the array when j is either 0 or t2-1 .j为 0 或t2-1时,您读取数组的末尾。 Doing so triggers undefined behavior这样做会触发未定义的行为

You need to do an additional check for these cases:您需要对这些情况进行额外检查:

else if (m[i][j] == 0 && ((j < t2-1 && m[i][j + 1] == 1 ) || 
                          (j > 0) && m[i][j - 1] == 1)) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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