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
MediaN - median.c

median.c

Caricato da:
Scarica il programma completo

  1. /***************************************************************************
  2.  *   Copyright (C) 2007 by Lorenzo La Porta   *
  3.  *   lorelapo@gmail.com   *
  4.  *                                                                         *
  5.  *   This program is free software; you can redistribute it and/or modify  *
  6.  *   it under the terms of the GNU General Public License as published by  *
  7.  *   the Free Software Foundation; either version 2 of the License, or     *
  8.  *   (at your option) any later version.                                   *
  9.  *                                                                         *
  10.  *   This program is distributed in the hope that it will be useful,       *
  11.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  12.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  13.  *   GNU General Public License for more details.                          *
  14.  *                                                                         *
  15.  *   You should have received a copy of the GNU General Public License     *
  16.  *   along with this program; if not, write to the                         *
  17.  *   Free Software Foundation, Inc.,                                       *
  18.  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  19.  ***************************************************************************/
  20.  
  21.  
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. int iLength,iDegree;
  29. float mediaN(float *);
  30. inline float radn(float x, int n)
  31. {
  32.         if(n==1)return x;
  33.         if(n==0)
  34.                 if(x!=0)
  35.                         return 0;
  36.                 else
  37.                         return 0;
  38.         if(n<0)return radn(1/x,-n);
  39.         int i,j;float rad=x/2,mrad;
  40.         for(i=0;i<128;i++)
  41.         {
  42.                 mrad=x;
  43.                 for(j=0;j<n-1;j++)
  44.                         mrad/=rad;
  45.                 rad=(rad+mrad)/2;
  46.         }    
  47.         return rad;
  48. }
  49. inline float pow(float x, int n)
  50. {
  51.         if(n<0) return pow(1/x,-n);
  52.         float fPowed=1;
  53.         int i;
  54.         for(i=0;i<n;i++)
  55.                 fPowed*=x;
  56.         return fPowed;
  57. }
  58. float mediaN(float *fV)
  59. {
  60.         float fSum;
  61.         int i;
  62.         for(i=0;i<iLength;i++)
  63.                 fSum+=pow(fV[i],iDegree);
  64.         fSum/=iLength;
  65.         return radn(fSum,iDegree);
  66. }
  67. int main(int argc, char *argv[])
  68. {
  69.         int i;
  70.         printf("Questo programma calcola la media n di un vettore float\n\n");
  71.         printf("Inserisci la lunghezza del vettore : ");
  72.         scanf("%d",&iLength);
  73.         float *fVector=malloc(sizeof(float)*iLength);
  74.         for(i=0;i<iLength;i++)
  75.         {
  76.                 printf("Inserire elemento %d : ",i+1);
  77.                 scanf("%f",&fVector[i]);
  78.         }
  79.         printf("Inserire il grado della media : ");
  80.         scanf("%d",&iDegree);
  81.         printf("\n\nLa media %d e' uguale a %f\n",iDegree,mediaN(fVector));
  82.         return EXIT_SUCCESS;
  83. }