2 D配列(平均値を求める)2 Dはいれつ
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("
");
}
}
}
}
2 D配列(平均値を求める)2 Dはいれつ
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行の乱数を生成し、行ごとに平均値を算出する