multidimensional array - C# - Create hollow rectangle -


i'm using multidimensional array create "border". if printed, this:

###### #    # #    # #    # ###### 

the code have far can create top, left, , bottom borders. @ moment don't know how create border right hand side of it.

int[,] array = new int[10, 10]; //create border around box //top (int = 0; < array.getlength(0); i++) {     array[0, i] = 1; }  //bottom (int = 0; < array.getlength(0); i++) {     array[array.getlength(0) - 1, i] = 1; }  //left (int = 0; < array.getlength(0); i++) {     array[i, 0] = 1; } 

how go creating border on right? also, think code improved, i'm new c#.

thanks

the right border

the right border reflection of bottom border along diagonal (top-left bottom-right). so, take bottom drawing code , invert x, , y coordinates. gives:

// right (int = 0; < array.getlength(0); i++) {     array[i, array.getlength(0) - 1] = 1; } 

code improvements

your code correct. suggest 2 improvements:

first, in c#, array dimensions cannot changed after creation of array , know size of array: 10. so, let's replace array.getlength(0) int called arraysize.

const int arraysize = 10;     int[,] array = new int[arraysize, arraysize]; //create border around box  //top (int = 0; < arraysize; i++) {     array[0, i] = 1; }  //bottom (int = 0; < arraysize; i++) {     array[arraysize - 1, i] = 1; }  //left (int = 0; < arraysize; i++) {     array[i, 0] = 1; }  // right (int = 0; < arraysize; i++) {     array[i, arraysize - 1] = 1; } 

second improvements. use multiple times same loops. let's merge them together.

const int arraysize = 10;     int[,] array = new int[arraysize, arraysize];  // create border around box (int = 0; < arraysize; i++) {     array[0, i] = 1;  // top     array[arraysize - 1, i] = 1;  // bottom     array[i, 0] = 1;  // left     array[i, arraysize - 1] = 1;  // right } 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -