-
-
Save hdznrrd/1688645 to your computer and use it in GitHub Desktop.
Mittelwert beliebig vieler Zahlen
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" | |
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