1. 将1~10000赋值给一个二维数组(100行100列)
int[,] array = new int[100,100];
int index = 1;
for(int i = 0;i < array.GetLength(0);i++){for(int j = 0;j < array.GetLength(1);j++){array[i,j] = index;++index;}
}
2. 将二维数组的右上半部分置零
int[,] array = new int[4,4];
Random r = new Random();
for(int i = 0;i < array.GetLength(0);i++){for(int j = 0;j< array.GetLength(1);j++){if(i <= 1 && j >1) array[i,j] = 0;else array[i,j] = r.Next(1,101)Console.Write(array[i,j]+" ");}Console.WriteLine();
}
3. 求二维数组的对角线元素的和
int[,] array = new int[3,3];
Random r = new Random();
for(int i = 0;i < array.GetLength(0);i++){for(int j = 0;j< array.GetLength(1);j++){array[i,j] = r.Next(1,11);if(i==j || i+j==2) sum+=array[i,j];Console.Write(array[i,j]+" ");}Console.WriteLine();
}
Console.WriteLine(sum);
4. 求二维数组中最大元素及其行列号
int[,] array = new int[5,5];
Random r = new Random();
//记录最大值的行列号
int maxI=0;
int maxJ=0;
for(int i=0;i<array.GetLength(0);i++){for(int j=0;j<array.GetLength(1);j++){array[i,j] = r.Next(1,501);Console.Write(array[i,j]+" ");//找最大值if(array[maxI,maxJ] < array[i,j]){maxI = i;maxJ = j;}}Console.WriteLine();
}
5. 在一个M*N的二维数组中,数组元素的值为0或1。转换数组:将含有1的行和列全置为1
int[,] array = new int[5,5];
Random r = new Random();
bool[] hang = new bool[5];
bool[] lie = new bool[5];for(int i = 0;i<array.GetLength(0);i++){for(int j = 0;j<array.GetLength(1);j++){if(array[i,j] ==1){hang[i] = true;lie[i] = true;}Console.Write(array[i,j] + " ");}Console.WriteLine();
}
for(int i = 0;i < array.GetLength(0);i++){for(int j = 0;j<array.GetLength(1);j++){if(hang[i] || lie[i]array[i,j] = 1;Console.Write(array[i,j]+" ");}Console.WriteLine();
}