[英]Given a rectangular MATRIX(MxN) of integers, already charged, calculate how many are symmetric elements with respect to the vertical axis
i don't know what's wrong with my code .. if the MATRIX is formed by (2x4) and i give in input : 我不知道我的代码有什么问题..如果MATRIX由(2x4)组成并且我输入了:
(0x0):1
(0x1):2
(0x2):2
(0x3):1
(1x0):7
(1x1):4
(1x2):5
(1x3):7
symmetric elements should be :3 if im not wrong , but my code prints 0 elements 如果我没记错的话,对称元素应该是:3,但是我的代码显示0个元素
here's my code 这是我的代码
int elementiSimmetrici(int colonne <--(MAXcolumns OF matrix(matrixIS FORMED BY THIS COLUMNS) ,int matrix[][colonne]<--(matrix ),int righe <-//matrix IS FORMED BY THIS ROWS)
{
int r <--(row),c <--(columns),elementisimm=0 <--elements counter;
colonne/=2; //columns/2 because i have to compare only first with last
for(r=0;r<righe;r++){//for row<Maxrow's of matrix
for(c=0;c<colonne;c++){ //for columns <maxcolums of matrix
if ( matrix[r][c] == matrix[r][colonne-1-c] //i think you can understand this ){
elementisimm++; //increase COUNTER
}
}
}
return elementisimm;
}
You divide colonne
by 2. That's fine for the loop condition but not for the comparison. 用2除以
colonne
。这对于循环条件是好的,但对于比较而言却不是。 This should work: 这应该工作:
// Don't do this: colonne/=2;
for(c = 0; c < colonne / 2; c++){
if ( vettore[r][c] == vettore[r][colonne-1-c] ){
elementisimm++;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.