Well the title pretty much explains itself...anyone know how to do this?
Printable View
Well the title pretty much explains itself...anyone know how to do this?
Yes. Pointer arithmetic and/or cast :p
so if i had an array for example power[8][7], i would cast it how?
power1D[56] == power2D[8][7]
Of course you can't assign arrays-as-a-whole. But you could do int *power1D=power2D[8][7], and pretend that power1D has 56 elements.
so it must have that pointer
You could use a real array if you want, instead of a pointer. But you'd have to use a big ol' for-loop to get all the numbers in the new array.
ok ill go with the pointer
As long as you access the 2D array elements in their proper storage order you can use either a pointer with an offset or a subscripted array.
The thing to remember is to walk along the 2D array correctly knowing that the rightmost subscript varies fastest.
so does this work NROWS being 8 and NCOLS being 7
Code:int i, j, k;
int power1d[56];
for(i = 0; i<NROWS; i++)
{
for(j=0; j<NCOLS; j++)
{
for(k=0; k <56; k++)
power1d = power[i][j];
}}
does another array need to be declared?
do i declare the 2-d array as one-d then
right it should be
Code:fint i, j, k;
int power1d[56];
for(i = 0; i<NROWS; i++)
{
for(j=0; j<NCOLS; j++)
{
for(k=0; k <56; k++)
power1d[k] = power[i][j];
}}
i fixed the power1d
Closer. You just have to decide what k is going to be. Currently you are assigning each element of power into every element of power1d, which means only the last element is the one you get kept. You're already in the for loop, so you're looking at one specific element of the power array -- now where does that one specific element need to go?
Yea i knew something was wrong because when i printed it out it was a mess, so does k need to be an outside loop?
k shouldn't be a loop at all. k needs to be computed based on i and j.
so k should be
k=(i +1) *(j+1) - 1
????
i dont think this is the right equation? it works for the first seven then messes up on the index, but the value of the index is correct, do you know of an equation that would fix this?
figured it out
k=(i*7)+j;
Look at code that I have posted. I have given many examples of how to do this.