zoukankan      html  css  js  c++  java
  • 二维数组(求平均值)

    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}	", a[i, j]);
                    }
                    num[i] = (double)sum[i] / a.GetLength(1);
                    Console.WriteLine("平均值:{0}", num[i]);
                    Console.WriteLine("
    ");
                }
            }
        }
    }


    二维数组(求平均值) 

    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("	");
                        sum[i] += a[i][j];
                    }
                    for (int n = 1; n <= temp - a[i].Length; n ++ )
                    {
                        Console.Write("	");
                    }
                    num = (double)sum[i] / a[i].Length;
                    Console.Write("平均值:{0:f2}",num);
                    Console.WriteLine();
                }
            }
        }
    }


    交错数组(求平均数)

    随机产生5行随机数,并每行都算出平均值

  • 相关阅读:
    Linux基础命令:crontab
    linux基础命令:su和sudo
    Linux基础知识:文件权限管理以及umask
    Linux基础命令:useradd和groupadd
    Linux基础命令:more和less
    Linux基础命令:ln
    Linux基础命令:chattr和lsattr
    Linux基础命令:who和w
    vue项目1-pizza点餐系统6-路由精讲之复用router-view
    vue项目1-pizza点餐系统5-全局守卫
  • 原文地址:https://www.cnblogs.com/rinack/p/4623856.html
Copyright © 2011-2022 走看看