views:

67

answers:

5

Hello,

I got a 2-dimentional array containing boolean values written in C#. The cols and rows of the array are to be determined by the user upon creation of the array. I then want to print out the array and it´s containing values onto the console in order.

For example like this, how is this done in C#?

ROWS - COLS - VALUE


1 - A - True

1 - B - True

1 - C - True

1 - D - True


2 - A - True

2 - B - False

2 - C - False

2 - D - True


A: 
foreach (Cell[] rows in cells) {
    foreach (Cell c in rows) {
        // print id, c.letter, c.value
    }
}
Sjoerd
Pretty sure that doesn't work in C# for some reason. (Edit: of course it does, just not for staggered arrays (bool[,] arr)
Graphain
+4  A: 
for (int row = 0; row < array.Length; row++)
{
    Console.WriteLine("--------------------------------");

    for (int col = 0; col < array[row].Length; col++)
    {
        Console.WriteLine("{0} - {1} - {2}", row + 1, 'A' + col, array[row][col]);
    }
}

Something like that.

ErikHeemskerk
Others should upvote this as the OP won't mark an accepted and this was first correct.
Graphain
+1  A: 

The idea of this assignment is to learn it yourself. Put some code up of what you've tried first. I really don't think anyone should be answering these kinds of questions unless they have some code already and can't work out where they're going wrong.

Graphain
No, this is no homework, I am trying to learn C# because my boss wants our firm to offer C# to our customers. Stupid if you ask me.
Presumably you know another language already if you're being asked this?
Graphain
And if you don't, you're not going to gain the mental model of a multidimensional array by just asking for code that prints a particular format.
Graphain
Yeah, I know but my boss is breathing down my neck. We need to be ready by july because one of our better customers are starting to request more programming services from us. I really just need to get ahead of this "deadline". Thanks for all help!
If you need to learn a new language completely from scratch it's already too late.
Phil
Mm, I know, but tell that to my boss... :)
@Phil It's too late if he doesn't know multi-dimensional arrays anyway. Patching a new language is doable but learning language fundamentals isn't going to happen in less than 6 months.
Graphain
A: 

Thanks guys! I think I got how it works now so will be moving on. You are as awesome as always.

Take care and if you got your ways past Chicago, I´ll buy you a beer!

You mark a question as answered by clicking the check mark next to the answer that was most helpful.
Phil
+2  A: 

Here's is another approach and a complete example. I like working with Lambda but making the first foreach to a lambda aswell wouldn't be very pretty.

var array = new[]
                {
                    new [] { true, false, true, true, false },
                    new [] { false, false, true, false, false },
                    new [] { true, true , true, true, true}
                };

var rowId = 1;
var colId = 0;

foreach (var row in array)
{
    row.ToList().ForEach(value => Console.WriteLine(
         string.Format("{0} - {1} - {2}", rowId, (char)('A' + colId++), value)));

    rowId += 1;

    Console.WriteLine("--------------------------");
}

Will output the following:

1 - A - True
1 - B - False
1 - C - True
1 - D - True
1 - E - False
--------------------------
2 - A - False
2 - B - False
2 - C - True
2 - D - False
2 - E - False
--------------------------
3 - A - True
3 - B - True
3 - C - True
3 - D - True
3 - E - True
--------------------------
Filip Ekberg
Ha I don't think the OP will handle lambdas if they can't handle multidimensionals (or even work out a Google reference on it).
Graphain
You never know! :)
Filip Ekberg