#Can't figure out to run the code

21 messages · Page 1 of 1 (latest)

full wolf
#

sudoko.c

#include "sudoku.h"

#define N 9

// Sudoku validation functions

// Function to check if the number exists in the row
int is_exist_row(int grid[N][N], int row, int num) 
{
    for (int col = 0; col < N; col++) 
    {
        if (grid[row][col] == num)
            return 1; // Number found in the row
    }
    return 0; // Number not found in the row
}

// Function to check if the number exists in the column
int is_exist_col(int grid[N][N], int col, int num) 
{
    for (int row = 0; row < N; row++) 
    {
        if (grid[row][col] == num)
            return 1; 
    }
    return 0; 
}

// Function to check if the number exists in the 3x3 box
int is_exist_box(int grid[N][N], int startRow, int startCol, int num) 
{
    for (int row = 0; row < 3; row++) 
    {
        for (int col = 0; col < 3; col++) 
        {
            if (grid[row + startRow][col + startCol] == num)
                return 1; 
        }
    }
    return 0; 
}

// Function to check if placing a number is valid
int is_safe_num(int grid[N][N], int row, int col, int num)
{
    return !is_exist_row(grid, row, num) &&
           !is_exist_col(grid, col, num) &&
           !is_exist_box(grid, row - row % 3, col - col % 3, num);
}

// Function to check if the Sudoku puzzle is valid
int is_valid_sudoku(int grid[N][N]) 
{
    for (int row = 0; row < N; row++) 
    {
        for (int col = 0; col < N; col++) 
        {
            int num = grid[row][col];
            if (num != 0) 
            {
                
                if (!is_safe_num(grid, row, col, num)) 
                {
                    return 0; 
                }
            }
        }
    }
    return 1;
}
tight magnetBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

full wolf
#

sudoku.h

#ifndef SUDOKU_H
#define SUDOKU_H

#define N 9

int is_exist_row(int grid[N][N], int row, int num);
int is_exist_col(int grid[N][N], int col, int num);
int is_exist_box(int grid[N][N], int startRow, int startCol, int num);
int is_safe_num(int grid[N][N], int row, int col, int num);
int is_valid_sudoku(int grid[N][N]);

#endif

main.c

#include <stdio.h>
#include "sudoku.h"

#define N 9


int main() {
    // Example of a Sudoku puzzle (0 represents empty spaces)
    int grid[N][N] = 
        {{5, 3, 0, 0, 7, 0, 0, 0, 0},
        {6, 0, 1, 1, 9, 5, 0, 0, 0},
        {0, 9, 8, 0, 0, 0, 0, 6, 0},
        {8, 0, 0, 0, 6, 0, 0, 0, 3},
        {4, 0, 0, 8, 0, 3, 0, 0, 1},
        {7, 0, 0, 0, 2, 0, 0, 0, 6},
        {1, 6, 0, 0, 0, 0, 2, 8, 0},
        {0, 0, 0, 4, 1, 9, 0, 0, 5},
        {0, 0, 0, 0, 8, 0, 0, 7, 9}};

    if (is_valid_sudoku(grid)) {
        printf("The Sudoku puzzle is valid.\n");
    } else {
        printf("The Sudoku puzzle is invalid.\n");
    }
    
    return 0;
}

I cant figure out a way to run the code, it keep giving error main.exe doesnt exists

finite sandal
#

How do you compile it (please don't say the word 'coderunner')

full wolf
#

f5 in vscode ;-;

#

gcc

finite sandal
#

F

full wolf
finite sandal
#

Either use the terminal to compile it manually or use a build system of sort

full wolf
#

how about codeblocks? lemme try in code blocks

finite sandal
#

If you're using your terminal
gcc main.c soduku.c -Wall -Wextra -Wpedantic -o your_exe_name should do the trick in this case

finite sandal
#

Yes

#

Or rather - the terminal window within vsc. Usually on windows thats powershell

full wolf
#

it says this

#

I type main.exe after -0

#

gcc main.c soduku.c -Wall -Wextra -Wpedantic -o main.exe

finite sandal
#

my bad sudoku.c. the name of your file basically

tight magnetBOT
#

@full wolf Has your question been resolved? If so, type !solved :)

full wolf
#

!solved