简体   繁体   中英

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 :

(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

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. 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++;
      }
  }

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