【题】C#-数组:二维数组

1. 将1~10000赋值给一个二维数组(100行100列)
cs 复制代码
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. 将二维数组的右上半部分置零
cs 复制代码
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. 求二维数组的对角线元素的和
cs 复制代码
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. 求二维数组中最大元素及其行列号
cs 复制代码
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
cs 复制代码
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();
}
相关推荐
c#上位机1 天前
halcon多个区域合并为1个区域—union1
c#·上位机·halcon·机器视觉
c#上位机1 天前
halcon图像增强——图像取反
图像处理·算法·c#·halcon
zwm2698888151 天前
悦龙台 监控掉线问题
c#
c#上位机1 天前
halcon图像去噪—导向滤波
图像处理·人工智能·计算机视觉·c#·halcon
切糕师学AI1 天前
Z.EntityFramework.Extensions.Core 如何批量删除数据?
c#
CHANG_THE_WORLD1 天前
Python 中的循环结构详解
开发语言·python·c#
c#上位机1 天前
halcon获取多个独立连通域—connection
图像处理·c#·halcon
烛阴2 天前
代码的“病历本”:深入解读C#常见异常
前端·c#
努力小周2 天前
基于STM32的智能台灯系统设计与实现
stm32·单片机·嵌入式硬件·c#·毕业设计·毕设·javaee
arron88992 天前
C# 项目源码进行全面的技术架构和调用逻辑分析。以下是系统性的技术方案
开发语言·架构·c#