二维数组(求平均值)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace twoArray { class Program { static void Main(string[] args) { int [,]a = new int[10,5]; Random ran = new Random(); int[] sum = new int[10]; double[] num = new double[10]; for (int i = 0; i < a.GetLength(0); i++) { Console.Write("第{0}个学生的成绩: ",i+1); for (int j = 0; j < a.GetLength(1); j++) { a[i , j] = ran.Next(100); sum[i] += a[i , j]; Console.Write(" {0}\t", a[i, j]); } num[i] = (double)sum[i] / a.GetLength(1); Console.WriteLine("平均值:{0}", num[i]); Console.WriteLine("\n"); } } } }
二维数组(求平均值)
1.随机产生10个学生的5项成绩并求出平均值
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace staggerArray { class Program { static void Main(string[] args) { int [][] a=new int[5][]; Random ran = new Random(); int[] sum = new int[a.Length]; double num = 0; int i , temp=0; for ( i = 0; i < a.Length;i++ ) { a[i] = new int[ran.Next(5)+1]; if(temp < a[i].Length){ temp = a[i].Length; } } for (i = 0; i < a.Length; i++) { Console.Write("第{0}行:", i + 1); for(int j = 0;j < a[i].Length;j++){ a[i][j] = ran.Next(100)+1; Console.Write(a[i][j]); Console.Write("\t"); sum[i] += a[i][j]; } for (int n = 1; n <= temp - a[i].Length; n ++ ) { Console.Write("\t"); } num = (double)sum[i] / a[i].Length; Console.Write("平均值:{0:f2}",num); Console.WriteLine(); } } } }
交错数组(求平均数)
随机产生5行随机数,并每行都算出平均值