How would you do this Array??

Jawab How would you do this Array??

  • 31 Juli 2012 11:09
     
     

    Hi All

    how would you do this array?


    The first Array will consist of the following

    Array1 = A,B,C,D,E,F,G,H,I,J,K,L,M

    The second

    Array2 = 2,2,1,2,2,2,1

    I will have a form with a dropdown textbox of Array1 so the user can select the desired
    letter. Also they indicate from which start position they want from Array2

    So basic example

    User selects "A" and position 1 from Array2

    So we start with "A" then

    add "2" (from Array2) which outputs "C" (From Array1), ("C" is two away from "A" etc etc)
    add "2" (from Array2) which outputs "E" (From Array1)
    add "1" (from Array2) which outputs "F" (From Array1)
    add "2" (from Array2) which outputs "H" (From Array1)
    add "2" (from Array2) which outputs "J" (From Array1)
    add "2" (from Array2) which outputs "L" (From Array1)
    add "1" (from Array2) which outputs "M" (From Array1)

    If a user selects a middle letter for example "G" and position 3 then the following occurs.

    Start with "G"

    add "1" (from Array2) which outputs "H" (From Array1), ("H" is one away from "G" etc etc)
    add "2" (from Array2) which outputs "J" (From Array1)
    add "2" (from Array2) which outputs "L" (From Array1)
    add "2" (from Array2) which outputs "A" (From Array1), ("A" is two away from "L", going from left to right)
    add "1" (from Array2) which outputs "B" (From Array1)
    add "2" (from Array2) which outputs "D" (From Array1)
    add "2" (from Array2) which outputs "F" (From Array1)

    Regards

    Geoff

    • Jenis yang Diubah JimiH100 31 Juli 2012 13:38 Requested
    •  

Semua Balasan

  • 31 Juli 2012 12:40
     
     
    Hi All
     
    how would you do this array?
     
    The first Array will consist of the following
     
    Array1 = A,B,C,D,E,F,G,H,I,J,K,L,M
     
    The second
     
    Array2 = 2,2,1,2,2,2,1
     
    I will have a form with a dropdown textbox of Array1 so the user can select the desired
    letter. Also they indicate from which start position they want from Array2
       

    I'm really not sure what your question is, but would it help you to know that that char is just an 8-bit integer, and the letters 'A' through 'Z' have consecutive values? That is, the following expressions all evaluate true:

    'C' == 'A' + 2
    'E' == 'C' = 2
    'F' == 'E' + 1

    Please change the type of your thread to "Ask a Question."


    David Wilkinson | Visual C++ MVP

    • Diedit oleh davewilkMVP 31 Juli 2012 13:04 note added
    •  
  • 31 Juli 2012 13:53
     
     

    Sorry, its difficult to explain :(

    I'll try again

    Imagine a clock with the hour hand only. first of all you select a number to start from (1,2,3,4,5,6,7,8,9,10,11,12)

    Lets pick 5

    Now we have another sequence of numbers., 2,2,1,2,2,2,1

    So our first number is 5 which we are going to rotate clockwise using the each one of the second sequence

    5+2=7

    7+2=9

    9+1=10

    10+2=12

    12+2=2 (Start from 12 add 2 = 2 on the clock)

    2+2=4

    4+1 = 5

    So the end result would be 5,7,9,10,12,2,4,5

    thats starting from 5 and using 2,2,1,2,2,2,1 as the rotating sequence.

    Now we could also start at any point in the sequence also ,so we could start at the second "2"

    Giving us a sequence of 2,1,2,2,2,1,2

    thanks

    Geoff

  • 31 Juli 2012 14:16
     
     Jawab
    Sorry, its difficult to explain :(
     
    I'll try again
     
    Imagine a clock with the hour hand only. first of all you select a number to start from (1,2,3,4,5,6,7,8,9,10,11,12)
     
    Lets pick 5
     
    Now we have another sequence of numbers., 2,2,1,2,2,2,1
     
    So our first number is 5 which we are going to rotate clockwise using the each one of the second sequence
     
    5+2=7
     
    7+2=9
     
    9+1=10
     
    10+2=12
     
    12+2=2 (Start from 12 add 2 = 2 on the clock)
     
    2+2=4
     
    4+1 = 5
     
    So the end result would be 5,7,9,10,12,2,4,5
     
    thats starting from 5 and using 2,2,1,2,2,2,1 as the rotating sequence.
     
    Now we could also start at any point in the sequence also ,so we could start at the second "2"

    Something like this?

    int B[] = {2,2,1,2,2,2,1};
    const int len = sizeof(B)/sizeof(B[0]); // 7
    int A[len + 1];
    A[0] = 5;
    for (int i = 1; i <= len; i++)
    {
      A[i] =(A[i - 1] + B[i - 1])%12;
    }
    assert(A[len] == 5);


    David Wilkinson | Visual C++ MVP

  • 01 Agustus 2012 9:02
     
     
    Thanks David ,this gives me something to go on.
  • 03 Agustus 2012 5:52
    Moderator
     
     Jawab Memiliki Kode

    Hi JimiH100,

    According to your description, I write a piece of codes for your reference:

    char Array1[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M'};
    int Array2[] = {2,2,1,2,2,2,1};
    
    const int iLen1 = sizeof(Array1)/sizeof(Array1[0]);
    const int iLen2 = sizeof(Array2)/sizeof(Array2[0]);
    for (int i = 0; i < iLen1; i++)
    { cout<<Array1[i]; }
    cout<<endl;
    for (int i = 0; i < iLen2; i++)
    { cout<<Array2[i]; }
    cout<<endl;
    
    int i = 0, seq, seqUser;
    char letter;
    cout<<"Choose a letter in Array1 and sequence in Array2:";
    cin>>letter>>seqUser;
    
    for (; i < iLen1; ++i)
    {
    	if (Array1[i] == letter)
    	{
    		seq = i;
    		for (int j = 0; j < iLen2; ++j)
    		{
    			seq = (seq + Array2[(seqUser-1)%iLen2])%iLen1;
    			cout<<Array1[seq];
    			seqUser++;
    		}
    		cout<<endl;
    		return 0;
    	}
    }
    if (i >= iLen1)
    	cout<<"Letter not found in Array1!"<<endl;
    Cheers,
    Damon

    Damon Zheng [MSFT]
    MSDN Community Support | Feedback to us