Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Numero di maggioranza - Maggioranza.c

Maggioranza.c

Caricato da: Piero Tofy
Scarica il programma completo

  1. #include <stdio.h> /* Input/Output Header */
  2. #include <stdlib.h> /* Per la funzione malloc */
  3.  
  4. /* Maggioranza.c
  5. Written by Piero Tofy 2005
  6. http://www.pierotofy.it */
  7.  
  8.  
  9. /* About Messages */
  10. void ShowAbout(void){
  11.         printf("Calcolo dell'elemento di maggioranza by Piero Tofy 2005 http://www.pierotofy.it\n\n");
  12. }
  13.  
  14. void GetElementCount(int *pInt){
  15.         do{
  16.                 printf("Digita il numero di elementi da inserire (min. 3):");
  17.         }while(scanf("%d",pInt) != 1 || *pInt < 3);
  18. }
  19.  
  20. int GetMajorValue(int iElementCount){
  21.         /* Se il numero è pari, il valore di maggioranza è pari alla metà,
  22.         altrimenti è pari alla metà più uno */
  23.  
  24.         if ((iElementCount % 2) == 0) return iElementCount/2;
  25.         else return iElementCount/2+1;
  26. }
  27.  
  28. void GetValues(double pElementArray[], int iElementCount){
  29.         unsigned short int c;
  30.  
  31.         for (c = 0; c<iElementCount; c++){
  32.                 do{
  33.                         printf("Inserisci l'elemento n.%d: ",c+1);
  34.                 }while(scanf("%lf",&pElementArray[c]) != 1);
  35.         }
  36. }
  37.  
  38. int IsTheMajorValue(double dNumber, double pElementArray[], int iElementCount, int iMajorValue){
  39.         unsigned short int c;
  40.         int iElementMatchCount = 0;
  41.  
  42.         for (c=0; c<iElementCount; c++)
  43.                 if (pElementArray[c] == dNumber) iElementMatchCount++;
  44.        
  45.         if (iElementMatchCount >= iMajorValue) return 1;
  46.         else return 0;
  47. }
  48.  
  49. void CheckForAMajorValue(double pElementArray[], int iElementCount, int iMajorValue){
  50.         unsigned short int c;
  51.         for (c=0; c<iElementCount; c++){
  52.                 if (IsTheMajorValue(pElementArray[c],pElementArray,iElementCount,iMajorValue)){
  53.                         printf("E' stato trovato il numero di maggioranza (%lf).\n",pElementArray[c]);
  54.                         return;
  55.                 }
  56.         }
  57.  
  58.         /* Se non è stato trovato il numero di maggioranza... */
  59.         printf("Non e' stato trovato un numero di maggioranza.\n");
  60. }
  61.  
  62.  
  63.  
  64. /* Entry Point */
  65. int main(void){
  66.         /* Dichiarazione delle variabili */
  67.         double *pElementArray = 0;
  68.         int iElementCount, iMajorValue;
  69.  
  70.         /* Visualizza l'about */
  71.         ShowAbout();
  72.        
  73.         /* Prende il numero di elementi */
  74.         GetElementCount(&iElementCount);
  75.  
  76.         /* Alloca la memoria necessaria per l'array */
  77.         pElementArray = (double *)malloc(sizeof(double)*iElementCount);
  78.  
  79.         /* Assengna il valore di maggioranza */
  80.         iMajorValue = GetMajorValue(iElementCount);
  81.  
  82.         /* Raccoglie gli elementi */
  83.         GetValues(pElementArray,iElementCount);
  84.        
  85.         /* Controlla l'esistenza di un numero di maggioranza */
  86.         CheckForAMajorValue(pElementArray,iElementCount,iMajorValue);
  87.  
  88.         /* Esce dal programma */
  89.         exit(EXIT_SUCCESS);
  90. }