【题】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();
}
相关推荐
ghie90907 小时前
C# WinForms 条形码生成器(含保存和打印预览功能)
开发语言·c#
蒙塔基的钢蛋儿7 小时前
告别内存泄露与空指针:用C#与.NET 10开启STM32H7高性能单片机开发新纪元
stm32·c#·.net
ZoeJoy87 小时前
C# Windows Forms 学生成绩管理器(StudentGradeManager)—— 方法重载、out、ref、params 参数示例
开发语言·c#
solicitous7 小时前
历史与术语
学习·c#
第二只羽毛7 小时前
第三章 栈,队列和数组
大数据·数据结构·c#
biuba10247 小时前
18 openclaw事务管理:确保数据一致性的最佳实践
开发语言·ai·c#·编程·技术
fengfuyao9858 小时前
C# 高仿QQ截图工具(支持自定义快捷键)
开发语言·c#
时光追逐者8 小时前
一款基于 .NET 开源、跨平台应用程序自动升级组件
c#·.net·.net core
啸啸说8 小时前
1.图像的加载与保存
opencv·c#
yang_B6218 小时前
C# ISerializable 允许对象控制自己的序列化/反序列化过程
java·开发语言·c#