/***************************************************************************
* Copyright (C) 2007 by Lorenzo La Porta *
* lorelapo@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
int iLength,iDegree;
float mediaN(float *);
inline float radn(float x, int n)
{
if(n==1)return x;
if(n==0)
if(x!=0)
return 0;
else
return 0;
if(n<0)return radn(1/x,-n);
int i,j;float rad=x/2,mrad;
for(i=0;i<128;i++)
{
mrad=x;
for(j=0;j<n-1;j++)
mrad/=rad;
rad=(rad+mrad)/2;
}
return rad;
}
inline float pow(float x, int n)
{
if(n<0) return pow(1/x,-n);
float fPowed=1;
int i;
for(i=0;i<n;i++)
fPowed*=x;
return fPowed;
}
float mediaN(float *fV)
{
float fSum;
int i;
for(i=0;i<iLength;i++)
fSum+=pow(fV[i],iDegree);
fSum/=iLength;
return radn(fSum,iDegree);
}
int main(int argc, char *argv[])
{
int i;
printf("Questo programma calcola la media n di un vettore float\n\n");
printf("Inserisci la lunghezza del vettore : ");
scanf("%d",&iLength);
float *fVector=malloc(sizeof(float)*iLength);
for(i=0;i<iLength;i++)
{
printf("Inserire elemento %d : ",i
+1
);
scanf("%f",&fVector[i]);
}
printf("Inserire il grado della media : ");
scanf("%d",&iDegree);
printf("\n\nLa media %d e' uguale a %f\n",iDegree
,mediaN
(fVector
));
return EXIT_SUCCESS;
}