-
-
Save binarybucks/1688414 to your computer and use it in GitHub Desktop.
Mittelwert beliebig vieler Zahlen (double Ptr)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdio.h" | |
#include "stdlib.h" | |
/*Beliebige Anzahl an Zahlen einlesen:*/ | |
void inputNumbers(int *size, double **doublePtr){ | |
int i; | |
/* Abfrage nach Anzahl der Zahlen */ | |
printf("Anzahl der zu speichernden Zahlen:\n"); | |
scanf("%d", size); | |
/* Entsprechende Menge Speicher reservieren */ | |
*doublePtr = (double*)malloc(*size * sizeof(double)); | |
/* Zahlen einlesen */ | |
printf("Geben sie %d Zahlen ein:\n", *size); | |
for(i=0; i < *size; i++){ | |
scanf("%lf", *doublePtr + i * sizeof(double)); | |
} | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
double *numbers = NULL; | |
double **doublePtr = &numbers; | |
int size; | |
double sum=0, average=0; | |
int i; | |
inputNumbers(&size, doublePtr); | |
/*Mittelwert berechnen*/ | |
for (i = 0; i < size; ++i){ | |
sum = sum + *(numbers + i * sizeof(double)); | |
} | |
average = sum / size; | |
printf("%lf\n", average); | |
free(numbers); | |
free(doublePtr); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment