Thread: Mean Median Mode Problem

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    23

    Mean Median Mode Problem

    It has a compiling error and highloights the retunr (0); saying theres a syntax error before it. I cannot find the problem anyone care to help or improve my program.
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <time.h>
    #include <stdlib.h>
    #define ARRAY_SIZE 50
    
    void fillArray(int total, int nums[]);
    void sortArray(int nums[], int total);
    void printResults(double mean, double median, double mode);
    int findMode(int nums[], int total);
    int readTotalNums();
    double findMean(int nums[], int total);
    double findMedian(int nums[], int total);
    
    int 
    main(void)  
    {
        int nums[50];
        int total;
        double mean, median, mode;
        do{
            total = readTotalNums(); //guarantee 1-100
            fillArray(total, nums); //read in the #s don't need to check range
            sortArray(nums, total);
            mean = findMean(nums, total);
            median = findMedian(nums, total);
            mode = findMode(nums, total);
            printResults(mean, median, mode);
           }
        return (0);
    }
    
    
    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;
            }
        }
     }
    }
    
    void printResults(double mean, double median, double mode) 
    {
        printf("Mean: %lf\tMedian: %lf\tMode: %i", mean, median, mode);
    }
    
    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;
    }
    
    int readTotalNums() {
        int num;
        do {
            printf("How many numbers? ");
            scanf("%i", &num);
        } while (num < 1 || num > 101);
        return num;
    }
    
    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) {
                return (nums[total/2]+nums[total/2-1])/2;
            }else{
                return nums[total/2];
            }
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You are forgetting while(condition); at line 29.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Median and Mode of an Array
    By skmightymouse in forum C Programming
    Replies: 3
    Last Post: 04-29-2012, 12:36 PM
  2. mean, mode median calculation..
    By naspek in forum C Programming
    Replies: 2
    Last Post: 09-10-2009, 09:15 AM
  3. Mean, Median, and Mode .....code
    By AdamLAN in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2004, 02:29 PM
  4. Finding Mode Median and Mean
    By Ginny Morgan in forum C Programming
    Replies: 3
    Last Post: 05-08-2003, 03:09 PM
  5. median mode
    By bobbydigital in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2003, 08:53 PM