วันอังคารที่ 31 มีนาคม พ.ศ. 2558

C# Calculate Standard Deviation (Using List Object)

ไม่มีความคิดเห็น:
สร้าง Class StandardDeviation ไว้คำนวนค่าหาส่วนเบี่ยงเบนมาตรฐาน(S.D.) ในกรณีข้อมูลไม่ได้มีการแจกแจงความถี่
public class StandardDeviation {
        private double ExP2 { set; get; } //Ex^2
        private double Ex2 { set; get; }//(Ex)^2
        private List listScore { set; get; }
        private int n { set; get; }

        private double sum {set;get;}

        public StandardDeviation(List scores){
            this.listScore = scores;
            this.n = scores.Count;
        }

        public double Calculator(){
             calExP2();
             CalEx2();
             /*แทนค่าในสูตร 
                สูตร s.d = รูท nEx^2 - (Ez)^2 / n(n-1)
            */
            double g1 = this.n*this.ExP2 - this.Ex2;
            int g2 = this.n * (this.n - 1);

            sum = Math.Sqrt(g1 / g2);
             return sum;
        }
        private void calExP2(){
            for (int i = 0; i < this.listScore.Count; i++) {
                this.ExP2 = this.ExP2 + (this.listScore[i] * this.listScore[i]);
            }

            
        }

        private void CalEx2() {
            for (int i = 0; i < this.listScore.Count; i++ )
            {
                this.Ex2 = this.Ex2 + this.listScore[i];
            }
            this.Ex2 = (this.Ex2 * this.Ex2);
        }
    }
ตัวอย่างการเรียกใช้งาน
จากตัวอย่าง : http://www.stvc.ac.th/elearning/stat/csu3.html
//Ex2.จากข้อมูลต่อไปนี้จงหาส่วนเบี่ยงเบนมาตรฐาน 1, 2, 4, 6, 8, 9
            List listScore = new List();
            listScore.Add(1d);// 1, 2, 4, 6, 8, 9
            listScore.Add(2d);
            listScore.Add(4d);
            listScore.Add(6d);
            listScore.Add(8d);
            listScore.Add(9d);

            //1.การหาส่วนเบี่ยงเบนมาตรฐาน(S.D.) ในกรณีข้อมูลไม่ได้มีการแจกแจงความถี่
            StandardDeviation sd = new StandardDeviation(listScore);
            Console.WriteLine("SD : "+ sd.Calculator());
            Console.ReadLine();
ผลการรันโปรแกรม

Read More

C# Calculate Grade T-Score (Using List Object)

ไม่มีความคิดเห็น:
สมมุติ มีคะแนนนักเรียนดังนี้
คนที่ 1 มี 1 คะแนน
คนที่ 2 มี 2 คะแนน
คนที่ 3 มี 4 คะแนน
คนที่ 4 มี 6 คะแนน
คนที่ 5 มี 8 คะแนน
คนที่ 6 มี 9 คะแนน

ให้ตัดเกรดของนักเรียนเหล่านี้ โดยวิธีตัดเกรดด้วย T-Score
ที่มาของโจทย์ : http://www.stvc.ac.th/elearning/stat/csu3.html
ใช้การคำนวณ SD จากบทความที่แล้ว C# Calculate Standard Deviation (Using List Object)
class Program
    {
        static void Main(string[] args)
        {
            //Ex2.จากข้อมูลต่อไปนี้จงหาส่วนเบี่ยงเบนมาตรฐาน 1, 2, 4, 6, 8, 9

            List listScore = new List();
            listScore.Add(1d);// 1, 2, 4, 6, 8, 9
            listScore.Add(2d);
            listScore.Add(4d);
            listScore.Add(6d);
            listScore.Add(8d);
            listScore.Add(9d);

            //1.การหาส่วนเบี่ยงเบนมาตรฐาน(S.D.) ในกรณีข้อมูลไม่ได้มีการแจกแจงความถี่
            StandardDeviation sd = new StandardDeviation(listScore);
            //จาก สมการ T = 10Z + 50
            //1 หา z = (14- ค่าเฉลี่ยเรขาคณิต ) / SD
            double sd_ = sd.Calculator();//3.22
            double xbar = average(listScore);//1.5
            for (int i = 0; i < listScore.Count; i++) { 
                double z = (listScore[i] - xbar) / sd_; //14 
                //Console.WriteLine("Z Score : " + z);
                double t = 10 * z + 50;
                Console.WriteLine(listScore[i]+" To  T Score : " + t);
            }
            Console.ReadLine();

        }
        static double average(List listScore)
        {
            double sum = 0.0d;
            int n = listScore.Count;
            for (int i = 0; i < listScore.Count; i++)
            {
                sum = sum + listScore[i];
            }
            double result = sum / n;


            return result;            
        } 

    }
รันแล้วเราก็จะได้ คะแนนนักเรียนออกมา

จากนั้นเอาโปรแกรมตัดเกรดจากบทความ c# Grade Calculator (Using List Object) มาใช้
static void Main(string[] args)
        {
            //Ex2.จากข้อมูลต่อไปนี้จงหาส่วนเบี่ยงเบนมาตรฐาน 1, 2, 4, 6, 8, 9

            List listScore = new List();
            listScore.Add(1d);// 1, 2, 4, 6, 8, 9
            listScore.Add(2d);
            listScore.Add(4d);
            listScore.Add(6d);
            listScore.Add(8d);
            listScore.Add(9d);

            //ตั้งค่าเกรด
            List ls = new List();
            ls.Add(new Grades(49f, "F"));
            ls.Add(new Grades(49.5f, "D"));
            ls.Add(new Grades(55f, "D+"));
            ls.Add(new Grades(60f, "C"));
            ls.Add(new Grades(65f, "P"));
            ls.Add(new Grades(70f, "B"));
            ls.Add(new Grades(75f, "B+"));
            ls.Add(new Grades(80f, "A"));

            //1.การหาส่วนเบี่ยงเบนมาตรฐาน(S.D.) ในกรณีข้อมูลไม่ได้มีการแจกแจงความถี่
            StandardDeviation sd = new StandardDeviation(listScore);
            //จาก สมการ T = 10Z + 50
            //1 หา z = (14- ค่าเฉลี่ยเรขาคณิต ) / SD
            double sd_ = sd.Calculator();//3.22
            double xbar = average(listScore);//1.5
            for (int i = 0; i < listScore.Count; i++)
            {
                double z = (listScore[i] - xbar) / sd_; //14 
                //Console.WriteLine("Z Score : " + z);
                double t = 10 * z + 50;
                //Console.WriteLine(listScore[i] + " To  T Score : " + t);
                Console.WriteLine(listScore[i] + " To  T Score : " + t + " -> grade : " + CalGrade((float)t, ls));
            }   

            Console.ReadLine();

        }
จะได้ผลเกรดดังนี้


อยากได้โค้ดทั้งหมด ติดต่อผมทางได้นี้นะครับ (ไม่มีค่าใช้จ่ายใดๆแค่อยากรู้ว่ามีคนเอาใช้จริงๆหรือเปล่า)

Facebook: https://www.facebook.com/DotNetUnderLine
E-mail: agkasit.ecp7@gmail.com
Read More