filling an array

J

John Salerno

I might decide to use a 3D array, but for now I'm experimenting with 2D.
Here's what I have so far:

for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
panelOne[x, y] = switches;
}
}

What I'm trying to do is fill up the panelOne array with values read
from a file and stored in the switches array. (I don't read them
straight into the panelOne array because the switches array is in a
while loop and it's being reused to read each line from the file. Before
the next line is read, the switches array elements are stored
permanently in the panelOne array. Maybe there's a better way?)

Anyway, what I want to know is, how do keep incrementing the indexes so
that it fills up correctly? In the code above, I know it will put the
value of switches[0] in all elements of the panelOne array, because I'm
not incrementing i, but how do I do this? Do I use another for loop, and
if so where do I put it? The above code does

panelOne[0, 0] = switches[0]

and then it does panelOne[0, 1] = switches[0]

How can I make it read panelOne[0, 1] = switches[1]? And then next would
be panelOne[0, 2] = switches[1], and so forth.

The answer might be really easy, but it seems to me that wherever I
increment the i, it's going to mess up incrementing the other variables.

Thanks.
 
J

John Salerno

John said:
How can I make it read panelOne[0, 1] = switches[1]? And then next would
be panelOne[0, 2] = switches[1], and so forth.

Sorry, that last part would be panelOne[0, 2] = switches[2];

By the way, if it matters, the switches array holds 20 values, and each
column of the panelOne array holds 10 values.
 
J

John Richardson

so you want your 2D array to be flattened into your switches array, and you
need to figure out what i should be?

i = 0;
yLen = 10;
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < yLen; y++)
{
i = yLen * x + y;
panelOne[x, y] = switches;
}
}

John Salerno said:
John said:
How can I make it read panelOne[0, 1] = switches[1]? And then next would
be panelOne[0, 2] = switches[1], and so forth.

Sorry, that last part would be panelOne[0, 2] = switches[2];

By the way, if it matters, the switches array holds 20 values, and each
column of the panelOne array holds 10 values.
 
S

Siva M

panelOne[x, y] = switches[i++];

Is this fine?

John said:
How can I make it read panelOne[0, 1] = switches[1]? And then next would
be panelOne[0, 2] = switches[1], and so forth.

Sorry, that last part would be panelOne[0, 2] = switches[2];

By the way, if it matters, the switches array holds 20 values, and each
column of the panelOne array holds 10 values.
 
J

John Salerno

John said:
so you want your 2D array to be flattened into your switches array, and you
need to figure out what i should be?

No, I don't think so. The switches array (which is one dimensional) will
come first, and then I want to expand that into the 2D array. But I
think you example will work anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top