-
-
Save binarybucks/1688388 to your computer and use it in GitHub Desktop.
Mittelwert beliebig vieler Zahlen (Returning 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:*/ | |
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