473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

filling an array

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[i];
}
}

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.
Nov 17 '05 #1
5 1966
John Salerno wrote:
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.
Nov 17 '05 #2
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[i];
}
}

"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:oK********************@rcn.net...
John Salerno wrote:
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.

Nov 17 '05 #3
panelOne[x, y] = switches[i++];

Is this fine?

"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:oK********************@rcn.net...
John Salerno wrote:
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.
Nov 17 '05 #4
John Richardson wrote:
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.
Nov 17 '05 #5
Siva M wrote:
panelOne[x, y] = switches[i++];

Is this fine?


Hmm, that seems too simple! :)

Thanks to both of you!
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Raptor | last post by:
I'm using a single script to generate a table with <input>s in each row. I fill the array with initial values, then write it out to the table and let the user edit the values. Something like: ...
1
by: rkmoray | last post by:
I have created a Multi Dimentional array, but having problems filling it. int mCount=0; mCount=ds.Tables.Rows.Count; string arrayppsa = new string ; DataTable myDataTable=ds.Tables;...
3
by: MSDousti | last post by:
Hello I want to write a C# (or VB.NET) program which reads the structure of a PE (odinary win32 executable) file. These files have a long header (512 bytes or so). The definition of the header...
12
by: pjhyett | last post by:
standard 2d array filling with increasing numbers for rows and columns: for(int i=0;i<n;i++) for(int j=0;j<n;j++) a = i + j; problem is it's O(n^2). I'm looking for a method to decrease the...
2
by: drdave | last post by:
Hi All, I'm filling an arraylist with other arraylists within a loop and within the first iteration and addition all is fine.. however when I clear my value holding temporary array I lose the...
3
by: John Espinosa | last post by:
I am new to C#. I am trying to use a for loop of integers to fill up a String array. Actually, I am trying to get a dropdown list of all the days in the current month. This is what I have been...
5
by: John | last post by:
How can I fill an array randomly so it contains a certian range of numbers (1 - 100 for example) ? My Goal is to generate a set of numbers in random order.
6
by: lukasso | last post by:
Hi, this is my code that should produce something like a timetable for a few days with each day divided into 30 minute pieces. It makes query from MySQL and then creates a 2d $array which then is to...
12
by: ab12 | last post by:
I'm trying to write a program in C that gets a shape outlined with asterisks from the user, and returns that shape filled with asterisks. It will also get the coordinates of a point inside the shape...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.