Ex7.1, pg 330 of Beginning C, Ivor Horton, 3- . , . , , . , . Code:: Blocks Ubuntu 11.04. , .
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
int main(int argc, char ** argv[])
{
float input = 0;
int count=0, n = 0;
float *numbers = NULL;
float *more_numbers;
float sum = 0.0;
while (TRUE)
{
do
{
printf("Enter an floating point value (0 to end): ");
scanf("%f", &input);
count++;
more_numbers = (float*) realloc(numbers, count * sizeof(float));
if ( more_numbers != NULL )
{
numbers = more_numbers;
numbers[count - 1] = input;
}
else
{
free(numbers);
puts("Error (re)allocating memory");
exit(TRUE);
}
} while ( input != 0 );
printf("Numbers entered: ");
while( n < count )
{
printf("%f ", numbers[n]);
n++;
}
n = 0;
while( n < count )
{
sum += numbers[n];
n++;
}
printf("\n Average of floats = %f \n", sum / (count - 1));
}
return 0;
}