need help to calculate mode of entries

This is a discussion on need help to calculate mode of entries within the C Programming forums, part of the General Programming Boards category; i need some help to figure out how to find the most frequent entry that was inputed Code: #include <stdio.h> ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    1

    need help to calculate mode of entries

    i need some help to figure out how to find the most frequent entry that was inputed
    Code:
    #include <stdio.h>
    #include <math.h>
    #define MAXARR 100
    void main (void)
    {
     int
    	mode[MAXARR], i, entry;
     int counter = 0;
     int total = 0;
     int total2 = 0;
     int mostfreq = 0;
     float average, stdev;
     printf("Please type your entry (-1 to quit): \n");
     scanf("%d",&entry);
     while (entry >= 0)
    	{
    	 total = total + entry;
    	 total2 = total2 + entry * entry;
    	 mode[entry]++;
    	 counter = counter +1;
    	 printf("Please type your entry (-1 to quit): \n");
    	 scanf("%d",&entry);
    	}
     average = (float)total / (float)counter;
     for(i=0;i<100;i++)
    	{
    	 if (mode[i] > mostfreq);
    		{
    		 mostfreq = mode[i];
    		}
    	}
     printf("Most common age is %d \n", i);
     printf("It occurs %d times \n", mostfreq);
     printf("There are %d ages recorded \n", counter);
     stdev = sqrt (total2 / counter - average * average);
     printf("The average age is : %6.2f \n", average);
     printf("The standard deviation from the average is %6.2f \n", stdev);
     getchar();
     getchar();
    }

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    One idea would be to have two arrays, one for the numbers and one for the frequency of each number. As each number is entered you increment its frequency counter, when you're finished you simply look for the largest number in your array of frequency's and there you have it.

    An advantage to doing it that way is that it gives you complete data so you could also display numbers in different age brackets or what ever.

    Also, main should always return an int, it should never be void. But WD on the code tags, so many people miss those on their first post.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Another way to figure the most frequently entered age would be to have a nested loop. Something like:
    Code:
    int ages[] = { 3, 3, 2, 7, 19, 4, 3, 8, 10, 7, 4, 5, 4, 17, 9, 4 };
    int num_ages = 16;
    int i, j, ctr, high_ctr = 0, high_index = 0;
    
    for(i = 0;i < 16;++i)
    {
      ctr = 0;
    
      for(j = i+1;j < 16;++j)
        if(ages[i] == ages[j])
          ctr++;
    
      if(ctr > high_ctr)
      {
        high_ctr = ctr;
        high_index = i;
      }
    }
    
    printf("The age entered most frequently was: %d\n", ages[high_index]);
    Something like that
    Last edited by itsme86; 10-22-2004 at 06:24 PM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 10:06 AM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 09:01 PM

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