1. ## Printing 2D Array

I am new to 2D arrays.

I am trying to print out the array.

Code:
```#include <stdio.h>

int main (void)
{
int array[3][4];

int i, k;

for( i = 0; i < 3; i++ )
for( k = 0; k < 4; k++ )
{
printf("The first array is: %d\n", array[i] );
printf("The second array is: %d\n", array[k] );
}
}```
Should print out:

{0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11}

2. Originally Posted by BB89
I am new to 2D arrays.

I am trying to print out the array.

Code:
```#include <stdio.h>

int main (void)
{
int array[3][4];

int i, k;

for( i = 0; i < 3; i++ )
for( k = 0; k < 4; k++ )
{
printf("The first array is: %d\n", array[i] );
printf("The second array is: %d\n", array[k] );
}
}```
Should print out:

{0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11}
You want to print i and k, not array[i] and array[k].

It still won't print what you write that it "should", because on the very first loop, it will print 0, 0, since both i and k will be zero.

You are trying to print out a 2D array, as if it were a 1D array, and there is a formula for that.

Try and work it out, by hand. (hint: don't print either i or k, Think of i and k working together,)

3. Do I need the second for loop?

can I do just one loop

if i changed the array to array[4][4]

Code:
`for( i = 0; i < 4; i++ )`

4. Yes, you need the second for loop.

What would happen if i and k got "married". What was two, becomes one value.

5. Ok, one last question. What do you mean when you said

Code:
`You are trying to print out a 2D array, as if it were a 1D array.`

6. 2D = Two dimension array (one with rows and columns). Like yours. Array[][].

1D = One dimension array (just a sing row in the array). Array[]

You WANT to print out your 2D array, as a 1D array. That was me going "OK, now I know what BB89 wants".

7. Ok, understood. Im gonna work on it.

8. Originally Posted by BB89
I am new to 2D arrays.

I am trying to print out the array.

Code:
```#include <stdio.h>

int main (void)
{
int array[3][4];

int i, k;

for( i = 0; i < 3; i++ )
for( k = 0; k < 4; k++ )
{
printf("The first array is: %d\n", array[i] );
printf("The second array is: %d\n", array[k] );
}
}```
Should print out:

{0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11}
im confused. how could you possibly get that output if you declare but dont initialise

9. Oddly, he doesn't need to print the values of the array[], at all. All he needs to do, to get *THAT* output, is to print the right combination of i and k.

Once he does that, he'll know the equation to print up (or change up), any 2D array, into (or as), a 1D array. My Sudoku program does this, that's why I want him to figure this out for himself. Six months from now, there's a good chance he will still remember it. Otherwise, probably not.

Oddly, he doesn't need to print the values of the array[], at all. All he needs to do, to get *THAT* output, is to print the right combination of i and k.

Once he does that, he'll know the equation to print up (or change up), any 2D array, into (or as), a 1D array. My Sudoku program does this, that's why I want him to figure this out for himself. Six months from now, there's a good chance he will still remember it. Otherwise, probably not.
oh ok.

is this a common programming practice though?

i ask cause couple weeks ago I had to code up a BFS type algorithm on a grid represented as a string, and not a 2d array. it was painful since I had to account for a bunch of edge cases which would have been much easier to handle if it were a simple 2d array

11. Originally Posted by Brain_Child
oh ok.

is this a common programming practice though?

i ask cause couple weeks ago I had to code up a BFS type algorithm on a grid represented as a string, and not a 2d array. it was painful since I had to account for a bunch of edge cases which would have been much easier to handle if it were a simple 2d array
If you like solving problems with arrays, then it's a great trick to know. If you always use linked lists, etc., as your favorite data structure, then no.

Getting that sense of using array indexes for tricks like this, is invaluable, imo.

If the clue about i and k being "married" wasn't enough, I don't know what more of a clue could be given, short of just spelling it out.

I should preface all this by saying I'm a hobby coder, not a pro.

12. well if you need another clue, I wrote this program to help me determine the relationship

Code:
```#include <stdio.h>

int main (void)
{
int array[3][4];

int i, k, j=0;

for( i = 0; i < 3; i++ )
for( k = 0; k < 4; k++ )
{
printf("%d %d %d\n",i, k, j++);

}
}```
running that code should allow you to see the pattern a bit easier