/***************************************************************************
 *   Copyright (C) 2005 by Christian Bucher                                *
 *   christian.bucher@amadyne.net                                          *
 *                                                                         *
 *   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>

// WICHTIG wegen pow() und sqrt()
#include <math.h>

// wirkt einfach kewler...
#define ARRAY_SIZE   100
#define RANDOM_LIMIT 1337

// prototypen
void   arrayShow(int *pnArray);
int    arrayMin(int *pnArray);
int    arrayMax(int *pnArray);
int    arraySum(int *pnArray);
int    arrayMean(int *pnArray);
int    arrayWidth(int *pnArray);
double arrayStdDev(int *pnArray);

// =========== main() here! =================

int main(int argc, char *argv[])
{
int anArray[ARRAY_SIZE];
int i;

  // "Zufallswerte" ins Array bimsen
  for (i = 0; i < ARRAY_SIZE; i++)
    {
    anArray[i] = rand() % RANDOM_LIMIT;
    }
  
  arrayShow(anArray);
  
  printf("\nAuswertung:\n");
  
  printf("Minimum: %d, Maximum: %d\n", arrayMin(anArray), arrayMax(anArray));
  printf("Summe: %d, Mittelwert: %d\n", arraySum(anArray), arrayMean(anArray));
  printf("Streubreite: %d, Standardabweichung: %.1f\n\n", arrayWidth(anArray), arrayStdDev(anArray));

  return EXIT_SUCCESS;
}

// ***************** Funktionen **********************
// Anzeige
void arrayShow(int *pnArray)
{
int i;
  
  for (i = 0; i < ARRAY_SIZE; i++)
    {
    printf ("%4d", pnArray[i]);
    
    // nach 10 Werten eine neue Zeile...
    if (9 == (i%10))
      putchar('\n');
    else
      putchar('\t');
    
    }
}

// ***************************************
// Minimum
int  arrayMin(int *pnArray)
{
int i, nMin;
  
  for (i = 0, nMin = pnArray[0]; i < ARRAY_SIZE; i++)
    {
    if (pnArray[i] < nMin)
      nMin = pnArray[i];
    }
  return nMin;
}

// ***************************************
// Maximum
int  arrayMax(int *pnArray)
{
int i, nMax;
  
  for (i = 0, nMax = pnArray[0]; i < ARRAY_SIZE; i++)
    {
    if (pnArray[i] > nMax)
      nMax = pnArray[i];
    }
  return nMax;
}

// ***************************************
// Summe
int  arraySum(int *pnArray)
{
int i, nSum;
  
  for (i = 0, nSum = 0; i < ARRAY_SIZE; i++)
    {
    nSum += pnArray[i];
    }
  return nSum;
}

// ***************************************
// Mittelwert (ganzzahlig, gerundet)
int  arrayMean(int *pnArray)
{
  return (arraySum(pnArray) + (ARRAY_SIZE / 2) ) / ARRAY_SIZE;
}

// ***************************************
// Streubreite (BILLIG)
int  arrayWidth(int *pnArray)
{
  return arrayMax(pnArray) - arrayMin(pnArray);
}

// ***************************************
// Standardabweichung lt. Wikipedia (weniger billig)
double arrayStdDev(int *pnArray)
{
int    i;
int    nMean;
double dftTmp;
  
  // WICHTIG: Den Mittelwert nur 1x AUSSERHALB der Schleife 
  // berechnen, das ist EXORBITANT schneller!
  nMean = arrayMean(pnArray); 

  // Die Summe da drin wird recht gross. Besser gleich in double rechnen
  for (i = 0, dftTmp = 0.0; i < ARRAY_SIZE; i++)
    {
    // die Differenz ist noch schnell in int zu rechnen,
    // dann erst nach double casten
    dftTmp += pow((double)(pnArray[i] - nMean), 2);
    }
  
  // Den letzten Berechnungsschritt mit dem return-Operator verbinden (1337)
  // Der "(double)ARRAY_SIZE" Typecast stellt sicher, dass wirklich in double
  // gerechnet wird.        
  return sqrt(dftTmp / (double)ARRAY_SIZE);
}

syntax highlighted by Code2HTML, v. 0.9.1