Lost
Lost

Reputation: 1

issues on using mode in C#

I'm having issues with my Mode and getting it to let me input more then 10 numbers!

Those are the two issues I am having with my code.

        public static void Main(string[] args)
        {

            int[] myNums = new int[10];
            int number = 0;
            int count = 0;
            Console.WriteLine("--Nmber Modifier--\n");
            Console.WriteLine("Entering a number that is no between 1 and 10 will end your process");
            do// limited to 10 HELP
            {
                number = Util.PromptForInt("Enter a number between 1 and 10 : ");
                if ((number <= 10) && (number >= 1))
                {
                    myNums[count] = number;
                }
                count++;
            }
            while ((number <= 10) && (number >= 1));
            Array.Sort(myNums);


            Console.WriteLine("Average number is : " + MeantAverage(myNums));
            Console.WriteLine("Largest Number is : " + LargestNum(myNums));
            Console.WriteLine("Smallest Number is : " + SmallestNum(myNums));
            Console.WriteLine("Most common number is : " + Mode(myNums));
            Console.ReadLine();
        }

        static double MeantAverage(int[] nums)
        {

            double dMeanAverage;
            double dSum = 0;
            var groups = nums.GroupBy(item => item);

            foreach (var group in groups)
            {
                dSum = group.Key + dSum;
            }

            dMeanAverage = dSum / nums[nums.Length - 1];
            return Math.Round(dMeanAverage, 2);
        }
        static int LargestNum(int[] nums)
        {
            int highestNum;
            highestNum = nums[nums.Length - 1];
            return highestNum;
        }
        static int SmallestNum(int[] nums)
        {

            int lowest = 0;

            for (int b = 0; b < nums.Length; b++)
            {
                if (nums[b] > lowest)
                {
                    lowest += nums[b];

                    return lowest;
                }
            } return lowest;
        }
        static int Mode(int[] nums)
        {
            // issues with mode
            int modes = 0;

            var modeGroup = nums.GroupBy(v => v);
            int max = modeGroup.Max(g => g.Count());
            modes = modeGroup.First(g => g.Count() == max).Key;
            return modes;
        }
    }
}

Upvotes: 0

Views: 70

Answers (2)

Daniel MesSer
Daniel MesSer

Reputation: 1181

Modified code somewhat, intention is the same though.

  • You forgot to check against count in the while case
  • You will crash on '0' inputs (no safe guard against empty array)
  • Changed Mode to merge items and then sort descending

    var myNums = new List<int>(10);
    for(int i=0; i < 10; ++i)
    {
        int number = Utils.PromptForInt("Enter a number between 1 and 10 : ");
        if(number > 10 || number < 1)
            break;
        myNums.Add(number);
    }
    if(myNums.Count < 1)
        return; //no item input, do something
    
    myNums.Sort();
    Console.WriteLine("Average: {0}", myNums.Sum() / (double)myNums.Count);
    Console.WriteLine("Largest: {0}", myNums[myNums.Count - 1]);
    Console.WriteLine("Smallest: {0}", myNums[0]);
    
    var result = myNums.GroupBy(n => n)
                .Select(c => new { Key = c.Key, total = c.Count() })
                .OrderByDescending( a => a.total);
    Console.WriteLine("Most common: {0}, used {1} times", result.First().Key, result.First().total);
    

Upvotes: 0

BradleyDotNET
BradleyDotNET

Reputation: 61369

You created an array of ten numbers:

int[] myNums = new int[10];

So while your loop doesn't restrict you to 10 numbers because you don't check against count, the system does because as soon as you try to access the 10th element (myNums[10]) you will get an IndexOutOfRangeException.

Since you don't catch it anywhere, its just going to terminate your program.

To solve your problem:

  • Check against count so you don't input too many numbers!
  • If you need a variable length collection, use a collection built for that like List<T> instead of an array. Arrays are fixed-length (mostly), and the way around that is a horrible misuse of the array semantic.

Upvotes: 1

Related Questions