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

This is a discussion on write a method that calculates the mode of integers within an array? within the C Programming forums, part of the General Programming Boards category; I have a program where the user inputs numbers and the program calculates the mean median and mode. everything works ...

  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
    2,902
    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,868
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 06: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, 03: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, 06:01 AM
  4. Read/Write Method
    By bigpmc in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2004, 04: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21