-
Notifications
You must be signed in to change notification settings - Fork 0
/
Part 1.cpp
207 lines (168 loc) · 4.96 KB
/
Part 1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# include <iostream>
# include <pthread.h>
# include <ctime>
# include <stdlib.h>
using namespace std;
pthread_t pid[11];
int** arr = new int*[9];
pthread_mutex_t mtlock;
struct suduko // for keeping check of grids' row/columns
{
int row =0;
int column =0;
};
void* row(void* arg) // row checker
{
suduko *obj1 = (suduko*) arg;
int count=0;
cout << "\n\n------Row Wise Checking---------\n\n\n";
int temp[9] ;
for(int i=0; i<9;i++)
{
for (int k = 0; k<9;k++)
temp[k] = false;
for (int j=0; j<9; j++)
{
if ( (arr[i][j] > 9) || (arr[i][j] < 1) )
{
cout << "row: " << i+1 << " colummn: " << j+1 << " value: " << arr[i][j] << endl;
bool boolean = false;
return (void*) boolean;
}
if (temp[arr[i][j]-1] == true)
{
cout << "row: " << i+1 << " colummn: " << j+1 << " value: " << arr[i][j]<< endl;
count++;
}
else
temp[arr[i][j]-1] = true;
}
}
cout << "Row-Vise matrix has repeated values: " << count << endl;
if (count>0)
cout << "Validity: 0" << endl;
else
cout << "Validity: 1" << endl;
pthread_exit(NULL);
return 0;
}
void* column(void* arg) // column checker
{
suduko *obj1 = (suduko*) arg;
int count=0;
cout << "\n\n------Column Wise Checking---------\n\n\n";
int temp[9] ;
for(int i=0; i<9;i++)
{
for (int k = 0; k<9;k++)
temp[k] = false;
for (int j=0; j<9; j++)
{
if ( (arr[j][i] > 9) || (arr[j][i] < 1) )
{
cout << "row: " << i+1 << " colummn: " << j+1 << " value: " << arr[i][j]<< endl;
bool boolean = false;
return (void*) boolean;
}
if (temp[arr[j][i]-1] == true)
{
cout << "row: " << i+1 << " colummn: " << j+1 << " value: " << arr[i][j]<< endl;
count++;
}
else
temp[arr[j][i]-1] = true;
}
}
cout << "Column-Vise matrix has repeated values: " << count << endl;
if (count>0)
cout << "Validity: 0" << endl;
else
cout << "Validity: 1" << endl;
pthread_exit(NULL);
return 0;
}
void* grid(void* arg) // grid checker
{
suduko* obj1 = (suduko*) arg;
int count=0;
pthread_mutex_lock(&mtlock);
cout << "\n\n------Grid Wise Checking---------\n\n";
cout << endl;
int rows = obj1->row;
int columns = obj1->column;
if (obj1->column>6)
{
obj1->row+=3;
obj1->column=0;
}
rows = obj1->row;
columns = obj1->column;
cout << rows << " " << columns << endl;
obj1->column+=3;
pthread_mutex_unlock(&mtlock);
int temp[9] ;
for (int k = 0; k<9;k++)
temp[k] = false;
for(int i=rows; i<rows+3;i++)
{
for (int j=columns; j<columns+3; j++)
{
if ( (arr[i][j] > 9) || (arr[i][j] < 1) )
{
cout << "row: " << i+1 << " colummn: " << j+1 << " value: " << arr[i][j] << endl;
bool boolean = false;
return (void*) boolean;
}
if (temp[arr[i][j]-1] == true)
{
cout << "row: " << i+1 << " colummn: " << j+1 << " value: " << arr[i][j] << endl;
count++;
}
else
temp[arr[i][j]-1] = true;
}
}
cout << "Matrix-Vise matrix has repeated values: " << count << endl;
if (count>0)
cout << "Validity: 0" << endl;
else
cout << "Validity: 1" << endl;
pthread_exit(NULL);
}
int main()
{
for(int i = 0; i < 9; ++i)
arr[i] = new int[9];
int arr_temp[9][9] = {
{1,2,3,4,5,6,7,8,9} ,
{1,2,7,4,5,6,3,8,9} ,
{1,2,3,4,5,6,7,8,9} ,
{1,2,3,4,5,6,7,8,9} ,
{1,2,3,4,5,6,7,8,9} ,
{1,2,3,4,5,6,7,8,9} ,
{1,2,3,4,5,6,7,8,9} ,
{1,2,3,4,5,6,7,8,9} ,
{1,2,3,4,5,6,7,8,9} ,
} ;
suduko obj2 ;
for (int i=0; i<9; i++)
{
for (int j =0 ; j<9; j++)
{
arr[i][j] = arr_temp[i][j];
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
// row & column threads
pthread_create(&pid[0] , NULL , row , (void*) &obj2);
pthread_create(&pid[1] , NULL , column , (void*) &obj2);
obj2.column=0;
obj2.row=0;
// Grid threads
for(int i=2 ; i<=10 ; i++)
pthread_create(&pid[i] , NULL , grid , (void*) &obj2 );
pthread_exit(NULL);
return 0;
}