Thread: help using arrays and calculating mean median and mode

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

    Post help using arrays and calculating mean median and mode

    My code is below. I have a program that compiles and works, I just have a few problems with it. the mode method isn't working, and the median doesn't work either. if the user enters 4 (to enter 4 integers) the median will be the 2 middle numbers average. so if the numbers are : 1 4 5 30, the median would be 4.5000. also, my goAgain() method doesn't work, and I dont know why. I want to ask the user if he / she wants to play again, enter y or Y for yes and n or N for no, but even if they do enter those letters, it just goes "invalid, try again" and never works right. PLEASE HELP!!! thank you

    Code:
    #define MAX 25
    #include<stdio.h>
    #include <stdbool.h>
    #include <time.h>
    #include <stdlib.h>
    
    int readTotalNums();
    void fillArray(int total, int nums[]);
    void sortArray(int nums[], int total);
    double findMean(int nums[], int total);
    double findMedian(int nums[], int total);
    int findMode(int nums[], int total);
    void printResults(double mean, double median, double mode);
    bool goAgain();
    
    int main()  {
        int nums[MAX];
        int total;
        double mean, median, mode;
        do {
            total = readTotalNums();
            fillArray(total, nums);
            sortArray(nums, total);
            mean = findMean(nums, total);
            median = findMedian(nums, total);
            mode = findMode(nums, total);
            printResults(mean, median, mode);
        } while (goAgain()==true);
        return 0;
    }
    
    int readTotalNums() {
        int num;
        do {
            printf("How many numbers? ");
            scanf("%i", &num);
        } while (num < 1 || num > 25);
        return num;
    }
    
    void fillArray(int total, int nums[]) {
        int temp;
        int i;
        printf("Please enter %i numbers\n", total);
        for (i = 0; i <= total-1; i++) {
           scanf("\n%i",&nums[i]);
        }
    }
    
    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;
                }
            }
        }
    }
    
    double findMean(int nums[], int total) {
        int i;
        double sum = 0.0;
        for(i = 0; i < total; i++) {
            sum += nums[i];
        }
        return (sum/total);
    }
    
    double findMedian(int nums[], int total) {
        int temp;
        int i,j;
        for(i=0;i<total;i++)
            for(j=i+1;j<total;j++) {
                if(nums[i]>nums[j]) {
                    temp=nums[j];
                    nums[j]=nums[i];
                    nums[i]=temp;
                }
            }
            if(total%2==0) {
                int x;
                int y;
                x = nums[total/2];
                y = nums[(total/2) - 1];
    
                return ((x+y)/2);
            }else {
                return nums[total/2];
            }
    }
    
    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;
    }
    
    void printResults(double mean, double median, double mode) {
        printf("Mean: %f\tMedian: %f\tMode: %f", mean, median, mode);
    }
    
    bool goAgain() {
        char *temp;
        printf("\nWould you like to play again(Y/N)? ");
        scanf("%s", &temp);
        while (temp != 'n' && temp != 'N' && temp != 'y' && temp != 'Y') {
            printf("\nI am sorry that is invalid -- try again");
            printf("\nWould you like to play again(Y/N)? ");
            scanf("%s", &temp);
        }
        if (temp == 'y' || temp == 'Y') {
            return true;
        } else {
            return false;
        }
    }
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    > so if the numbers are : 1 4 5 30, the median would be 4.5000.
    You need to force the calculation to use double arithmetic.

    Say
    return ((x+y)/2.0);

    As for goAgain, that's just plain wrong when it comes to reading a string.

    Consider something along the lines of
    Code:
        char temp;
        printf("\nWould you like to play again(Y/N)? ");
        scanf(" %c", &temp);
    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.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    I recognized two mistakes very quickly. About your median function, you calculate (x + y) / 2. This expression yields an integer which is then converted to a double for the return value. If you want it to yield a floating point number you must divide by at least one floating point number. About your go again function, let me explain why that doesn't work.

    Code:
     "hello" = {'h','e','l','l','o','\0'} type: char[6]
    That is what a string is, an array of char. %s takes a pointer to the first element of an array of char and tries to fill it. It also has to fill in the '\0' part, which it doesn't have room to do in your temp character.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    I'm not sure why you chose to duplicate your thread in the C forum, but a mod will take care of that.

    Upon closer inspection of the code, I noticed that the tally array has some problems. The size of the array isn't constant, for whatever reason, so it is a variable length array. Hopefully you are using C99, where it is a standard feature.

    See here:
    Code:
    tally[nums[i]]++;
    Make sure all of tally's elements start from 0. C doesn't initialize them for you.

    I typically do a better implementation where the assumption that there is one mode isn't being made, but I guess that isn't necessary here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating the Median
    By openwindow in forum C Programming
    Replies: 1
    Last Post: 11-20-2011, 07:15 AM
  2. Mean, Median, and Mode .....code
    By AdamLAN in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2004, 02:29 PM
  3. Finding Mode Median and Mean
    By Ginny Morgan in forum C Programming
    Replies: 3
    Last Post: 05-08-2003, 03:09 PM
  4. median mode
    By bobbydigital in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2003, 08:53 PM
  5. Computing Mean, Median and Mode Using Arrays
    By Rodneo in forum C++ Programming
    Replies: 0
    Last Post: 05-29-2002, 11:40 PM

Tags for this Thread