Sử dụng mảng tính các giá trị thống kê


Bài tập: Viết một chương trình C# tính các giá trị thống kê như là : trung bình, trung vị, mode, và độ lệch chuẩn của một tập dữ liệu các số nguyên. Dữ liệu đầu vào được người sử dụng đưa vào từ bàn phím. Chương trình sẽ hiển thị kết quả đầu ra tương tự như trong hình dưới đây:

ThongKe

Giải pháp:


 class Program
{
    static void Main(string[] args)
    {
        int n;
        float mean, median, std;
        Console.Write("Nhap vao so phan tu mang:");
        n = int.Parse(Console.ReadLine());
        if (n < 3)
        {
            Console.WriteLine("Kich thuoc mang phai lon hon 2.");
        }
        else
        {
            //Khai báo một mảng kích thước n lưu trữ các số nguyên
            int[] dataset = new int[n];
            //allow user inputs
            int i = 0;
            for (i = 0; i < n; i++)
            {
                Console.Write("[{0}]:", i);
                dataset[i] = int.Parse(Console.ReadLine());
            }
            //Sắp xếp dữ liệu
            bubblesort(dataset, n);
            //Tính toán trung bình
            int sum = 0;
            int j = 0;
            while (j < n)
            {
                sum = sum + dataset[j];
                j++;
            }
            mean = (float)sum / n;
            //Tính toán trung vị
            //Nếu n là chẵn, median=dataset[n/2]
            //Nếu n là lẻ, median=(dataset[n/2]+dataset[1+n/2])/2
            //Chỉ số của mảng bắt đầu từ 0, viì vậy bạn cần trừ đi 1 
            //từ chỉ số được sử dụng trong tính toán trung bị
            if (n % 2 != 0) median = dataset[n / 2];
            else median = (dataset[(n / 2) - 1] + dataset[n / 2]) / (float)2;

            //Tính giá trị mode
            int[,] mode = new int[n, 2];
            //Khởi tạo mảng 2 chiều sắp xếp số lần xuất hiện của các giá trị và giá trị của chúng
            for (i = 0; i < 2; i++)
                for (j = 0; j < n; j++) mode[j, i] = 0;
            mode[0, 0] = 1;

            for (i = 0; i < n; i++)
                for (j = 0; j < n - 1; j++)
                    if (dataset[i] == dataset[j + 1]) { ++mode[i, 0]; mode[i, 1] = dataset[i]; }
            int max;
            int k = 0;
            max = mode[0, 0];
            for (j = 0; j < n; j++)
                if (max < mode[j, 0]) { max = mode[j, 0]; k = j; }
            //calculate standard deviation, std
            float temp = 0.0f;
            for (j = 0; j < n; j++)
            {
                temp = temp + (float)Math.Pow(dataset[j] - mean, 2);
            }

            std = (float)Math.Sqrt(temp / (n - 1));
            //Hiện thị kết quả 
            Console.WriteLine("Cac gia tri thong ke:");
            Console.WriteLine("..................................................");
            Console.WriteLine("Trung binh:{0}", mean);
            Console.WriteLine("Trung vi:{0}", median);
            if (mode[k, 1] != 0)
                Console.WriteLine("Mode:{0}", mode[k, 1]);
            else Console.WriteLine("Mode: khong co gia tri mode");
            Console.WriteLine("Do lech chuan {0}", std);
        }
        Console.ReadLine();
    }
    ///Thuật toán sắp xếp bubble sort
    static void bubblesort(int[] dataset, int n)
    {
        int i, j;
        for (i = 0; i < n; i++)
            for (j = n - 1; j > i; j--)
                if (dataset[j] < dataset[j - 1])
                {
                    int temp = dataset[j];
                    dataset[j] = dataset[j - 1];
                    dataset[j - 1] = temp;
                }
    }
}

Có thể bạn sẽ thích…

Trả lời

EnglishVietnamese