can anyone help me initialize this array with nested for loops?

char board[3][3];                
board[0][0] = '1';
board[0][1] = '2';
board[0][2] = '3';
board[1][0] = '4';
board[1][1] = '5';
board[1][2] = '6';
board[2][0] = '7';
board[2][1] = '8';
board[2][2] = '9';

Recommended Answers

All 4 Replies

Would it help you to know

char a = '1';
a++;

will give you '2'?

Now use the array indicies to offset the value by the number of rows (with the appropriate number of columns) and by the leftover columns. Trial and error should get it if nothing else.

Honestly, it's almost less work to do it the way you have it there.

Honestly, it's almost less work to do it the way you have it there.

Maybe ( i actually disagree ), but that solution wouldn't extend very well at all.

OP: think about how you would mentally walk over a matrix visiting every element. Think about the system you use to ensure that you visit every single element. Now, think about how you could write down instructions describing how to walk the matrix. Think about how to write these instructions so that another person who has no idea of what a matrix is or what it is for could still walk over the matrix and visit every element. If you carefully and thoughtfully do that, you should see that the instructions contain two logical loops with one nested inside of the other. Try to apply the logic of those instructions in code.

that solution wouldn't extend very well at all.

I couldn't agree more. He/she had said "this array" which I took to mean this was it for the array and the elements.

>> can anyone help me initialize this array with nested for loops?

Do you know how to do it for a single for loop, i.e initialize say an Array[4]
using a for loop?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.