Thread: Mode (Statistics)

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    11

    Mode (Statistics)

    i have created a menu driven statistics program in which it calculates no. of items, mean, median, mode, variance, standard deviation.
    I have written function for all but except mode. I am not getting idea how to add function for mode which will calculate the mode.
    Take a look in my current code:

    Code:
    #include <stdio.h>
    #include <math.h>
    #define SIZE 25
    float std_dev(float a[], int n);
    float mean(float a[], int n);
    float variance(float a[], int n);
    int median(float a[], int n);
    float mode(float a[], int n);
    
    int main(void)
    {
      int ch;
      float value[SIZE];
      int i;
      printf("\t\t\t\t****MAIN MENU****");
      printf("\n\t\t\t\t-----------------");
      printf("\n\t\t\t\t1. Calculate Number of Items");
      printf("\n\t\t\t\t2. Calculate Median");
      printf("\n\t\t\t\t3. Calculate Mode");
      printf("\n\t\t\t\t4. Calculate Mean");
      printf("\n\t\t\t\t5. Calculate Variance");
      printf("\n\t\t\t\t6. Calculate Standard Deviation\n");
      printf("\n\t\t\t\t7. Calculate ALL\n");
      printf("\n\t\t\t\t-----------------");
      printf("\n\n\t\t\tEnter your choice : ");
      scanf("%d", &ch);
      switch(ch)
        {
          case 1:
            printf("Enter %d float values\n", SIZE);
            for(i=0; i<SIZE; i++)
              scanf("%f", &value[i]);
            printf("Number of items : %d\n", i);
            break;
          
          case 2:
            printf("Enter %d float values\n", SIZE);
            for(i=0; i<SIZE; i++)
              scanf("%f", &value[i]);
            printf("\nMedian : %d\n",median(value,SIZE));
            break;
          
          case 3:
            printf("Enter %d float values\n", SIZE);
            for(i=0; i<SIZE; i++)
              scanf("%f", &value[i]);
            printf("\nMode : ");
            break;
            
          case 4:
            printf("Enter %d float values\n", SIZE);
            for(i=0; i<SIZE; i++)
              scanf("%f", &value[i]);
            printf("\nMean : %.3f\n",mean(value,SIZE));
            break;
          
          case 5:
            printf("Enter %d float values\n", SIZE);
            for(i=0; i<SIZE; i++)
              scanf("%f", &value[i]);
            printf("\nVariance : %.3f\n",variance(value,SIZE));
            break;
          
          case 6:
            printf("Enter %d float values\n", SIZE);
            for(i=0; i<SIZE; i++)
              scanf("%f", &value[i]);
            printf("\nStandard Deviation : %.3f\n",std_dev(value,SIZE));
            break;
          
          case 7:
            printf("Enter %d float values\n", SIZE);
            for(i=0; i<SIZE; i++)
              scanf("%f", &value[i]);
            printf("\n\nNumber of items : %d\nMedian : %d\nMode : \nMean : %.3f\nVariance : %.3f\nStandard Deviation : %.3f\n",i,median(value,SIZE),mean(value,SIZE),variance(value,SIZE),std_dev(value,SIZE));
          
          default:
            break;
        }
    }
    
    int median(float a[], int n)
    {
      int i,j;
      float t,median;
      
      for(i=1; i<=n-1; i++)
      {
        for(j=1; j<n-i; j++)
        {
          if (a[j] <= a[j+1])
          {
            t=a[j];
            a[j]=a[j+1];
            a[j+1]=t;
          }
          else
          continue;
        }
      }
      for(i=1; i<=n; i++)
      printf("\n%f ",a[i]);
      if(n%2 == 0)
        median = (a[n/2] + a[n/2+1])/2.0;
      else
        median = a[n/2 + 1];
      
      return(median);
    }
    
    float mean(float a[], int n)
    {
      int i;
      float sum = 0.0, mean;
      for(i=0; i<n; i++)
        sum = sum + a[i];
      mean = sum/((float)n);
      
      return(mean);
    }
    
    float variance(float a[], int n)
    {
      int i;
      float sum = 0.0, mean, variance, sumsqr, deviation;
      for(i=0; i<n; i++)
        sum = sum + a[i];
      mean = sum/((float)n);
      for(i=1; i<=n; i++)
      {
        deviation = a[i] - mean;
        sumsqr += deviation*deviation;
      }
      variance = sumsqr/(float)n;
      
      return(variance);    
    }
    
    float std_dev(float a[], int n)
    {
      int i;
      float sum = 0.0, mean, variance, sumsqr, deviation, std_deviation;
      for(i=0; i<n; i++)
        sum = sum + a[i];
      mean = sum/((float)n);
      for(i=1; i<=n; i++)
      {
        deviation = a[i] - mean;
        sumsqr += deviation*deviation;
      }
      variance = sumsqr/(float)n;
      std_deviation = sqrt(variance);
      
      return(std_deviation);
    }
    Last edited by getout; 01-03-2007 at 04:31 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    As I remember - you should just sort the array and take the item that's index is SIZE/2
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    11
    No mode is the element which frequently occurs, for eg: 25 values input are 0,0,1,2,3,4,5,6,6,6,6,6,7,8,9,0,6,5,4,3,2,1,7,8,9

    so the mode will be 6

    Now how would be a function for this??

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Use the qsort() function in stdlib.h
    There are many examples of it being used on the board.
    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.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. sort array
    2. scan array
    3. for each new value found - count the number of occurencies (tiil the new value found)
    4. if the count is bigger than the mod_count - replace the mode_value and mode_count
    5. continue till the end of the array
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I don't believe that sorting is a necessary step. At least, I've done it before in math class without putting a series in order.

    But beyond that, vart has the right idea.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by citizen
    I don't believe that sorting is a necessary step. At least, I've done it before in math class without putting a series in order.

    But beyond that, vart has the right idea.
    without sorting you need to store several pairs (value, count) during scanning the array.
    Working with sorted array - you need only to store two pairs (mode_value, mode_count) and (curr_value, curr_count).

    both approaches are possible, depending on the preferencies that are out the bounds of the algorithm itself
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Quote Originally Posted by citizen
    I don't believe that sorting is a necessary step. At least, I've done it before in math class without putting a series in order.

    But beyond that, vart has the right idea.
    One way or another, you end up sorting the array, or performing an equivalent amount of work.

    If you just count the elements 'without sorting', then you have, for all intents and purposes, performed what is called a Count Sort.
    Callou collei we'll code the way
    Of prime numbers and pings!

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    11
    thx for the help, i have created the function for mode & it is working properly but when same amount of two numbers is entered it give only one value
    for eg. if 8,8,8 & 7,7,7 is entered in input it only gives mode as 8 not 8 & 7 both
    so requesting a little bit of editing which should be done on the function

    @Salem, I am quite new to C so cant work out qsort() function,...I have searched the board but cant get satisfactory answer. so can u plz give me example regarding this question.

    here is my mode function

    Code:
    float mode(float a[], int n)
    {
      int i,j,count=0,num=0,max=0;
      float t,mode;
    
      for(i=1; i<=n-1; i++)
      {
        for(j=1; j<n-i; j++)
        {
          if (a[j] >= a[j+1])
          {
            t=a[j];
            a[j]=a[j+1];
            a[j+1]=t;
          }
          else
          continue;
        }
      }
      for(i=1; i<n-1; i++)
      {
        if(num!=a[i])
          {
          num=a[i];
          count=1;
          }
          else
          count++;
        if(count>max)
        {
          max=count;
          mode=num;
        }
      }
      return(mode);
    }

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Like http://cboard.cprogramming.com/showt...ighlight=qsort
    just change the int* to float*
    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.

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    for eg. if 8,8,8 & 7,7,7 is entered in input it only gives mode as 8 not 8 & 7 both
    so requesting a little bit of editing which should be done on the function
    1. Calculate the count of all occurrences of number and store them in an array.
    2. Get the max value of that array
    3. Check is there any array element which is equal to max
    4. If so print all those corresponding values which reflects the count

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. console mode and service mode
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 06-01-2008, 01:42 AM
  2. Shortening main
    By pdstatha in forum C Programming
    Replies: 1
    Last Post: 04-03-2002, 04:56 PM
  3. Implementing "ls -al"
    By pdstatha in forum Linux Programming
    Replies: 11
    Last Post: 03-20-2002, 04:39 AM
  4. free() usage
    By pdstatha in forum C Programming
    Replies: 5
    Last Post: 03-13-2002, 09:28 AM
  5. Implementing "ls -al"
    By pdstatha in forum C Programming
    Replies: 7
    Last Post: 03-06-2002, 05:36 PM