Simple Mapping
Currently there are a lot of instances that we need to map data from the user interfaces or user interfaces to data.
premise #1: That greater number is divisible by the lower number.
#X = number of X elements
#Y = number of Y elements
Xr = X ratio against Y
Yr = Y ratio against X
R = range of single element of greater size
M = map
if #X == #Y, mapping is 1:1
if #X > #Y, mapping is Xr:Yr, where Xr > Yr
if #X < #Y, mapping is Xr:Yr, where Xr < Yr
Constant R is:
X/Y, where X > Y or
Y/X, where X < Y
Mapping from X -> Y, where X > Y:
X/R = q, if q has zero remainder then M is q or
If q has a remainder then M is q+ 1
until X=1
Mapping from X -> Y, where X < Y:
X*R = p, M is p - (R-1)
Example 1:
If X=20, Y=5
R = 20/5 = 4
M, X->Y :
20/4=5 = q , then 20 -> 5
19/4=4.? = q+1, then 19->5
18/4=4.? = q+1, then 18->5
17/4=4.? = q+1, then 17->5
So, 20,19,18,17 ->5
16/4=4 = q, then 16->4
15/4=3 = q+1, then 15->4
14/4=3.? = q+1, then 14->4
…
7/4=1.? = q+1, then 7->2
…
4/4=1 = q, then 4->1
¾= 0 = q+1, then 3->1
2/4 =0 = q+1, then 2->1
¼=0 = q+1, then 1->1
So, 4,3,2,1 -> 1
Example 2:
If X=3, Y=15
R = 15/3 = 5
X*R = 3*5=15
15-(R-1) = 15-(5-1)=11
15-(4-1-1) = 15-(4-1)=12
15-(3-1) = 15-(3-1) = 13
…
15-(1-1) = 15-(1-1)=15
So, 3 -> 11,12,13,14,15
If X=1,
X*R = 1*5=5
5-(5-1)=1
5-(4-1)=2
5-(3-1)=3
5-(2-1)=4
5-(1-1)=5
So, 1 -> 1,2,3,4,5
Sample Code:
Int main()
{
UINT x=300,y=5;
CMapping mapper;
mapper.StartMap(x,y);
mapper.DisplayMapToTerminal();
CArray<UINT,UINT> map;
mapper.FindMap(3,map);
mapper.DisplayFindMap(map);
UINT z=0;
mapper.FindMap(108,z);
cout << "\n" << z << "\n";
system("PAUSE");
}
Interesting idea.. simple but powerful
ReplyDelete