How do I turn a two dimensional array into a one dimensional array?
This is a discussion on arrays within the C++ Programming forums, part of the General Programming Boards category; How do I turn a two dimensional array into a one dimensional array?...
How do I turn a two dimensional array into a one dimensional array?
You don't have to. Hint: cast.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Here is what I have I dont know what to put in the for loops
int val[height][width]
int n,m;
int main()
{
for( )
for( )
{val[]=(n+1)*(m+1);
}
return 0;
{
What are you trying to compute, here? I see no reason why you would expect, or for that matter want, val as a one-dimensional array.
Its just a problem for school I need to find out how to take a two dimensional array and turn it in to a one dimensional array
>> Here is what I have I dont know what to put in the for loops
You put valid C statements in them and then check the output, for starters. But if you're too lazy to pick up a book and actually learn how to do that, well...
Last edited by Sebastiani; 11-30-2008 at 07:15 PM. Reason: tone
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Here is what the two dimensional array code looks like, I need to find out what goes in the for loops in the above code to turn it in to a one dimensional array.
int val[height][width]
int n,m;
int main()
{
for(n=0; n<height;n++)
for(m=0;m<width;m++)
{val[n][m]=(n+1)*(m+1);
}
return 0;
}
Last edited by john5754; 11-30-2008 at 07:23 PM.
Ok, the array contents are *guaranteed* to be contiguous, so a simple cast is *really* all that is needed (ie: ** -> * ). Can you manage that?
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
what do you mean by cast
eg:
It works with pointers, too, of course.Code:double d = 2.5; int i = int( d ); // c-style // - or - int j = static_cast< int >( d ); // c++-style
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Is that true everywhere? For example, dynamically allocated arrays, are they also guaranteed to be contiguous?
My solution would have looked something like this
However, casting would be a much more efficient way to move the data. (Ok, you aren't really moving data)Code:int old2darray[width][height]; int new1darray[width * height]; for (blah) { for(blah) { move data from oldarray to new array } }
Just in case there is confusion, note that what tabstop means is that consecutively (or otherwise) allocated dynamic arrays are not guaranteed to be contiguous. However, like fixed size arrays, the contents of each dynamic array are contiguous.Originally Posted by tabstop
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
Here is what I got, It seems to work when I run it. See any problems?
# define width 5
# define height 3
int jimmy[Height * Width]
int n,m;
int main()
{
for(n=0; n<height ; n++)
for(m=0; m<width; m++)
{ jimmy[n]=(n+1)*(m+1);
}
return 0;
{