Thread: write a method that calculates the mode of integers within an array?

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    Post write a method that calculates the mode of integers within an array?

    I have a program where the user inputs numbers and the program calculates the mean median and mode. everything works fine except the mode method, and I dont know why. here is some of the code. I included a method I call that sorts the array, and that may or may not be the problem, but otherwise I just want this to work!!! :/



    Code:
    void sortArray(int nums[], int total) {
        int x;
        int y;
        for(x=0; x<total; x++) {
            for(y=0; y<total-1; y++) {
                if(nums[y]>nums[y+1]) {
                    int temp = nums[y+1];
                    nums[y+1] = nums[y];
                    nums[y] = temp;
                }
            }
        }
    }
    
    int findMode(int nums[],int total) {
         int i, j, maxCount, modeValue;
        int tally[total];
        for (i = 0; i < total; i++) {
            tally[nums[i]]++;
        }
         maxCount = 0;
        modeValue = 0;
        for (j = 0; j < total; j++) {
            if (tally[j] > maxCount) {
                maxCount = tally[j];
                modeValue = j;
            }
        }
        return modeValue;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,630
    Does this code compile without errors? If not post the complete error messages exactly as they appear in your development environment. If it does compile then what is wrong with your snippet? What is it doing, not doing that is incorrect? One thing you may want to insure is that you are not accessing your arrays out of bounds.


    Jim

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,866
    One suggestion: initialize
    Code:
    int tally[total]={0};
    to all zero's, before you start incrementing it's values.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,677
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. haw to write a pixel in mode 0x101
    By joseCarlos in forum C++ Programming
    Replies: 0
    Last Post: 10-26-2009, 05:19 AM
  2. How to give write access mode to folder in Vista
    By Bargi in forum Windows Programming
    Replies: 1
    Last Post: 08-17-2007, 02:40 PM
  3. Program runs and calculates before all integers are input
    By thewizardalbany in forum C Programming
    Replies: 7
    Last Post: 07-30-2006, 05:01 AM
  4. Read/Write Method
    By bigpmc in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2004, 03:28 PM
  5. File Encryption & Read/Write in Binary Mode
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 06:45 PM

Tags for this Thread