I am building a program to check soduku solutions in C, where the user inserts the size of the puzzle and the solution. The program needs to check whether the solution is valid or not using only 2d arrays.
My algorithm is to take every row, column, and box, put it in a 1d arrray (each), sort it and compare it to another 1d array going from 1-n.
I don't know why, but my program doesn't work and I need help.
My code:
/*-------------------------------------------------------------------------
Include files:
--------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/*=========================================================================
Constants and definitions:
==========================================================================*/
/* put your #defines and typedefs here*/
void printOpenMessageForSodokoSize();
void printOpenMessageForSodokoSolution();
void printValidSolution();
void printBadSolution();
int root(int a);
void bubbleSort(int arr[], int n);
int compareTo(int arr[], int n);
int checkRows(int n, int mat[][n]);
int checkColumns(int n, int mat[][n]);
int checkGrids(int n, int mat[][n], int sRow, int sCol);
/*-------------------------------------------------------------------------
The main program. (describe what your program does here)
-------------------------------------------------------------------------*/
int main()
{
printOpenMessageForSodokoSize();
int n;
int grids=1, rows=1, cols=1;
scanf("%d", &n);
while(root(n)<=0)
scanf("%d", &n);
printOpenMessageForSodokoSolution();
int mat[n][n];
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
scanf("%d", &mat[i][j]);
while(grids) //checking each sub-grid
{
for (int i=0;i<n;i+=root(n))
for (int j=0;j<n;j+=root(n))
grids=checkGrids(n, mat ,i, j);
}
rows=checkRows(n, mat);
cols=checkColumns(n, mat);
if ((rows==1)&&(cols==1)&&(grids==1))
printValidSolution();
else
printBadSolution();
return 0;
}
void printOpenMessageForSodokoSize()
{
printf("Please enter the size of your soduko:\n");
}
void printOpenMessageForSodokoSolution()
{
printf("Please enter your solution:\n");
}
void printValidSolution()
{
printf("Valid solution!");
}
void printBadSolution()
{
printf("Bad solution!");
}
int root(int a) //not using math library on purpose.
{
if (a==0)
return 0;
if (a<0)
return -1;
for(int i=1;i<=a;i++)
{
if (i*i==a)
return i;
}
return -1;
}
void bubbleSort(int arr[],int n) //sorting an array
{
int swap;
for (int i=0;i<(n-1);i++)
for (int j=0;j<n-i-1;j++)
{
if (arr[j] > arr[j+1])
{
swap=arr[j];
arr[j]=arr[j+1];
arr[j+1]=swap;
}
}
}
int compareTo(int arr[],int n) //check if 2 arrays are equal
{
int equal=1; //return 1 if equals 0 if not
int arr2[n]; //building the array to compare to
for(int i=0;i<n;i++)
arr2[i]=i+1;
while(equal)
{
for(int i=0;i<n;i++)
equal=(arr[i]==arr2[i]);
}
return equal;
}
int checkRows(int n, int mat[][n]) //checking the rows
{
int rows=1;
int arr[n];
while (rows)
{
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
arr[j]=mat[i][j]; //creating an array for each row
bubbleSort(arr, n); // sort it
rows=compareTo(arr, n); // compare it
}
}
return rows;
}
int checkColumns(int n, int mat[][n])
{
int columns=1;
int arr[n];
while (columns)
{
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
arr[j]=mat[j][i]; //creating an array for each column
bubbleSort(arr, n); // sort it
columns=compareTo(arr, n); //compare it
}
}
return columns;
}
int checkGrids(int n, int mat[][n], int sRow, int sCol)
{
int grids=1;
int arr[n];
int index=0;
for (int i=sRow;i<root(n);i++)
for (int j=sCol;j<root(n);j++)
{
arr[index]=mat[i][j]; //creating an array for each grid
index++;
}
bubbleSort(arr, n); // sort it
grids=compareTo(arr, n); //compare it
return grids;
}
printf
uses buffered output which means that anything you printf
does not appear on the terminal until a \\n
is printed or an fflush(stdout)
is executed.
Try the following:
void printValidSolution()
{
printf("Valid solution!\n"); // new line added
}
void printBadSolution()
{
printf("Bad solution!\n"); // new line added
}
Another problem
int compareTo(int arr[],int n) //check if 2 arrays are equal
{
int equal=1; //return 1 if equals 0 if not
int arr2[n]; //building the array to compare to
for(int i=0;i<n;i++)
arr2[i]=i+1;
while(equal)
{
for(int i=0;i<n;i++)
equal=(arr[i]==arr2[i]);
}
return equal;
}
This function will never terminate if arr[n - 1] == arr2[n - 1]
because there is nothing in the while
loop that changes the arrays, so the for
loop will always eventually set equal
to the result of the last comparison. Unfortunately, I'm not exactly sure what the function is supposed to do, so I can't tell you how to fix it.
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.