Converting one dimensional array into two dimensional array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nlal
    New Member
    • Jan 2010
    • 19

    Converting one dimensional array into two dimensional array

    Hi I have a small program that takes a 10bit key, compresses it into 8bit key and does a left shift 8times, generating a key each time. I am using a one dimensional array to store the key value of each round. the probelm is that the next round over writes the key values. My code is as follows:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
    
        int cipherKey[10] = {1,0,1,0,0,1,0,1,0,0};
        int compression_P_box[8]= {4,10,6,3,8,2,1,7};
        int block[8];
        int key[8][8];
        cout << "Compressed Key: ";
        for (int i=0; i<8; i++)
        {
           block[i] = cipherKey[compression_P_box[i]-1];
           cout << block[i] << " ";
        }
        cout << endl<<endl;   
        cout << "Round Key:" << endl;
        for (int j=0; j<8; j++)
        {
           int temp[8];
           temp[j] = block[0];
           for (int k=1; k<=8; k++)
           {
             block[k-1] = block[k];
             block[8] = temp[j];
             cout << block[k] << " " ;
           }
           cout << endl;
        }
        cout << endl;
    
          system("PAUSE");
          return 0;
    }

    I want to store the values in a two dimensional array. Any help would be appreciated.
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    I assume you want to populate your [B]key[8][8][B] array at the top of the j loop starting at line 19?

    Where j is the key round?

    If so, you might try inserting this right after line 20
    Code:
      for (int bit = 0;  (8 > bit);  bit ++)
        key[j][bit] = block[bit];
    Also, there's a really good essay on arrays at this C topic: http://bytes.com/topic/c/insights/77...rrays-revealed

    Good luck!

    Comment

    Working...