-
Notifications
You must be signed in to change notification settings - Fork 0
/
example02.c
164 lines (137 loc) · 4.6 KB
/
example02.c
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
#include <stdio.h>
#include <stdlib.h>
#define side 3
/*
* Past examination 18/02/2018 (version C)
* today date: 04/04/2021
* my signature: 49dbffdad5428e0a32ba850a6e5e0d45a550c70ef110a748058feb78590ca4dd
*/
FILE* open_file(char[]);
void import_square(int[2], int[side][side], FILE*);
int compute_x(const int[2], int[side][side]);
void print_all(const int[2], int[side][side]);
int main() {
char filename[] = "input.txt";
FILE* file;
int square[side][side], k[2];
int x;
if(!(file = open_file(filename))){
printf("\nError opening the file\n");
exit(EXIT_FAILURE);
}
import_square(k, square, file);
if(k[0] < 0){
printf("\nThe square is not a magic one\n");
return 0;
}
if (k[1] == -2){
printf("\nThe -1 is not on the diagonal\n");
exit(EXIT_FAILURE);
}
x = compute_x(k, square);
if(x >= 0)
print_all(k, square);
else
printf("\nImpossible to complete the square magic\n");
exit(EXIT_SUCCESS);
}
FILE* open_file(char filename[]){
FILE* file;
if (fopen_s(&file, filename, "r") == 0) {
return file;
}
return 0;
}
void import_square(int k[2], int square[side][side], FILE* file) {
int tmp_x_sum = 0; // sum of the row
int tmp_y_sums[side] = {0}; // sums of the columns
short i, j;
k[0] = -1; // K
k[1] = -1; // -1 position (the variable x)
// Read from the file
rewind(file);
for (i = 0; i < side; i++) {
j = 0;
while (j < side) {
// Read side*side values (ignoring everything that isn't an integer)
if (fscanf_s(file, "%d%*c", &square[i][j])) j++;
}
}
// Compute k
for (i = 0; i < side; i++) {
// For each row
for (j = 0; j < side; j++) {
// For each column
if (square[i][j] == -1) {
// In the position of -1
tmp_x_sum = -1; // set the row sum to -1
tmp_y_sums[j] = -1; // set the column sum to -1
k[1] = (i == j ? i : -2); // save the position
} else {
if (tmp_x_sum != -1) // If the row doesn't contain the -1
tmp_x_sum += square[i][j]; // update the row sum
if (tmp_y_sums[j] != -1) // If the column doesn't contain the -1
tmp_y_sums[j] += square[i][j]; // update the column sum
}
}
/*
* Sum of the row validation
* -3 means that the actual row sum is different from the previous one computed
* k[0] is == -1 if we are at the first iteration
* tmp_x_sum is < 0 if in that row there's the -1
*/
if (tmp_x_sum >= 0) {
// If the sum of the row is not negative
if (k[0] == -1) {
k[0] = tmp_x_sum; // if k is not define, do it now
} else {
k[0] = (k[0] != tmp_x_sum ? -3 : k[0]);
}
}
tmp_x_sum = 0;
}
/*
* Sum of the columns validation
* -3 means that the actual column sum is different from the previous one computed
* tmp_y_sum is < 0 if in that column there's the -1
*/
for (j = 0; j < side; j++){
// For each column sum
if (tmp_y_sums[j] >= 0 && k[0] != tmp_y_sums[j]) {
// If the column sum is not negative, but the column sum is different from k
k[0] = -3;
}
}
}
int compute_x(const int k[2], int square[side][side]){
int x; // the -1 (the unknown)
unsigned short j;
int sum = 0;
for(j=0; j<side; j++) // For each column
if (j != k[1]) // different from the one with the -1
sum += square[k[1]][j]; // update the sum
x = k[0] - sum;
sum = k[0]*2; // set the sum as 2 times K
square[k[1]][k[1]] = x; // substitute in the square the x
// Diagonal check
for(j = 0; j<side; j++){
sum -= square[j][j]; // update the sum
sum -= square[j][side - j - 1];
}
if (!sum)
return x;
else
return -1;
}
void print_all(const int k[2], int square[side][side]){
unsigned short i, j;
printf("\nMagic constant: %d\n\nSquare:\n", k[0]);
for (i = 0; i<side; i++){
// For each row
for (j = 0; j < side; j++){
// For each column
printf("%d\t", square[i][j]);
}
printf("\n");
}
}