Home > Mobile >  Functions that checks the same characters in field
Functions that checks the same characters in field

Time:11-22

Hello how to make a function return true if all the characters in the columns are the same? I edit this post.. Thank you for your response.

She could look like this? Is it possible to change it?


for(int i =0; i <  rows; i  ){
   for(int j = 0; j < columns; j  ){
       if(field[i][j] == field[i][j]){
         return true;
        }
        else{
         return false;
        }

     }
   }


CodePudding user response:

  1. Use correct type for sizes (size_t)
  2. you need to decide if rows or cols are zero if it should return true or false.
bool check(const size_t rows, const size_t columns, const char field[rows][columns])
{
    //if(!field) return false;
    //if(!rows || !columns) return false;
    for(size_t row = 1; row < rows; row  )
        for(size_t col =  0; col < columns; col  )
            if(field[row][col] != field[0][col]) return false;
    return true;
}

CodePudding user response:

the function needs to return false as soon as it notices the difference.

for(int i = 0; i < rows; i  )
   for(int j = 1; j < columns; j  )
       if(field[0][i] != field[j][i])
         return false;
return true;
  • Related