C#二维数组(答案).doc
【精品文档】如有侵权,请联系网站删除,仅供学习与交流C#二维数组(答案).精品文档.c#程序设计基础必做习题集(5)十三、二维数组任务106:编写程序定义一个二维数组arr,该数组一共有4行3列,如下表所示,请把下表中的所有对应数据利用赋值语句赋值到该二维数组arr中去: arr 第0列 第1列 第2列18257913118111943279182576673第0行第1行第2行第3行编写程序依次把第2行第1列、第0行第2列、第3行第0列的元素值显示出来。参考代码:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2 class Program static void Main(string args) int, arr = new int4, 3; arr0,0=18; arr0,1=25; arr0,2=7; arr1,0=9; arr1,1=13; arr1,2=118; arr2,0=1119; arr2,1=4327; arr2,2=91; arr3,0=8; arr3,1=2576; arr3,2=673; Console.WriteLine("第2行第1列的元素是:0",arr2, 1); Console.WriteLine("第0行第2列的元素是:0", arr0, 2); Console.WriteLine("第3行第0列的元素是:0", arr3, 0); Console.ReadKey();任务107:编写程序定义一个二维数组arr,该数组一共有4行3列,如下表所示,请把下表中的所有对应数据利用赋值语句赋值到该二维数组arr中去: arr 第0列 第1列 第2列1802719113911411943786927673第0行第1行第2行第3行按照以下要求编写程序:(1) 利用循环显示第0行的所有元素。(2) 利用循环显示第1列的所有元素。(3) 利用循环求第2行所有元素的和。参考代码:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2 class Program static void Main(string args) int, arr = new int4, 3; arr0,0=180; arr0,1=2; arr0,2=71; arr1,0=91; arr1,1=139; arr1,2=114; arr2,0=119; arr2,1=437; arr2,2=86; arr3,0=9; arr3,1=276; arr3,2=73; Console.Write("所有第0行的数据:"); for (int i = 0; i <= 2; i+) Console.Write(arr0,i+" "); Console.Write("n所有第1列的数据:"); for (int i = 0; i <= 3; i+) Console.Write(arri, 1 + " "); Console.Write("所有第2行的数据:"); for (int i = 0; i <= 2; i+) Console.Write(arr2, i + " "); Console.ReadKey();任务108:编写程序定义一个二维数组arr,该数组一共有4行3列,如下表所示,请把下表中的所有对应数据利用赋值语句赋值到该二维数组arr中去: arr 第0列 第1列 第2列1802719113911411943786927673第0行第1行第2行第3行编写程序利用二重循环,把以上数组所有数组按照4行、3列的方式打印出来。参考代码:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2 class Program static void Main(string args) int, arr = new int4, 3; arr0,0=180; arr0,1=2; arr0,2=71; arr1,0=91; arr1,1=139; arr1,2=114; arr2,0=119; arr2,1=437; arr2,2=86; arr3,0=9; arr3,1=276; arr3,2=73; for (int i = 0; i <= 3;i+ ) for (int j = 0; j <= 2; j+) Console.Write(arri,j+" "); Console.Write("n"); Console.ReadKey();任务109:编写程序,定义一个4行,4列的二维数组,随机产生16个10到99之间的整数,放入其中,并把这4行4列的数据显示出来;把该数组的对角线元素显示出来,所谓的对角线元素如下图所示:注意:从左上到右下的称为正对角线元素,从左下到右上的称为斜对角线元素。参考代码:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2 class Program static void Main(string args) int, arr = new int4, 4; Random rnd=new Random(); Console.WriteLine("二维数组的内容如下:"); for (int i = 0; i <= 3;i+ ) /产生随机数放入数组 for (int j = 0; j <= 3; j+) arri, j = rnd.Next(10,100); Console.Write(arri,j+" "); Console.Write("n"); Console.Write("正对角线的元素有:"); for (int i = 0; i <= 3; i+) Console.Write(arri,i+" "); Console.Write("n斜对角线的元素有:"); for (int i = 0; i <= 3; i+) Console.Write(arr3-i, i + " "); Console.ReadKey();任务110:编写程序,定义一个4*4的二维数组,随机产生16个100到999之间的整数放入其中,并把这4行4列的数据显示出来;编写程序,把该二维数组每行的和、每列的和求出来。参考代码:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2 class Program static void Main(string args) int, arr = new int4, 4; Random rnd=new Random(); Console.WriteLine("二维数组的内容是:"); for (int i = 0; i <= 3; i+) /产生并显示二维数组的内容 for (int j = 0; j <= 3; j+) arri, j = rnd.Next(100,1000); Console.Write(arri,j+" "); Console.Write("n"); Console.Write("n"); Console.WriteLine("每行的和如下:"); for (int i = 0; i <= 3;i+ ) /求每行的和 int sum = 0; Console.Write("第0行的和=",i); for (int j = 0; j <= 3; j+) sum = sum + arri, j; Console.WriteLine(sum); Console.Write("n"); Console.WriteLine("每列的和如下:"); for (int i = 0; i <= 3; i+) /求每列的和 int sum = 0; Console.Write("第0列的和=",i); for (int j = 0; j <= 3; j+) sum = sum + arrj, i; Console.WriteLine(sum); Console.ReadKey();任务111:编写程序,定义一个6*8的二维数组,随机产生48个10到99之间的整数,放入该二维数组中。把该数组的内容按照6行8列的形式显示出来。再求出该数组中的最大值和最小值。参考代码:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2 class Program static void Main(string args) int, arr = new int6, 8; Random rnd=new Random(); for (int i = 0; i <= 5; i+) /随机产生并显示数组的所有数据内容 for (int j = 0; j <= 7; j+) arri,j=rnd.Next(10,100); Console.Write(arri,j+" "); Console.Write("n"); int max=-100000; int min=+100000; for (int i = 0; i <= 5; i+) for (int j = 0; j <= 7; j+) if (arri, j > max) /求最大 max = arri, j; if (arri, j < min) /求最小 min = arri, j; Console.WriteLine("最大值=0",max); Console.WriteLine("最小值=0",min); Console.ReadKey();任务112:编写程序,定义一个5*5的二维数组,该数组中每个元素的值是行下标和列下标的平方和,把这个二维数组的内容全部显示出来。参考代码:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2 class Program static void Main(string args) int, arr = new int5, 5; for(int i=0;i<=4;i+) /初始化数组内容 for (int j = 0; j <= 4;j+ ) arri,j=i*i+j*j; for (int i = 0; i <= 4; i+) /显示数组内容 for (int j = 0; j <= 4; j+) Console.Write(arri,j+" "); Console.Write("n"); Console.ReadKey();任务113:编写程序,定义一个10*10的二维数组,随机产生100个10到99之间的整数放入其中,请把该数组的内容全部显示出来。再编写程序,对这个二维数组的每行进行升序排序,把排序后的二维数组的内容显示出来。参考代码:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2 class Program static void Main(string args) int, arr = new int10, 10; Random rnd = new Random(); Console.WriteLine("排序后的内容:"); for (int i = 0; i <= 9; i+) /产生并显示数组 for (int j = 0; j <= 9; j+) arri, j = rnd.Next(10,99); Console.Write(arri, j + " "); Console.Write("n"); for (int p = 0; p <= 9; p+) /对每行进行排序 for (int i = 0; i <= 8; i+) for (int j = i + 1; j <= 9; j+) if (arrp, i > arrp, j) int temp = arrp, i; arrp, i = arrp, j; arrp, j = temp; Console.WriteLine("排序后的内容:"); for (int i = 0; i <= 9; i+) /显示排序数组 for (int j = 0; j <= 9; j+) Console.Write(arri, j + " "); Console.Write("n"); Console.ReadKey();方法二:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2 class Program static void Main(string args) int, arr = new int10, 10; Random rnd = new Random(); Console.WriteLine("排序前的内容:"); for (int i = 0; i <= 9; i+) /产生并显示数组 for (int j = 0; j <= 9; j+) arri, j = rnd.Next(10,99); Console.Write(arri, j + " "); Console.Write("n"); for (int p = 0; p <= 9; p+) /对每行进行排序 for (int i = 0; i < 9; i+) for (int j = 0; j < 9 - i;j+ ) if (arrp, j > arrp, j + 1) int t = arrp, j; arrp, j = arrp, j + 1; arrp, j + 1 = t; Console.WriteLine("排序后的内容"); for(int i=0;i<=9;i+) for(int j=0;j<=9;j+) Console .Write (arri,j+" "); Console .WriteLine (); Console.ReadLine();任务114:编写程序把以下值通过赋值语句赋值给一个二维数组:12 65 34 12 3545 18 23 56 3871 90 18 33 9223 31 63 34 8795 83 37 22 91请求出该二维数组每行的最大值和每列的最小值。参考代码:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2 class Program static void Main(string args) int, arr = new int5, 5 12 , 65 , 34 , 12 , 35, 45 , 18 , 23 , 56 , 38, 71 , 90 , 18 , 33 , 92, 23 , 31 , 63 , 34 , 87, 95 , 83 , 37 , 22 , 91 Console.WriteLine("数组内容如下:"); /显示数组内容 for (int i = 0; i <= 4; i+) for (int j = 0; j <= 4; j+) Console.Write(arri,j+" "); Console.Write("n"); Console.WriteLine("每行的最大值如下:"); for(int i=0;i<=4;i+) /求每行最大值 int max=arri,0; for (int j = 1; j <= 4; j+) if (arri, j > max) max = arri, j; Console.WriteLine("第0行的最大值=1",i,max); Console.WriteLine("每列的最小值如下:"); for (int i = 0; i <= 4; i+) /求每列的最小值 int min = arr0, i; for (int j = 1; j <= 4; j+) if (arrj, i < min) min = arri, j; Console.WriteLine("第0列的最小值=1",i,min); Console.ReadKey();任务115:利用二维数组打如下的图形: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 11 5 10 10 5 1思路:可以先考虑打印如下图形:11 11 2 11 3 3 11 4 6 4 11 5 10 10 5 1参考代码:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2 class Program static void Main(string args) int, arr = new int6, 6; for (int i = 0; i <= 5;i+ ) /把第1列全部设为1 arri,0=1; for(int i=0;i<=5;i+) /把对角线设为1 arri,i=1; for (int i = 2; i <= 5;i+ ) /构造杨辉三角形 for (int j = 1; j <= i - 1;j+ ) arri,j=arri-1,j+arri-1,j-1; int p = 30; for (int i = 0; i <= 5; i+) /显示出来 for (int j = 0; j < p;j+) Console.Write(" "); p = p - 1; for (int j = 0; j <= i; j+) Console.Write(arri,j+" "); Console.Write("n"); Console.ReadKey();