couples.c (8733B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <math.h> 4 5 int even(int x){ 6 return (!(x % 2)); 7 } 8 9 int max(int a, int b){ 10 if(a > b) 11 return a; 12 return b; 13 } 14 15 long int orderedPairToCodeNat(int x, int y){ 16 long code; 17 int sumxy; 18 sumxy = x + y; 19 code = ((sumxy)*(sumxy + 1))/2; 20 21 if(even(sumxy)){ 22 code = code + x; 23 } 24 else{ 25 code = code + y; 26 } 27 return code; 28 } 29 30 int* codeToOrderedPairNat(long int code){ 31 int *couple = malloc(2*sizeof(int)); 32 int n = sqrt(code * 2); 33 long int axis = (n * (n + 1))/2; 34 int diff = 0; 35 if(axis > code){ 36 n = n - 1; 37 axis = (n * (n + 1))/2; 38 } 39 diff = code - axis; 40 if(even(n)){ 41 couple[0] = diff; 42 couple[1] = n - diff; 43 } 44 else{ 45 couple[0] = n - diff; 46 couple[1] = diff; 47 } 48 return couple; 49 } 50 51 long int orderedPairToCodeInt(int x, int y){ 52 long int code = 0; 53 54 if (x < 0){ 55 x = (2 * (abs (x))) - 1; 56 } 57 else{ 58 x = 2 * x; 59 } 60 61 if (y < 0){ 62 y = (2 * (abs(y))) - 1; 63 } 64 else{ 65 y = 2 * y; 66 } 67 68 code = orderedPairToCodeNat(x, y); 69 return code; 70 } 71 72 int* codeToOrderedPairInt(long int code){ 73 int *pair = codeToOrderedPairNat(code); 74 75 if (even(pair[0])){ 76 pair[0] = pair[0] / 2; 77 } 78 else{ 79 pair[0] = (pair[0] / 2)*(-1) - 1; 80 } 81 82 if (even (pair[1])){ 83 pair[1] = pair[1] / 2; 84 } 85 else{ 86 pair[1] = (pair[1] / 2)*(-1) - 1; 87 } 88 89 return pair; 90 } 91 92 long int orderedPairToCodeIntAlgo2(int x, int y){ 93 long int code = 0; 94 int _x, _y, diff; 95 _x = _y = diff = 0; 96 int temp; 97 int absmax; 98 absmax = max(abs(x), abs(y)); 99 100 if(absmax == abs(x)){ // _x == x 101 _x = _y = x; 102 temp = abs(x) * 2; 103 if(x < 0){ // x negative 104 code = temp * (temp + 1); 105 if(y < 0){ // x negative, y negative 106 diff = abs(_y) - abs(y); 107 } 108 else{ // x negative, y positive 109 diff = abs(_y) + abs(y); 110 } 111 } 112 else{ // x positive 113 code = (temp - 1) * temp; 114 if(y > 0){ // x positive, y positive 115 diff = abs(_y) - abs(y); 116 } 117 else{ // x positive, y negative 118 diff = abs(_y) + abs(y); 119 } 120 } 121 code = code - diff; 122 } 123 else{ // _y = y 124 _x = _y = y; 125 temp = abs(y) * 2; 126 if(y < 0){ // y negative 127 code = temp * (temp + 1); 128 if(x < 0){ // y negative, x negative 129 diff = abs(_x) - abs(x); 130 } 131 else{ // y negative, x positive 132 diff = abs(_x) + abs (x); 133 } 134 } 135 else{ // y positive 136 code = (temp - 1) * temp; 137 if(x > 0){ // y positive, x positive 138 diff = abs(_x) - abs(x); 139 } 140 else{ // y positive, x negative 141 diff = abs(_x) + abs(x); 142 } 143 } 144 code = code + diff; 145 } 146 return code; 147 } 148 149 int* codeToOrderedPairIntAlgo2(long int code){ 150 int* pair = malloc(2*sizeof(int)); 151 int isqrtcode = (int) sqrt(code); 152 long int pivotcode = isqrtcode * (isqrtcode + 1); 153 int x, y; 154 x = y = 0; 155 156 if(even(isqrtcode)){ 157 x = y = -(isqrtcode / 2); 158 if(code > pivotcode){ 159 x = x + (code - pivotcode); 160 } 161 else{ 162 y = y + (pivotcode - code); 163 } 164 } 165 else{ 166 x = y = (isqrtcode / 2) + 1; 167 if(code > pivotcode){ 168 x = x - (code - pivotcode); 169 } 170 else{ 171 y = y - (pivotcode - code); 172 } 173 } 174 pair[0] = x; 175 pair[1] = y; 176 return pair; 177 } 178 179 long int orderedMultipleToCodeNat(int *arr, int size){ 180 long int code; 181 if(size > 1){ 182 code = orderedPairToCodeNat(arr[size - 2], arr[size - 1]); 183 arr[size - 2] = code; 184 arr = realloc(arr, (size - 1)); 185 if(size > 2){ 186 code = orderedMultipleToCodeNat(&arr[0], (size - 1)); 187 } 188 } 189 return code; 190 } 191 192 int* codeToOrderedMultipleNat(long int code, int size){ 193 int *multiple = malloc(size*sizeof(int)); 194 int *pair; 195 int i = 0; 196 for(i = 0; i < (size - 1); i++){ 197 pair = codeToOrderedPairNat(code); 198 code = pair[1]; 199 multiple[i] = pair[0]; 200 multiple[i + 1] = pair[1]; 201 } 202 return multiple; 203 } 204 205 206 long int orderedMultipleToCodeInt(int *arr, int size){ 207 long int code; 208 if(size > 1){ 209 code = orderedPairToCodeInt(arr[size - 2], arr[size - 1]); 210 arr[size - 2] = code; 211 arr = realloc(arr, (size - 1)); 212 if(size > 2){ 213 code = orderedMultipleToCodeInt(&arr[0], (size - 1)); 214 } 215 } 216 return code; 217 } 218 219 int* codeToOrderedMultipleInt(long int code, int size){ 220 int *multiple = malloc(size*sizeof(int)); 221 int *pair; 222 int i = 0; 223 for(i = 0; i < (size - 1); i++){ 224 pair = codeToOrderedPairInt(code); 225 code = pair[1]; 226 multiple[i] = pair[0]; 227 multiple[i + 1] = pair[1]; 228 } 229 return multiple; 230 } 231 232 233 long int orderedMultipleToCodeIntAlgo2(int *arr, int size){ 234 long int code = 0; 235 if(size > 1){ 236 code = orderedPairToCodeIntAlgo2(arr[size - 2], arr[size - 1]); 237 arr[size - 2] = code; 238 arr = realloc(arr, (size - 1)); 239 if(size > 2){ 240 code = orderedMultipleToCodeIntAlgo2(&arr[0], (size - 1)); 241 } 242 } 243 return code; 244 } 245 246 int* codeToOrderedMultipleIntAlgo2(long int code, int size){ 247 int *multiple = malloc(size*sizeof(int)); 248 int *pair; 249 int i = 0; 250 for(i = 0; i < (size - 1); i++){ 251 pair = codeToOrderedPairIntAlgo2(code); 252 code = pair[1]; 253 multiple[i] = pair[0]; 254 multiple[i + 1] = pair[1]; 255 } 256 return multiple; 257 } 258 259 260 261 262 void testMultiNat(){ 263 264 int x = 0; 265 int y = 0; 266 long int code = 0; 267 int *p; 268 int size = 0; 269 270 do{ 271 printf("\n(NATURAL NUMBERS) testPairInt();Enter a size of a multidimensional array representing a 'ordered multiple': "); 272 scanf("%d",&size); 273 p = malloc(size * sizeof(int)); 274 int i; 275 for(i = 0; i < size; i++){ 276 printf("Enter value number %d: ", i); 277 scanf("%d", &p[i]); 278 } 279 280 code = orderedMultipleToCodeNat(p, size); 281 printf("\n... The code is %ld", code); 282 printf ("\ncode = "); 283 scanf("%ld",&code); 284 printf ("\nnumber of ordered elements = "); 285 scanf("%d",&size); 286 p = codeToOrderedMultipleNat(code, size); 287 printf("The ordered multiple identified by code %ld contains the following elements: ", code); 288 printf("("); 289 290 for(i = 0; i < (size - 1); i++){ 291 printf("%d, ", p[i]); 292 } 293 printf("%d)", p[size-1]); 294 } 295 while(1); 296 } 297 298 void testPairInt(){ 299 300 int x = 0; 301 int y = 0; 302 long int code = 0; 303 int *p; 304 305 do{ 306 p = malloc(2 * sizeof(int)); 307 int i; 308 for(i = 0; i < 2; i++){ 309 printf("(ORDERED PAIRS INT) Enter value number %d: ", i); 310 scanf("%d", &p[i]); 311 } 312 313 code = orderedPairToCodeInt(p[0], p[1]); 314 printf("\n... The code is %ld", code); 315 printf ("\ncode = "); 316 scanf("%ld",&code); 317 p = codeToOrderedPairInt(code); 318 printf("The ordered pair identified by code %ld contains the following elements: ", code); 319 printf("("); 320 321 for(i = 0; i < 1; i++){ 322 printf("%d, ", p[i]); 323 } 324 printf("%d)\n", p[1]); 325 } 326 while(1); 327 } 328 329 void testMultiInt(){ 330 331 int x = 0; 332 int y = 0; 333 long int code = 0; 334 int *p; 335 int size = 0; 336 337 do{ 338 printf("\n(INTEGERS) Enter a size of a multidimensional array representing a 'ordered multiple': "); 339 scanf("%d",&size); 340 p = malloc(size * sizeof(int)); 341 int i; 342 for(i = 0; i < size; i++){ 343 printf("Enter value number %d: ", i); 344 scanf("%d", &p[i]); 345 } 346 347 code = orderedMultipleToCodeInt(p, size); 348 printf("\n... The code is %ld", code); 349 printf ("\ncode = "); 350 scanf("%ld",&code); 351 printf ("\nnumber of ordered elements = "); 352 scanf("%d",&size); 353 p = codeToOrderedMultipleInt(code, size); 354 printf("The ordered multiple identified by code %ld contains the following elements: ", code); 355 printf("("); 356 357 for(i = 0; i < (size - 1); i++){ 358 printf("%d, ", p[i]); 359 } 360 printf("%d)", p[size-1]); 361 } 362 while(1); 363 } 364 365 void testPairIntAlgo2(){ 366 367 int x = 0; 368 int y = 0; 369 long int code = 0; 370 int *p; 371 372 do{ 373 p = malloc(2 * sizeof(int)); 374 int i; 375 376 for(i = 0; i < 2; i++){ 377 printf("(ORDERED PAIRS INT) Enter value number %d: ", i); 378 scanf("%d", &p[i]); 379 } 380 381 code = orderedPairToCodeIntAlgo2(p[0], p[1]); 382 printf("\n... The code is %ld", code); 383 384 printf ("\ncode = "); 385 scanf("%ld",&code); 386 p = codeToOrderedPairIntAlgo2(code); 387 printf("The ordered pair identified by code %ld contains the following elements: ", code); 388 printf("("); 389 390 for(i = 0; i < 1; i++){ 391 printf("%d, ", p[i]); 392 } 393 printf("%d)\n", p[1]); 394 } 395 while(1); 396 } 397 398 399 void testMultiIntAlgo2(){ 400 401 int x = 0; 402 int y = 0; 403 long int code = 0; 404 int *p; 405 int size = 0; 406 407 do{ 408 printf("\n(INTEGERS) Enter a size of a multidimensional array representing a 'ordered multiple': "); 409 scanf("%d",&size); 410 p = malloc(size * sizeof(int)); 411 int i; 412 for(i = 0; i < size; i++){ 413 printf("Enter value number %d: ", i); 414 scanf("%d", &p[i]); 415 } 416 417 code = orderedMultipleToCodeIntAlgo2(p, size); 418 printf("\n... The code is %ld", code); 419 printf ("\ncode = "); 420 scanf("%ld",&code); 421 printf ("\nnumber of ordered elements = "); 422 scanf("%d",&size); 423 p = codeToOrderedMultipleIntAlgo2(code, size); 424 printf("The ordered multiple identified by code %ld contains the following elements: ", code); 425 printf("("); 426 427 for(i = 0; i < (size - 1); i++){ 428 printf("%d, ", p[i]); 429 } 430 printf("%d)", p[size-1]); 431 } 432 while(1); 433 } 434 435 436 437 438 int main(int argc, char **argv, char **envp){ 439 // testMultiNat(); 440 // testPairInt(); 441 // testMultiInt(); 442 // testPairIntAlgo2(); 443 testMultiIntAlgo2(); 444 } 445