How do you define a two-dimensional array using positions from two smaller arrays?

Tag: arrays Author: zpenghua Date: 2012-07-09

Basically, I have defined an array Tp with 150 positions at 0.01 increments. I have also defined an array Dp with 600 positions at the same 0.01 increments. I'm trying to write one large array TpDp which puts every combination of the Tp and Dp array values into its two columns so that Tp is in the first column and Dp is in the second column. I'm not sure how to define the TpDp[][] positions so that they equal, e.g. (and not syntacially correct), int TpDp[0][0] = new int [Tp[0]][Dp[0]].

I might have all kinds of errors in this, but here has been my set up so far with Tp and Dp already defined (A = 150, B = 600, these being the number of positions in Tp and Dp, respectively):

int [][] TpDp = new int [A*B][A*B]; //declaring new 2-dimensional array TpDp, size needed for combos 

int i; //iteration counter for Tp

int j; //iteration counter for Dp



for (i=0; i<=A; i++)
{ //i counting through all positions in Tp until exhausts A column options
    for (j=0; j<=B; j++)
    { //j counting through all positions in Dp until exhausts B column options
         TpDp[i][j] = Tp[i], Dp[j]; //This is where I'm not sure how do define TpDp[i][j]
    }
}

Other Answer1

The representation you have now is best.

First of all, you would need to roll your own data representation to fit the values into an array. Second of all, the array would be enormous. Third of all, the enormous array wouldn't do anything more than the 2 seperate arrays you already have, so it would be all cost and no benefit.

From a lookup standpoint: Either way, you would need to do two lookups. Currently you do one lookup from one array and another from a second array. The other way, you'd need to do one lookup in one dimension, then another in the second dimension.

From a memory standpoint: Your quantities are 32 bit values. You store 150 + 600 of them, which totals 3000 bytes. The other way, you would need 150 * 600 values 64 bits wide (to store both numbers), which is 720000 bytes (about 0.4% efficient compared to the current method).

From a time standpoint: both lookups would take the same amount of time.

From a data standpoint: the second way would have a huge amount of redundant data, and the first way stores each necessary value once.