473,657 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to iterate the second dimension of a 2-dimensional array?

Hello,

I want to iterate the second dimension of a 2-dim-array.

Let´s say I have an array:

double[,] myArray and a given index: int i.

Assume my index is given in want to iterate my array with something like
this:
int i = 2;
double sum= 0.0;

for(int j = 0; j < myArray[i].Length; j++)
sum += myArray[i,j];

But this does not work, because I am using the 2dim-array as 1dim-array.
My problem is... how do i get the size of the second dimension?
Regards,

Martin
Sep 26 '06 #1
5 2690

Martin Pöpping wrote:
Hello,

I want to iterate the second dimension of a 2-dim-array.

Let´s say I have an array:

double[,] myArray and a given index: int i.

Assume my index is given in want to iterate my array with something like
this:
int i = 2;
double sum= 0.0;

for(int j = 0; j < myArray[i].Length; j++)
sum += myArray[i,j];

But this does not work, because I am using the 2dim-array as 1dim-array.
My problem is... how do i get the size of the second dimension?
Regards,

Martin
double[,] a=new double[10,10];

for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
a[i,j]=i*j;
}
}
//or try
foreach(double d in a)
{
Console.WriteLi ne(d.ToString() );
}

Sep 26 '06 #2
DeveloperX schrieb:
double[,] a=new double[10,10];

for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
a[i,j]=i*j;
}
}
Well maybe you missunderstand my problem.
I do not know the second dimension.
In your example.... I do not know that j = 10.
//or try
foreach(double d in a)
{
Console.WriteLi ne(d.ToString() );
}
If it would work... it would traverse the whole dimensional array.

I only want to work on a[2,j] for example.
Regards,

Martin

Sep 26 '06 #3
Martin P?pping <ma******@despa mmed.comwrote:
I want to iterate the second dimension of a 2-dim-array.

Let?s say I have an array:

double[,] myArray and a given index: int i.

Assume my index is given in want to iterate my array with something like
this:
int i = 2;
double sum= 0.0;

for(int j = 0; j < myArray[i].Length; j++)
sum += myArray[i,j];

But this does not work, because I am using the 2dim-array as 1dim-array.
My problem is... how do i get the size of the second dimension?
Use Array.GetLength (int dimension)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 26 '06 #4
Martin,

If you don't know anything about the array then you need to use the Array
class method and properties. Array is a base class for all arrays and as
such all arrays have them.
Check MSDN docs. Here are some usefull methods and proeprties that you may
want to use:
- Length (property) - total number of elements in all dimensions
- Rank (property) - number of dimensions
- GetLength (method) - return number of element is a specified dimension.
--
HTH
Stoitcho Goutsev (100)

"Martin Pöpping" <ma******@despa mmed.comwrote in message
news:ef******** **@newsreader2. netcologne.de.. .
DeveloperX schrieb:
>double[,] a=new double[10,10];

for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
a[i,j]=i*j;
}
}

Well maybe you missunderstand my problem.
I do not know the second dimension.
In your example.... I do not know that j = 10.
>//or try
foreach(doub le d in a)
{
Console.WriteL ine(d.ToString( ));
}

If it would work... it would traverse the whole dimensional array.

I only want to work on a[2,j] for example.
Regards,

Martin

Sep 26 '06 #5
In addition there is

GetUpperBound(d imension), returning the last index (length - 1)
On Tue, 26 Sep 2006 16:04:32 +0200, Stoitcho Goutsev (100) <10*@100.com>
wrote:
Martin,

If you don't know anything about the array then you need to use the Array
class method and properties. Array is a base class for all arrays and as
such all arrays have them.
Check MSDN docs. Here are some usefull methods and proeprties that you
may
want to use:
- Length (property) - total number of elements in all dimensions
- Rank (property) - number of dimensions
- GetLength (method) - return number of element is a specified dimension.



--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 26 '06 #6

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

Similar topics

9
3723
by: Steve Jorgensen | last post by:
Hi all, I'm working on the schema for a database that must represent data about stock & bond funds over time. My connundrum is that, for any of several dimension fields, including the fund name itself, the dimension may be represented in different ways over time, and may split or combine from one period to the next. When querying from the database for an arbitrary time period, I need the data to be rolled up to the smallest extent...
1
2134
by: Sara | last post by:
I am trying to use MDX to dynamically determine which hierarchy of the time dimension is selected. I have a calculated member which is using the last non-empty descendant of the . dimension, but I want it to work even if Fiscal is selected instead of Calendar. Here is the code SUM(Tail(Filter(Descendants(..CurrentMember, ), isEmpty(.) = False), 1), .)
0
1455
by: vsridhar420 | last post by:
I am trying to create a local cube from a cube which has a parant child dimension. Is the syntax for DIMENSION clause in the CREATE CUBE statement different for a parent-child dimension ? Because when I use syntax as with other dimensions, I am getting an error. Can Local cubes contain parent-child dimensions ? Thanks
2
1498
by: SRL | last post by:
Hi, This is probably a classic scenario with a shared dimension that we need to use in different cubes, where all fact tables do not offer the same level of detail. Dimension is snow-flaked. The cube that's causing me troubles was designed by marking the lowest dimension level Diabled and not Visible. This allows me to get rid of one of the snow-flake tables (the one with the lowest level), thus allowing an INNER JOIN with the...
2
12626
by: Tommo | last post by:
Chaps, Could someone please provide me some examples of iterating through a multimap. I wish to do the following 1. Iterate through each key in the multimap 2. For each key i find get all the possible values out so I can check each value Cheers
4
3516
dshimer
by: dshimer | last post by:
I have a file whose structure in strictly generic terms is similar to the following.keyname first keyword1 1.1 keyword2 1.2 keyword3 1.3 keyname second keyword1 2.1 keyword2 2.2 keyword3 2.3 keyname third keyword1 3.1
2
2813
by: Efrat Regev | last post by:
Hello, Let's say a probability vector of dimension d is x_1, ..., x_d, where each one is a non-negative term, and they all sum up to 1. Now I'd like to iterate over all probability vectors, but this is impossible, since they're uncountable. So instead, let's say I want to iterate over all such vectors under the constraint that the granularity of each component is at most some delta. To make things pythonesque, here's the interface:
0
1162
by: Mike K | last post by:
I asked this question in the OLAP group but got no response. I am hoping somebody will have some answers here. Thanks ------------------- HI, I need to create a dimension that will play off another dimension. For example: lets say we have branches going into discontinued operations time to time. What we would like to create is a dimension that will have the Discontinued status with Year. So something like:
6
1495
by: notnorwegian | last post by:
i have a big file with sentences, the first file of each sentence contains a colon(:) somewher eon that line i want to jump past that sentence. if all(x != ':' for x in line): this way i can check but i dont want to check for every line in the whole file, quite unnecessary when i only need to chekc the first line.
1
5611
by: evelina | last post by:
Hello, I need help. I have the following hashmap: HashMap<HashMap<Dimension, Integer>, String> mapList = new HashMap<HashMap<Dimension, Integer>, String>(); I want to extract Dimesion from the key, where the Integer is "1", and String from the value. How could I iterate it? I wrote that, but it doesn't work at the way I expected: HashMap<Dimension, String> singleValues = new HashMap<Dimension, String>();
0
8421
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.