calculating the mode

This is a discussion on calculating the mode within the C Programming forums, part of the General Programming Boards category; Hi there, So I have a 10 element array of int's and I need to calculate the mode. I haven't ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    4

    calculating the mode

    Hi there,

    So I have a 10 element array of int's and I need to calculate the mode.

    I haven't written any code (for the calculation part) because I have no idea where to start. I was just hoping someone who has done this before or thinks they have a good idea could point me in the right direction, I would appreciate it.

    Is there a function to do this for me in math.h?

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    The mode is just the average, should be pretty easy. I'd suggest trying it out first and then come here with a specific problem if you get stuck.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    4
    Hey, thanks i appreciate any help i can get..

    I'm looking for:

    "the value that appears most frequently in the values input by the user."

    Is there a sorting function that checks for repeat input?

  4. #4
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Oh crap, it is to, sorry about that. Still, you could at least write a loop or something, just to show us that you're trying. We like to help, but not without some effort from you first.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You could sort the data, then loop through it looking for the longest string of repeat values.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Code:
    for (int i=0;i<10;i++)
    {
      //Do some stuff here
    }

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    4
    ok, ok sorry guys don't hate me i was thinking of something like this (keep in mind I already ordered the entries from smallest to largest):

    Code:
    void compute_mode(char test_scores[],int *n_scores)
    {
    int i, j,k;
    char mode[*n_scores];
    int n_modes = 1;
    
    for(i=o;i<*n_scores;i++)
    {
      for(j=i+1;j<*n_scores;j++) 
      {
        if( test_scores[i] < test_scores[j] )
          continue;
        else 
          {
           mode[n_modes] = test_scores[i];
           n_modes++;
           }
        }
    }
    
    for(k = 0; k <n_modes; k++)
       {
       printf("Mode %s", mode);
       }
    }
    Last edited by bigggame; 06-13-2006 at 01:28 AM.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    printf("Mode %s", mode[]);
    That won't work.

    test_score isn't the same variable as test_scores.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    > i was thinking of something like this
    Close enough - now write something which actually compiles.

    > char mode[*n_scores];
    C doesn't (yet) support variable length arrays.

    > for(i=o;i<*n_scores;i++)
    I guess that o was meant to be 0 (zero)
    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.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salem
    > char mode[*n_scores];
    C doesn't (yet) support variable length arrays.
    C99 does. Your compiler on the other hand...


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You can make it simpler. A whole lot simpler, in fact:

    Let's say you want to find the mode of 0,0,1,1,1,2,4,5,5,5,5,6,7,7,8,10. Let's call a row of consecutive numbers (like 3,3,3) a "run". You can proceed thus:

    We start with 0, because it is the first element. We find that the run of 0s is of length 2. This is the longest run so far.

    We proceed to the next element, 1, which has a run of three elements. This is longer than the zero run, so we store this as the longest run instead. The length of the 0s run is forgotten, as it is not needed.

    ... ...

    In the end, 5 is found to be the mode, as it has a run of 5. The trick is that finding the mode only requires that you keep track of the longest run SO FAR, while ignoring those runs shorter than the longest run found so far. After you've checked all the runs, the longest run so far is guaranteed to be your mode, because you've found the longest run in the whole array.

    Tip: you will only need a loop counter, two variables to keep track of the longest run found, and two more to keep track of the current run. You will only need to run through the whole array once.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

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, 02:42 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. Calculating mode
    By Lord CyKill in forum C Programming
    Replies: 1
    Last Post: 09-25-2003, 03:41 AM
  4. Showing the directory sturcture
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-26-2002, 04:46 PM
  5. Implementing "ls -al"
    By pdstatha in forum Linux Programming
    Replies: 11
    Last Post: 03-20-2002, 04:39 AM

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