Skip to content

Instantly share code, notes, and snippets.

@hdznrrd
Forked from lumaxis/A9.c
Created January 27, 2012 12:54
Show Gist options
  • Save hdznrrd/1688645 to your computer and use it in GitHub Desktop.
Save hdznrrd/1688645 to your computer and use it in GitHub Desktop.
Mittelwert beliebig vieler Zahlen
#include "stdio.h"
#include "stdlib.h"
void inputNumbers(int *size, double **numbers)
{
// Abfrage nach Anzahl der Zahlen
printf("Anzahl der zu speichernden Zahlen:\n");
scanf("%d", size);
// Entsprechende Menge Speicher reservieren
*numbers = (double*)malloc(*size * sizeof(double));
// Zahlen einlesen
printf("Geben sie %d Zahlen ein:\n", *size);
for(int i=0; i < *size; i++)
{
scanf("%lf", *numbers + i * sizeof(double));
}
}
int main(int argc, char const *argv[])
{
double *numbers = NULL;
int size = 0;
inputNumbers(&size, &numbers);
// Mittelwert berechnen
double sum=0;
for (int i = 0; i < size; ++i)
{
sum = sum + *(numbers + i * sizeof(double));
}
double average = sum / size;
printf("%lf\n", average);
free(numbers);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment