2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

Windows11に標準で搭載されているC#(ver5.0)コンパイラで配列の要素の合計、平均、配列の要素の値は平均より大きい?小さい?を調査するプログラムを実装しました。

問題

実行例に習い、以下のプログラムを作りなさい。
(1)長さ5のint型の配列を作成する。
(2)(1)の各要素に、1から10までの乱数を代入する。
(3)配列の内容をすべて表示する。
(4)配列の値の合計値と、平均値を表示する。
(5)平均値よりも大きい数を表示する
(6)平均値よりも小さい数を表示する。

という問題です。

実行結果例

画像の結果が一例です。

rei.jpg

実装したプログラムコード

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace prob57
{
    class prob57
    {
        public static void Main(string[] args){
            Random rnd = new Random();
            int num = 5;
            int[] array1 = new int[num];
            for(int i=0;i<num;i++){
                array1[i] = rnd.Next(1,10);
                Console.Write("{0} ",array1[i] );
            }
            Console.WriteLine();
            Console.WriteLine();
            int sum1 = array1.Sum();
            double avg = array1.Average();
            Console.WriteLine("配列の合計値は:{0}",sum1);
            Console.WriteLine("配列の平均値は:{0}",avg);
            string oavg = "";
            string uavg = "";
            foreach(int number in array1){
                if (number > avg){
                    oavg = oavg + Convert.ToString(number) + " ";
                } else if (number < avg) {
                    uavg = uavg + Convert.ToString(number) + " ";
                }
            }
            Console.WriteLine("平均より大きい数は:{0}",oavg);
            Console.WriteLine("平均より小さい数は:{0}",uavg); 
            
        }
    }    
}

実行結果

result.jpg

最後に

pythonでもやってみよっと。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?