-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
168 lines (140 loc) · 3.96 KB
/
main.cpp
File metadata and controls
168 lines (140 loc) · 3.96 KB
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
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#define N 9
using namespace std;
void binaryRhomb(int A[][N]){
int i,St,En,Mi,key,x,y,suspect,j,fin, e, hit, temphit;
do{
e=0;
hit=0;
temphit=0;
St=1;
fin=((N*N)/2)+1;
En=((N*N)/2)+1;
Mi=(N*N/4)+1;
printf("The key you want to search: ");
scanf("%d",&key);
printf("\n");
if(En==key){
x=N/2;
y=N/2;
printf("The key was found at the position: (%d,%d).\n\n",x,y);
St=En+1;
hit=1;
}
while(St<=En){
j=1;
for (e=0; e<=N/2-1; e++){
if(temphit==0){
for (i=N/2; i>=e; i--){
if (j==Mi){
suspect=A[i][N/2-i+e];
x=i;
y= N/2-i+e;
temphit=1;
break;}
j++;
}
}
if(temphit==0){
for(i=1+e; i<=N/2-1; i++){
if (j==Mi){
suspect=A[i][N/2+i-e];
x=i;
y= N/2+i-e;
temphit=1;
break;}
j++;
}
}
if(temphit==0){
for(i=N/2; i<=N-1-e; i++){
if (j==Mi){
suspect=A[i][N+N/2-1-i-e];
x=i;
y= N+N/2-1-i-e;
temphit=1;
break;}
j++;
}
}
if(temphit==0){
for(i=N-2-e; i>=N/2+1;i--){
A[i][i-N/2+e]=j;
if (j==Mi){
suspect=A[i][i-N/2+e];
x=i;
y= i-N/2+e;
temphit=1;
break;}
j++;
}
}
}
if (suspect==key){
hit=1;
printf("The key was found at the position: (%d,%d).\n\n",x,y);
St=En+1;}
else if (suspect<key && key<=A[N/2][N/2]){
St=Mi;
Mi=(En+St)/2;
temphit=0;}
else if (suspect>key && key<=A[N/2][N/2]){
En=Mi;
Mi=En/2;
temphit=0;}
else
St=En+1;
}
if (hit==0)
printf("The key doesn't exist in the table.\n\n");
}while(1);
}
void fillzeros(int A[][N], int M) {
int i,j;
for (i=0; i<=M-1; i++)
for (j=0; j<=M-1; j++)
A[i][j]=0;
}
void Rhomb(int A[][N], int M){
int i,j;
int e=0;
int k=1;
for (e=0; e<=M/2-1; e++){
for (i=M/2; i>=e; i--){
A[i][M/2-i+e]=k;
k++;}
for(i=1+e; i<=M/2-1; i++){
A[i][M/2+i-e]=k;
k++;}
for(i=M/2; i<=M-1-e; i++){
A[i][M+M/2-1-i-e]=k;
k++;}
for(i=M-2-e; i>=M/2+1;i--){
A[i][i-M/2+e]=k;
k++;}
}
A[M/2][M/2]=k;
}
void disp(int A[][N], int M){
int i,j;
printf("The generated table is the following: \n");
for (i=0; i<=M-1; i++){
for (j=0; j<=M-1; j++){
printf("%d\t",A[i][j]);
}
printf("\n\n");
}
}
int main(int argc, char *argv[]){
int M=N;
int A[N][N];
fillzeros(A,M);
Rhomb(A,M);
disp(A,M);
binaryRhomb(A);
int i,j;
system("PAUSE");
return EXIT_SUCCESS;
}