# arrays

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-30-2008
john5754
arrays
How do I turn a two dimensional array into a one dimensional array?
• 11-30-2008
Sebastiani
You don't have to. ;) Hint: cast.
• 11-30-2008
john5754
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;
{
• 11-30-2008
tabstop
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.
• 11-30-2008
john5754
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
• 11-30-2008
Sebastiani
>> 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...
• 11-30-2008
john5754
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;
}
• 11-30-2008
Sebastiani
Ok, the array contents are *guaranteed* to be contiguous, so a simple cast is *really* all that is needed (ie: ** -> * ). Can you manage that?
• 11-30-2008
john5754
what do you mean by cast
• 11-30-2008
Sebastiani
eg:

Code:

```double d = 2.5; int i = int( d ); // c-style // - or - int j = static_cast< int >( d ); // c++-style```
It works with pointers, too, of course.
• 11-30-2008
Cogman
Quote:

Originally Posted by Sebastiani
Ok, the array contents are *guaranteed* to be contiguous, so a simple cast is *really* all that is needed (ie: ** -> * ). Can you manage that?

Is that true everywhere? For example, dynamically allocated arrays, are they also guaranteed to be contiguous?

My solution would have looked something like this

Code:

```int old2darray[width][height]; int new1darray[width * height]; for (blah) {   for(blah)   {       move data from oldarray to new array   } }```
However, casting would be a much more efficient way to move the data. (Ok, you aren't really moving data)
• 11-30-2008
tabstop
Quote:

Originally Posted by Cogman
Is that true everywhere? For example, dynamically allocated arrays, are they also guaranteed to be contiguous?

Dynamic arrays are not guaranteed to be contiguous. (They almost always aren't, I would guess, if malloc/calloc writes extra bits to the allocated memory.)
• 11-30-2008
laserlight
Quote:

Originally Posted by tabstop
Dynamic arrays are not guaranteed to be contiguous.

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.
• 12-01-2008
Elysia
Quote:

Originally Posted by tabstop
Dynamic arrays are not guaranteed to be contiguous. (They almost always aren't, I would guess, if malloc/calloc writes extra bits to the allocated memory.)

That's new/delete for you.
But anyone in their right mind wouldn't try to make dynamically allocated arrays using new and delete in C... if they can avoid it!
• 12-01-2008
john5754
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;
{
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last