Skip to content

Instantly share code, notes, and snippets.

@binarybucks
Forked from lumaxis/A9.c
Created January 27, 2012 11:34
Show Gist options
  • Save binarybucks/1688388 to your computer and use it in GitHub Desktop.
Save binarybucks/1688388 to your computer and use it in GitHub Desktop.
Mittelwert beliebig vieler Zahlen (Returning Ptr)
#include "stdio.h"
#include "stdlib.h"
/*Beliebige Anzahl an Zahlen einlesen:*/
double* inputNumbers(int *size){
int i;
/* Abfrage nach Anzahl der Zahlen */
printf("Anzahl der zu speichernden Zahlen:\n");
scanf("%d", size);
/* Entsprechende Menge Speicher reservieren */
double* ptr = (double*)malloc(*size * sizeof(double));
/* Zahlen einlesen */
printf("Geben sie %d Zahlen ein:\n", *size);
for(i=0; i < *size; i++){
scanf("%lf", ptr + i * sizeof(double));
}
return ptr;
}
int main(int argc, char const *argv[])
{
double *numbers = NULL;
int size;
double sum=0, average=0;
int i;
numbers = inputNumbers(&size);
/*Mittelwert berechnen*/
for (i = 0; i < size; ++i){
sum = sum + *(numbers + i * sizeof(double));
}
average = sum / size;
printf("Average: %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