كيفية حساب الMode ل Array 2D

moodyzz95 • منذ 7 سنوات

السلام عليكم 

اريد معرفة كيفية حساب الMode ( اكثر عدد متكرر ) في مصفوفة من البعد الثاني (لكل صف منفصل )

كمثال 

,{int [][] array= {{ 2 , 3, 2

;{{3,4,4}

المود للصف الاول هو 2

المود للصف الثاني هو 4

الطريقه الي عندي تقارن الاراي كامل وانا اريد كل صف على حده 

واعتذر اطا كان هناك علط في الصيغ لاني اكتب من الجوال 

كلمات دليلية:

ساعد بالإجابة

"إن في قضاء حوائج الناس لذة لا يَعرفها إلا من جربها، فافعل الخير مهما استصغرته فإنك لا تدري أي حسنة تدخلك الجنة."

الإجابات (1)

Ali Majrashi • منذ 7 سنوات

وعليكم السلام ورحمة الله وبركاته 

ياريت تستخدم المحرر لاضافة الأكواد بشكل كامل مع توضيح ماتعمل عليه لمعرفة الخطأ ومحاولة مساعدتك بشكل افضل

بالبحث وجدت هالكلاس

http://pages.cs.wisc.edu/~cs302-5/resources/examples/MeanMedianMode_Methods.java

وفيه methods لحساب mean, median, and mode لاي مصفوفه انا اخذت الميثود تبع حساب mode وعملت كلاس جديد بمثالك الي بالسؤال لتبسيطها لك كالتالي:


public class Mode {

    public static void main(String args[])
    {
        int [][] MyArray = {{2 , 3, 2},{3,4,4}};

        for (int i = 0; i < MyArray.length; i++) {
            System.out.println(calculateMode(MyArray[i]));
        }
    }

    /**
     * This method computes the mode of the values in the
     * input array.
     *
     * @param arr - an array of ints
     * @return mode - the mode of the input array
     */
    public static int calculateMode(int[] arr)
    {

        int modeCount = 0;	// The count of the mode value
        int mode = 0;		// The value of the mode

        int currCount = 0;
        int currElement;

        // Iterate through all values in our array and consider it as a possible mode
        for (int candidateMode : arr)
        {
            // Reset the number of times we have seen the current value
            currCount = 0;

            // Iterate through the array counting the number of times we see the current candidate mode
            for (int element : arr)
            {
                // If they match, increment the current count
                if (candidateMode == element)
                {
                    currCount++;
                }
            }

            // We only save this candidate mode, if its count is greater than the current mode
            // we have stored in the "mode" variable
            if (currCount > modeCount)
            {
                modeCount = currCount;
                mode = candidateMode;
            }
        }

        return mode;
    }
}

راح يطبع لك Mode 2,4 طبعا استخدمنا loop عشان نقدر نحسب mode لكل مصفوفه لحالها

لايوجد لديك حساب في عالم البرمجة؟

تحب تنضم لعالم البرمجة؟ وتنشئ عالمك الخاص، تنشر المقالات، الدورات، تشارك المبرمجين وتساعد الآخرين، اشترك الآن بخطوات يسيرة !