c - I need to produce 144 unique numbers for array subscript with a formula that takes 2 numbers -
c - I need to produce 144 unique numbers for array subscript with a formula that takes 2 numbers -
i'm trying find formula function can take 2 numbers, x , y, each go 1-12 (144 combinations) , produce unique result.
the thought i'm making array, combotab[144]
formula produced x , y produces unique number used array subscript, element hold state yes
or no
.
the sides in x , y appear important, x y combo cannot equal y x combo.
here code create combos:
int combotab[size_needed]; int create_combos() { int x, y, j, z; y = x = 1; (j = 0; j < 144; j++) (x = 1; x <= 12; x++) (y = 1; y <= 12; y++){ if (combotab[formula in x , y here] == yes){ printf("possible duplicate, find improve formula\n"); homecoming 0; } else combotab[formula in x , y here] = yes; } homecoming 1; }
just restate i'm asking, need able go through every combination of x
, y
, y
goes 1-12, , x
goes through 1-12, create unique number based on 2 numbers, can used unique subscript array. each unique point in array hold state: yes
or no
. means should have 144 unique yes
states in array end of function.
(notice variable combotab[]
global, of elements initialized 0
or no
.)
after this, create function states based on number combos:
int get_combo_state(x, y);
this homecoming state based on combo given, remember order in numbers appear matter.
get_combo_state(10, 2) != get_combo_state (2, 10)
i tried possible formula, didn't go well:
(x * y + x + y + (x*x) / y) - y
this seemed easy because if x == 12 , y == 12 it's easy see need 168 array elements combotab[168].
anybody have ideas?
use generate composition:
int composed = 12 * (x - 1) + (y - 1); /*zero based*/
and recover x
, y
using
int x = composed / 12 + 1; /*using integer division*/ int y = (composed % 12) + 1;
this idiom mutual when working arrays underlying memory single contiguous block. zero-basing composition simplifies extraction arithmetic.
c algorithm
Comments
Post a Comment