PI-ITBA / 2024_01

8 stars 0 forks source link

TP6_ej20 #128

Closed AugustoOspal closed 3 months ago

AugustoOspal commented 6 months ago

Buenas tardes, tengo una consulta sobre este warning. Sale porque la matriz es char y el parámetro de la función es const char o por otro motivo? Saludos y gracias.

tp06_ej20_test.c:125:23: warning: pointers to arrays with different qualifiers are incompatible in ISO C [-Wpedantic] 125 | assert(sudokuSolver(sudoku)==0); | ^~ tp06_ej20_test.c:139:23: warning: pointers to arrays with different qualifiers are incompatible in ISO C [-Wpedantic] 139 | assert(sudokuSolver(sudoku2)==1); | ^~~ tp06_ej20_test.c:153:23: warning: pointers to arrays with different qualifiers are incompatible in ISO C [-Wpedantic] 153 | assert(sudokuSolver(sudoku3)==0); | ^~~ tp06_ej20_test.c:169:23: warning: pointers to arrays with different qualifiers are incompatible in ISO C [-Wpedantic] 169 | assert(sudokuSolver(sudoku4)==0); | ^~~ tp06_ej20_test.c:183:23: warning: pointers to arrays with different qualifiers are incompatible in ISO C [-Wpedantic] 183 | assert(sudokuSolver(sudoku5)==0); | ^~~

#include <stdio.h>
#include <assert.h>

#define DIM             9
#define DIM_BLOCK       3
#define LOWEST_NUM      1
#define GREATEST_NUM    9
#define SODOKU_NUMBERS  9

#define PADDING         DIM_BLOCK / 2 

int checkRow(const char row[])
{
    int numPosition;
    char counter[SODOKU_NUMBERS] = {0};
    for (int i = 0; i < DIM; i++)
    {
        if (row[i] > GREATEST_NUM || row[i] < LOWEST_NUM)
            return 0;

        numPosition = row[i] - 1;
        counter[numPosition]++;

        if (counter[numPosition] > 1)
            return 0;
    }

    return 1;
}

int checkRows(const char sodoku[DIM][DIM])
{
    int flag = 1;

    for (int i = 0; i < DIM && flag; i++)        
      flag = checkRow(sodoku[i]);

    return flag;
}

int checkCol(const char sodoku[DIM][DIM], int col)
{
    int numPosition;
    char counter[SODOKU_NUMBERS] = {0};

    for (int i = 0; i < DIM; i++)
    {
        if (sodoku[i][col] < LOWEST_NUM || sodoku[i][col] > GREATEST_NUM)
            return 0;

        numPosition = sodoku[i][col] - 1;
        counter[numPosition]++;

        if (counter[numPosition] > 1)
            return 0;
    }

    return 1;
}

int checkCols(const char sodoku[DIM][DIM])
{
    int flag = 1;

    for (int i = 0; i < DIM && flag; i++)
    {
        flag = checkCol(sodoku, i);
    }

    return flag;
}

int checkBlock(const char sodoku[DIM][DIM], const int row, const int col)
{
    int numPosition;
    char counter[SODOKU_NUMBERS] = {0};

    for (int i = row - PADDING; i < row + PADDING; i++)
    {
        for (int j = col - PADDING; j < col + PADDING; j++)
        {
            /*
             * No hace falta chequear que
             * sea >= lowest y <= gretest
             * porque con los otros chequeos
             * ya se verifica
             */

            numPosition = sodoku[i][j] - 1;
            counter[numPosition]++;

            if (counter[numPosition] > 1)
                return 0;
        }

    }

    return 1;
}

int checkBlocks(const char sodoku[DIM][DIM])
{
    int flag = 1;

    for (int i = PADDING; i < DIM; i+= DIM_BLOCK)
    {
        for (int j = PADDING; j < DIM; j += DIM_BLOCK)
        {
            flag = checkBlock(sodoku, i, j);
        }
    }

    return flag;
}

int sudokuSolver(const char m[DIM][DIM])
{
    return checkRows(m) && checkCols(m) && checkBlocks(m);
}

int main(void) {
  // Una matriz vacía no es solución
  char sudoku[DIM][DIM] = {{0}};

  assert(sudokuSolver(sudoku)==0);

  char sudoku2[DIM][DIM] = {
        {3,8,1,9,7,6,5,4,2}, 
        {2,4,7,5,3,8,1,9,6},
        {5,6,9,2,1,4,8,7,3},
        {6,7,4,8,5,2,3,1,9},
        {1,3,5,7,4,9,6,2,8},
        {9,2,8,1,6,3,7,5,4},
        {4,1,2,6,8,5,9,3,7},
        {7,9,6,3,2,1,4,8,5},
        {8,5,3,4,9,7,2,6,1}
  };

  assert(sudokuSolver(sudoku2)==1);

  char sudoku3[DIM][DIM] = {
        {2,8,1,9,7,6,5,4,2}, 
        {3,4,7,5,3,8,1,9,6},
        {5,6,9,2,1,4,8,7,3},
        {6,7,4,8,5,2,3,1,9},
        {1,3,5,7,4,9,6,2,8},
        {9,2,8,1,6,3,7,5,4},
        {4,1,2,6,8,5,9,3,7},
        {7,9,6,3,2,1,4,8,5},
        {8,5,3,4,9,7,2,6,1}
  };

  assert(sudokuSolver(sudoku3)==0);

  // Cuando se tomó este ejercicio en un parcial, en una de las respuestas
  // sólo chequeaban que la suma de cada fila, columna y cuadrado fuera 45
  char sudoku4[DIM][DIM] = {
        {5,5,5,5,5,5,5,5,5}, 
        {5,5,5,5,5,5,5,5,5}, 
        {5,5,5,5,5,5,5,5,5}, 
        {5,5,5,5,5,5,5,5,5}, 
        {5,5,5,5,5,5,5,5,5}, 
        {5,5,5,5,5,5,5,5,5}, 
        {5,5,5,5,5,5,5,5,5}, 
        {5,5,5,5,5,5,5,5,5}, 
        {5,5,5,5,5,5,5,5,5}
  };

  assert(sudokuSolver(sudoku4)==0);

  char sudoku5[DIM][DIM] = {
        {3,8,1,9,7,6,5,4,12}, 
        {2,4,7,5,3,8,1,9,6},
        {5,6,9,2,1,4,8,7,3},
        {6,7,4,8,5,2,3,1,9},
        {1,3,5,7,4,9,6,2,8},
        {9,2,8,1,6,3,7,5,4},
        {4,1,2,6,8,5,9,3,7},
        {7,9,6,3,2,1,4,8,5},
        {8,5,3,4,9,7,2,6,1}
  };

  assert(sudokuSolver(sudoku5)==0);

  puts("OK!");

  return 0;
}
ImNotGone commented 6 months ago

Es por lo que mencionas

AugustoOspal commented 6 months ago

Perfecto, gracias!