Modalità Programma In C
Nella matematica statistica, una modalità è un valore che si verifica per il maggior numero di volte.
For Example - assumere un insieme di valori 3, 5, 2, 7, 3. La modalità di questo valore impostato è 3 in quanto appare più di qualsiasi altro numero.
Algoritmo
Possiamo derivare un algoritmo per trovare la modalità, come indicato di seguito:
START
   Step 1 → Take an integer set A of n values
   Step 2 → Count the occurence of each integer value in A
   Step 3 → Display the value with highest occurence
STOPPseudocodice
Ora possiamo derivare lo pseudocodice usando l'algoritmo di cui sopra, come segue:
procedure mode()
   
   Array A
   FOR EACH value i in A DO
      Set Count to 0
      FOR j FROM 0 to i DO
         IF A[i] = A[j]
            Increment Count
         END IF
      END FOR
      
      IF Count > MaxCount
         MaxCount =  Count
         Value    =  A[i]
      END IF
   END FOR
   DISPLAY Value as Mode
   
end procedureImplementazione
L'implementazione di questo algoritmo è fornita di seguito:
#include <stdio.h>
int mode(int a[],int n) {
   int maxValue = 0, maxCount = 0, i, j;
   for (i = 0; i < n; ++i) {
      int count = 0;
      
      for (j = 0; j < n; ++j) {
         if (a[j] == a[i])
         ++count;
      }
      
      if (count > maxCount) {
         maxCount = count;
         maxValue = a[i];
      }
   }
   return maxValue;
}
int main() {
   int n = 5;
   int a[] = {0,6,7,2,7};
   printf("Mode = %d ", mode(a,n));
   return 0;
}Produzione
Il risultato del programma dovrebbe essere:
Mode = 7