Computer Science Canada

Mode Method Help

Author:  Carey [ Mon Jan 22, 2007 10:39 am ]
Post subject:  Mode Method Help

please help me improve my mode method. its only got one problem. the first number in the array does funny things. put in 1,1,1,2,2,2,2,2. the answer should be 5 instances of 2, but the ones get included in there for some reason. Twisted Evil

code:

public String mode()
    {
        int arrayCount = 0;
        int[] highCount = new int[3];              //how many times a number appears in the array
        double[] highNumber = new double[3];             //the number that appears most in the array
        int modeCount = 1;                      //temp counter for how many times a number appears in the array (set to one to start)
        double modeNumber = numbers[0];         //the number to start looking at and counting for
        for (int a = 1 ; a < Array.getLength(numbers) ; a++)    //for how many numbers in the array
        {
            if (numbers[a] == modeNumber)              //if the number is the same as the number you are checking for...
            {
                modeCount+=1;                   //add one to the count
            }
            else                                    //if it's not the same...
            {
                if (modeCount > highCount[arrayCount] && modeCount > 1)      //if the number appears more times than the current highCount...
                {
                    highCount[arrayCount] = modeCount;      //replace the record with the current stats
                    highNumber[arrayCount] = modeNumber;
                }
                else if (modeCount == highCount[arrayCount] && modeCount > 1)
                {
                    arrayCount +=1;
                    if (arrayCount > 2)
                    {
                        return "No mode";
                    }
                    highCount[arrayCount] = modeCount;      //replace the record with the current stats
                    highNumber[arrayCount] = modeNumber;
                }
                modeCount = 1;                  //set the modeCount to 1
                modeNumber = numbers[a];        //set the number you are checking for to the current number
            }//end if statement
        }//end for loop
        if (modeCount > highCount[arrayCount] && modeCount > 1)      //if the number appears more times than the current highCount...
        {
            highCount[arrayCount] = modeCount;      //replace the record with the current stats
            highNumber[arrayCount] = modeNumber;
        }
        else if (modeCount == highCount[arrayCount])
        {
            arrayCount +=1;
            if (arrayCount > 2)
            {
                return "No mode";
            }
            highCount[arrayCount] = modeCount;      //replace the record with the current stats
            highNumber[arrayCount] = modeNumber;
        }
        if (arrayCount == 0)
        {
            if (highCount[0] == 0)
            {
                return "No mode";
            }
            return highCount[0]+" of "+highNumber[0];
        }
        else if (arrayCount == 1)
        {
            return highCount[0]+" of "+highNumber[0]+", and "+highCount[1]+" of "+highNumber[1];
        }
        else if (arrayCount == 2)
        {
            return highCount[0]+" of "+highNumber[0]+", "+highCount[1]+" of "+highNumber[1]+", and "+highCount[2]+" of "+highNumber[2];
        }
        else
        {
            return "No mode";
        }
    }

Author:  wtd [ Mon Jan 22, 2007 11:00 am ]
Post subject:  RE:Mode Method Help

The code provided is insufficient. It's missing context. Where does this "numbers" array come from?

Author:  Carey [ Mon Feb 05, 2007 12:00 pm ]
Post subject:  Re: Mode Method Help

i don't need help on the rest of the program, but since you asked so nicely here is the whole thing. its a BlueJ project, so if you don't use BlueJ, just delete everything except the Statistics.java file. also ignore the standard deviation image.

Author:  Carey [ Tue Mar 20, 2007 11:16 am ]
Post subject:  Re: Mode Method Help

cmon people!!!! i need some help here!!!!


sorry about the necro-post, im desperate

Author:  McKenzie [ Tue Mar 20, 2007 7:57 pm ]
Post subject:  Re: Mode Method Help

I don't see a problem. After adding a main, I try your numbers 1,1,1,2,2,2,2,2 I get mode: 5 of 2. Exactly what I should get. Am I missing something here?


: