More newbie questions

A standard
Visual Basic:
Dim i As Integer

For Each i In multiDimArray
Next
will do it. I'm not totally sure what order it traverses it
(which dimension increments first in the loop), but I'm sure you
can figure that out.
 
Well that is VB code isn't it?

The array looks something like this. {{0,0}, {0,1}, {0,2}, {0,3}, {0,4}, {1,0}...........{4,4}


and it should look like this on the consolewindow:

0,0 0,1 0,2 0,3 0,4
1,0 1,1 1,2 1,3 1,4

and so on
 
Heh, sorry, I didn't realize this is the C# forum. In C# it would be
like this:
C#:
foreach(int i in theArray) {

}
 
Yes that's how far I got, but I can't get it to look the way I want it to. Someone told med that I had to use GetLength().
 
You know, you can always not use foreach; it may be more
appropriate, in this case, to use a standard for loop.
C#:
int i,j;
for (i=0;i<=myArray.GetLength();i++) 
  {
    for (j=0;j<=myArray.GetLength();j++)
    {                              
      // use myArray[i,j] to output the data
    }
  }
 
Oh. Well, I really don't know what to tell you. foreach can easily
get the data, but I have no idea how to get it out in the order that
you want it. Sorry. :-\
 
Why would you use foreach to traverse a 2-dimensional array?

Just curious. I assume your assignment gives some kind of reason. Perhaps I'm assuming too much :)
 
Nope no reason why and some friends have made it. Well assignement was a bad word to use, it's more like an excersice and we can use help from other people. There is a big difference between ripping code and getting help.
 
Done!

Code:
int[][] mul;

mul=new int[4][];

mul[0] = new int[5] {0,1,2,3,4};
mul[1] = new int[5] {0,1,2,3,4};
mul[2] = new int[5] {0,1,2,3,4};
mul[3] = new int[5] {0,1,2,3,4};
			
int counter=0;

foreach(int[] j in mul)
{
    foreach(int i in j)
    {
        Console.Write("{0},{1} ",counter,i);
    }
    
    Console.WriteLine();
    counter++;

}
 
Back
Top