Skip to content

Instantly share code, notes, and snippets.

@lumaxis
Created January 26, 2012 17:59
Show Gist options
  • Save lumaxis/1684050 to your computer and use it in GitHub Desktop.
Save lumaxis/1684050 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){
int i;
/* 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(i=0; i < *size; i++){
scanf("%lf", *numbers + i * sizeof(double));
}
}
int main(int argc, char const *argv[]){
double *numbers = NULL;
double sum=0, average=0;
int i, size;
inputNumbers(&size, &numbers);
/*Mittelwert berechnen*/
for (i = 0; i < size; ++i){
sum = sum + *(numbers + i * sizeof(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