Question
I need to check if the sudo puzzle for grid 1 and grid 2 is valid or invalid. I am having problems trying to access
I need to check if the sudo puzzle for grid 1 and grid 2 is valid or invalid. I am having problems trying to access the name of this file through argv[1] in your main() function . the first grid1 should come up valid , the second grid should come up invalid. How to you get it to read both txt files grid 1 and grid 2 without hardcoding. So executing hw2.c but checking each grid seperatedly. Theres hw2.c grid 1 and grid 2. We are checking to see if grid 1 is and grid 2 is valid but it should check sepeartely . So if you type ./hw2 grid1
ouput : sudo code is valid or invalid should come up
and if you type ./hw2 grid2 sudo code valid or invalid
I gave an code i try below, but it is does it hardcode? Plese help , also explain the comand steps to execute?
#include
#include
#include
#define NUMBER_OF_THREADS 11
#define PUZZLE_SIZE 9
void *column_worker(void *param); /* thread that checks columns */
void *row_worker(void *param); /* thread that checks rows */
void *subfield_worker(void *param); /* thread that checks subfields */
/* example puzzle */
int puzzle[PUZZLE_SIZE+1][PUZZLE_SIZE+1] = {
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,5,3,4,6,7,8,9,1,2},
{-1,6,7,2,1,9,5,3,4,8},
{-1,1,9,8,3,4,2,5,6,7},
{-1,8,5,9,7,6,1,4,2,3},
{-1,4,2,6,8,5,3,7,9,1},
{-1,7,1,3,9,2,4,8,5,6},
{-1,9,6,1,5,3,7,2,8,4},
{-1,2,8,7,4,1,9,6,3,5},
{-1,3,4,5,2,8,6,1,7,9}
};
int status_map[NUMBER_OF_THREADS] = {0};
/* data structure for passing data to threads */
typedef struct
{
int thread_number;
int x;
int y;
} parameters;
int main(int argc, char *argv[])
{
int i,j;
int count = 0;
int rv = 1;
pthread_t workers[NUMBER_OF_THREADS];
parameters *data = (parameters *) malloc(sizeof(parameters));
data->thread_number = count;
data->x = 1;
data->y = 1;
pthread_create(&workers[data->thread_number], 0, row_worker, data);
count++;
data = (parameters *) malloc(sizeof(parameters));
data->thread_number = count;
data->x = 1;
data->y = 1;
pthread_create(&workers[data->thread_number], 0, column_worker, data);
count++;
/* create the threads that check each subfield */
for (i = 1; i
for (j = 1; j
data = (parameters *) malloc(sizeof(parameters));
data->thread_number = count;
data->x = i;
data->y = j;
pthread_create(&workers[data->thread_number], 0, subfield_worker, data);
++count;
}
}
for (i = 0; i
pthread_join(workers[i], NULL);
/* check the status map */
for (i = 0; i
if (status_map[i] == 0)
rv = 0;
}
if (rv == 1)
printf("Sudoku puzzle is valid ");
else
printf("Sudoku puzzle is invalid ");
return 0;
}
void *row_worker(void *params)
{
int i,j, k, status;
int map[PUZZLE_SIZE + 1] = {0};
parameters* p = (parameters *)params;
for (i = 1; i
for (j = 1; j
map[puzzle[i][j]] = 1;
}
/* make sure the digits 1 .. 9 all appear */
status = 1;
for (k = 1; k
if (map[k] == 0) {
status = 0;
break;
}
}
if (status == 0)
break;
else {
/* reinitialize the map */
for (i = 0; i
map[i] = 0;
}
}
if (status)
status_map[p->thread_number] = 1;
pthread_exit(0);
}
void *column_worker(void *params)
{
int i,j, k, status;
int map[PUZZLE_SIZE + 1] = {0};
parameters* p = (parameters *)params;
for (i = 1; i
for (j = 1; j
map[puzzle[j][i]] = 1;
}
/* make sure the digits 1 .. 9 all appear */
status = 1;
for (k = 1; k
if (map[k] == 0) {
status = 0;
break;
}
}
if (status == 0)
break;
else {
/* reinitialize the map */
for (i = 0; i
map[i] = 0;
}
}
if (status)
status_map[p->thread_number] = 1;
pthread_exit(0);
}
void *subfield_worker(void *params)
{
int i,j, k, status;
int map[PUZZLE_SIZE + 1] = {0};
parameters* p = (parameters *)params;
for (i = p->x; i x + (PUZZLE_SIZE/3); i++) {
for (j = p->y; j y + (PUZZLE_SIZE/3); j++) {
map[puzzle[j][i]] = 1;
}
}
/* make sure the digits 1 .. 9 all appear */
status = 1;
for (k = 1; k
if (map[k] == 0) {
status = 0;
break;
}
}
if (status)
status_map[p->thread_number] = 1;
pthread_exit(0);
}
grid2 /hw2)- gedit Save hw2.c grid1 hw1.c grid2 5 3 4 67891 2 6 7 21 9 53 48 1 9 5 3 4 2 5 6 7 8 5 97 69 4 2 4 26 853 791 7 1 3 9 2 48 5 6 9 6 1 5 372 8 4 2 87419 6 35 3 492861 79 Plain Text Tab Width: 8 Ln9. Col 18 INS grid2 /hw2)- gedit Save hw2.c grid1 hw1.c grid2 5 3 4 67891 2 6 7 21 9 53 48 1 9 5 3 4 2 5 6 7 8 5 97 69 4 2 4 26 853 791 7 1 3 9 2 48 5 6 9 6 1 5 372 8 4 2 87419 6 35 3 492861 79 Plain Text Tab Width: 8 Ln9. Col 18 INS
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started