Thread: Finding the Mode

  1. #16
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    Well how i would just keep a count of the data points?

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Every time you see a number, you add one in the appropriate place.

  3. #18
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    So have it only store integers into an array if it has entered before? And then after they stop the loop(enter -number), have it check the array somehow for duplicates then determine which one has the most?

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    So you're going to start with a big pile of numbers, all of which are 0. When a number, call it number, is read in, you're going to add one to that particular number in your big pile: frequency[number]++. At the end of the day, whichever number(s) have frequency[number] the largest, win.

  5. #20
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    struct node
    {
      int value;
      int frequency;
      struct node *next;
    };
    
    struct node *insert(struct node *p, int value)
    {
      struct node *tmp = p, *head = p;
    
      if(!p)
      {
        p = malloc(sizeof(*p));
    
        if(p)
        {
          p->value = value;
          p->frequency = 1;
          p->next = 0;
        }
    
        return p;
      }
    
      /* From here on out, p represents the previous node in the tree */
      for(p = 0; tmp; tmp = tmp->next)
      {
        if(tmp->value < value)
        {
          tmp->next = p;
    
          p = malloc(sizeof(*p));
          if(p)
          {
            p->value = value;
            p->frequency = 1;
            p->next = tmp;
            return head;
          }
        }
        p = tmp;
      }
    
      return 0;
    }
    You can just use that function to build a list of arbitrary numbers.

  6. #21
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    i appreciate your help but i dont get what that code does, i cant compile it either.

  7. #22
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well its not stand-alone. It is a linked list.

    Example:
    Code:
    #include <FPT.h>
    
    struct node
    {
      double value;
      int frequency;
      struct node *next;
    };
    
    struct node *insert(struct node *p, double value)
    {
      struct node *tmp = p, *head = p;
    
      if(!p)
      {
        p = malloc(sizeof(*p));
    
        if(p)
        {
          p->value = value;
          p->frequency = 1;
          p->next = 0;
        }
    
        return p;
      }
    
      /* From here on out, p represents the previous node in the tree */
      for(p = 0; tmp; tmp = tmp->next)
      {
        if(tmp->value == value)
        {
          ++tmp->frequency;
          return head;
        } else if(tmp->value < value)
        {
          tmp->next = p;
    
          p = malloc(sizeof(*p));
          if(p)
          {
            p->value = value;
            p->frequency = 1;
            p->next = tmp;
            return head;
          }
        }
        p = tmp;
      }
    
      return 0;
    }
    
    void clean(struct node *p)
    {
      if(p)
      {
        clean(p->next);
        free(p);
      }
    }
    
    void outN(struct node *p)
    {
      if(p)
      {
        printf("&#37;g\t%d", p->value, p->frequency);
        outN(p->next);
      }
    }
    
    int main()
    {
      double i;
      struct node *p = 0;
    
      outS("Please input a postive integer...");
    
      while((i=inD())>=0){
        p = insert(p, i);
      }
    
      outN(p);
      clean(p);
    }
    Last edited by master5001; 10-06-2008 at 07:49 PM.

  8. #23
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Simple arrays would do. Unless the assignment asked for linked lists.

  9. #24
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    *mopes* I just figured since he was using floating points it would make more sense to do it with linked lists. But yeah, I suppose you could use arrays... if this is some sort of contest to use the least amount of lines.

  10. #25
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    This ain't COBOL (where you're paid by the line. LOL).

  11. #26
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    haha I will keep that in mind next time. Since I have come to know Lucid's programming level reasonably well, I think its safe to say he would never ethically use code he doesn't comprehend and this is above where he is at in his learning.

  12. #27
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    So, although i wont be turning in the code listed, i had to try it out. Seems to work good except for when there are two values that appear the same number of times.

  13. #28
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    Is there anyway to tweak this program to accept two "modes" im gonna try to learn from this program and write my own.

  14. #29
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Did you get my code before or after I corrected a bug in it? It should be working ok now.

  15. #30
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    The most recent one, if i enter 3,3,2,2 it shows 2...2. When it should show 3,2. Meaning the mode is 3 and 2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding mode!
    By afiq8289 in forum C Programming
    Replies: 1
    Last Post: 11-27-2008, 10:51 AM
  2. Finding mode in array of numbers
    By Snowcat in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2006, 09:58 PM
  3. finding mode of an array
    By n00by in forum C Programming
    Replies: 1
    Last Post: 05-18-2004, 07:40 PM
  4. Finding the mode of an array
    By Shnakepup in forum C++ Programming
    Replies: 16
    Last Post: 05-16-2003, 10:01 AM
  5. Finding Mode Median and Mean
    By Ginny Morgan in forum C Programming
    Replies: 3
    Last Post: 05-08-2003, 03:09 PM