GroupNo7-sudoku.c
Anonymous
c_cpp
02/08/2026 12:26 PM
8.4 KB
19
Indexable
#include "sudoku.h" //do not remove or replace
#include <stdio.h>
/*
Names of Contributing Members:
LATEO, Johann Raenen
MALLARI, Jared Marco
VALENCIA, Eunice
YU, Heather
Section: S11A
*/
/* Tasks : Implement each and every function indicated
in sudoku.h. You are not allowed to modify
the parameter list or the return type. Other
constraints and requirements are indicated in
the instructions in the pdf file.
Complete the main program based on the
comments indicated.
*/
/* This function returns 1 if the parameter is within the range of 1 to 9,
inclusive. Otherwise, the function returns 0. */
int isInRange(int num)
{
return num >= 1 && num <= 9;
}
/* This function gets input for the 9 x 9 2D array. The array then
represents the solution of the user to a Sudoku puzzle. part of the solution
for this function is call/s to isInRange() function to check that each input
is valid. The input is taken in row-wise.
Do NOt put any printf() statements here. */
void getInput(int aMatrix[][SIZE])
{
int row, col;
/* you are NOT allowed to declare additional variables */
for (row = 0; row < SIZE; row++)
{
for (col = 0; col < SIZE; col++)
{
do
{
scanf(" %d", &aMatrix[row][col]);
} while (!isInRange(aMatrix[row][col]));
}
}
}
/* This function displays the values in the array, which represents 1 row of
values.*/
void displayRow(int aRow[])
{
int col;
for (col = 0; col < SIZE; col++)
{
printf("%d", aRow[col]);
if (col == SIZE - 1)
printf("\n");
else
{
printf(" ");
if(col == 2 || col == 5)
printf(" ");
}
}
}
/* This function displays the values of the 2D array, row-wise. Showing the
entries as a table. Part of the solution for this function is call/s to
function displayRow().*/
void displayAll(int aMatrix[][SIZE])
{
int row;
for (row = 0; row < SIZE; row++)
{
displayRow(aMatrix[row]);
if (row == 2 || row == 5)
printf("\n");
}
}
/*This function returns 1 when the 1-D array aData, contains all the values
1 to 9. Otherwise, this function returns 0. */
int checkrow(int aData[])
{
int col;
int check[SIZE] = {0};
int val;
int valid = 1;
for (col = 0; col < SIZE && valid == 1; col++) {
val = aData[col];
if (!isInRange(val) || check[val - 1] == 1)
valid = 0;
else
check[val - 1] = 1;
}
return valid;
}
/* This function returns 1 when the column nColInd of the 2D array aMatrix
contains all the values 1 to 9. Otherwise, this function returns 0. That is,
this function only returns the result of checking 1 column, where the column
being checked is the one indicated by nColInd (representing column index) */
int checkcol(int aMatrix[][SIZE], int nColInd)
{
int row;
int check[SIZE] = {0};
int val;
int valid = 1;
for (row = 0; row < SIZE && valid == 1; row++)
{
val = aMatrix[row][nColInd];
if (!isInRange(val) || check[val - 1] == 1)
valid = 0;
else
check[val - 1] = 1;
}
return valid;
}
/* This function returns 1 when the 3 x 3 box starting at row index nRow and
the column index nCol of the 2D array aMatrix contains all the values 1 to
9. Otherwise, this function returns 0. That is, this function only returns
the result of checking one of the 3 x 3 box. For example, if nRow is 3 and
nCol is 6, this function checks the if 1 to 9 are in [3,6], [3,7], [3,8],
[4,6], [4,7], [4,8], [5,6], [5,7], [5,8]. */
int checkbox(int aMatrix[][SIZE], int nRow, int nCol)
{
int row, col;
int check[SIZE] = {0};
int val;
int valid = 1;
for (row = nRow; row < nRow + 3 && valid == 1; row++)
for (col = nCol; col < nCol + 3 && valid == 1; col++)
{
val = aMatrix[row][col];
if (!isInRange(val) || check[val - 1] == 1)
valid = 0;
else
check[val - 1] = 1;
}
return valid;
}
int
main()
{ int aMatrix[SIZE][SIZE];
/* you can add your own variables */
int row, col;
int valid = 1;
/* call getInput(). It is encouraged that you use
input redirection, so you do not need to keep
typing a set of values. Modify file containing
the input values to consider other test cases OR
create other files of input values per test case.
*/
getInput(aMatrix);
/* call displayAll() to verify that you got the inputs
correctly. Note the expected format of the displayed
result.
*/
displayAll(aMatrix);
/* call function checkrow() multiple times in loop.
Note that checkrow() will check one row at a time.
*/
for (row = 0; row < SIZE && valid == 1; row++)
{
if (!checkrow(aMatrix[row]))
{
//printf("Wrong Solution: error in row %d\n", row + 1);
valid = 0;
}
}
/* write your condition to execute the following only
if all rows are still correct. Call function
checkcol() mutiple times in a loop. checkcol() will
check one column at a time.
*/
for (col = 0; col < SIZE && valid == 1; col++)
{
if (!checkcol(aMatrix, col))
{
//printf("Wrong Solution: error in column %d\n", col + 1);
valid = 0;
}
}
/* write your condition to execute the following only
if all rows and columns are still correct. Call
function checkbox() mutiple times as the function will
check one 3x3 box at a time.
*/
for (row = 0; row < SIZE && valid == 1; row += 3)
{
for (col = 0; col < SIZE && valid == 1; col += 3)
{
if (!checkbox(aMatrix, row, col))
{
//printf("Wrong Solution: error in box starting at (%d,%d)\n", row + 1, col + 1);
valid = 0;
}
}
}
/* if the matrix is a correctly solved Sudoku puzzle,
display "Sudoku!". Otherwise display "Wrong Solution".
While debugging, it is suggested that you include
in the display which row or column or box the
wrong solution was [first] detected.
*/
if (valid == 1)
printf("Sudoku!\n");
else
printf("Wrong Solution\n");
return 0;
}
Editor is loading...
Leave a Comment