1. ## arrays

How do I turn a two dimensional array into a one dimensional array?

2. You don't have to. Hint: cast.

3. 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;
{

4. 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.

5. 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

6. >> 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...

7. 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;
}

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

9. what do you mean by cast

10. 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. 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)

12. 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.)

13. 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.

14. 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!

15. 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;
{