✅ How can I print a 2D array
I have a 2D array, how can i print each row of the array, on console
24 Replies
The array will not get bigger or small, I just need to change the contents
this is the array
print it like
0000000
0000000
0000000
etc?
yeah
i made code but only prints first row
show me
with two for loops
ah yes
youre printing out only a diognal
[i,i]
do you see the problem?
i tried i by itself but doesnt work
you need [0,1] [0,2] [0,3] [1,0] [1,1] [1,2] etc not [0,0] [1,1] [2,2] like youre doing now
yeah so would i need to like
iterate i
make another for loop?
yes
a loop for x and a loop for y
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
int element = array[i, j];
Console.WriteLine(element);
}
}
yeah im doing that rn
dont spoil everything
And why did you create array in this way?
why not?
basically i need to make a cinema seat booking program (as you can tell from the array name)
0 represents an empty seat
and the seats are 6x8
I didn't, look at the printing
okay
if you used an array like bool[][] which is an array of bool arrays instead of a 2d array
then i would suggest
for (bla bla length) // goes trough the array of arrays
Console.WriteLine(string.Join(null, seats[i])); // prints the whole array
yeah ok that would also set the whole thing to 0
You can have other default values too
i need them to be stored at different values even if they all start at 0
done ty for help
Closed!