388,751 Members | 4,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 388,751 IT Pros & Developers. It's quick & easy.

Converting one dimensional array into two dimensional array

nlal
P: 19
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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.  
  7.     int cipherKey[10] = {1,0,1,0,0,1,0,1,0,0};
  8.     int compression_P_box[8]= {4,10,6,3,8,2,1,7};
  9.     int block[8];
  10.     int key[8][8];
  11.     cout << "Compressed Key: ";
  12.     for (int i=0; i<8; i++)
  13.     {
  14.        block[i] = cipherKey[compression_P_box[i]-1];
  15.        cout << block[i] << " ";
  16.     }
  17.     cout << endl<<endl;   
  18.     cout << "Round Key:" << endl;
  19.     for (int j=0; j<8; j++)
  20.     {
  21.        int temp[8];
  22.        temp[j] = block[0];
  23.        for (int k=1; k<=8; k++)
  24.        {
  25.          block[k-1] = block[k];
  26.          block[8] = temp[j];
  27.          cout << block[k] << " " ;
  28.        }
  29.        cout << endl;
  30.     }
  31.     cout << endl;
  32.  
  33.       system("PAUSE");
  34.       return 0;
  35. }

I want to store the values in a two dimensional array. Any help would be appreciated.
Oct 5 '10 #1
Share this Question
Share on Google+
1 Reply


Oralloy
Expert 100+
P: 911
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
Expand|Select|Wrap|Line Numbers
  1.   for (int bit = 0;  (8 > bit);  bit ++)
  2.     key[j][bit] = block[bit];
  3.  
Also, there's a really good essay on arrays at this C topic: Arrays Revealed

Good luck!
Oct 5 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.