How do I turn a two dimensional array into a one dimensional array?
Printable View
How do I turn a two dimensional array into a one dimensional array?
You don't have to. ;) Hint: cast.
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...
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;
}
Ok, the array contents are *guaranteed* to be contiguous, so a simple cast is *really* all that is needed (ie: ** -> * ). Can you manage that?
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
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.Quote:
Originally Posted by tabstop
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;
{
It should not even compile without errors since this statement is not terminated by a semi-colon:Quote:
Originally Posted by john5754
I suggest that you post your actual code (copy and paste, if necessary), and please post it in [code][/code] bbcode tags.Code:int jimmy[Height * Width]
EDIT:
Incidentally, I think that you need two arrays: the two dimensional array source and the one dimensional array destination. Alternatively, you use a one dimensional array and access it as a two dimensional array (but that seems the opposite of what you are asking for). Or, you use a two dimensional array and access it as a one dimensional array via a cast (but in practice, this should be avoided).
Code:#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int j=99;
int height=3,width=5,n,m,i,jimmy[j];
for(n=0; n<height; n++)
for(m=0; m<width; m++)
{jimmy [n]=(n+1)*(m+1);
cout<< setw(4)<< jimmy[n];
}
return 0;
}
I really don't get what you're trying to accomplish here. The code you've posted does nothing but assign some random values to the first three elements in a one-dimensional array. If you want to transform a 2-d array to 1-d, just do a cast. If you want to copy it then do the cast and use memcpy (height * width * sizeof( int )). Or, if you insist on doing it the hard way, source[ n ][ m ] == destination[ width * n + m ].