#include <stdlib.h>
#include <stdio.h>

const int MAX_LEN= 256;  //  Maximum length of input strings

int main(int argc, char*argv[])
{
	int no_items;	// Number of data points we will input
	double *data;	// Data to be input
	char inp_str[MAX_LEN];  // String for user to type into
	double total= 0;
	int i;

	// Get from user the number of data items
	printf("How many data items are there?\n");
	fgets(inp_str,MAX_LEN,stdin);
	no_items= atoi(inp_str);
	
	if (no_items <= 0) {
		printf ("Incorrect input: %d\n",no_items);
		return -1;
	}

	// Allocate memory for data
	data= (double *)malloc(no_items * sizeof(double));
        if (data == NULL) {
		fprintf (stderr,"Out of memory\n");
		return -1;
        }
	// Loop for all data and get input
	for (i= 0; i < no_items; i++) {
		printf ("Input data item %d.\n",i);
		fgets(inp_str, MAX_LEN, stdin);
		data[i]= atof(inp_str);
	}

	// Calculate total
	for (i= 0; i < no_items; i++) {
		total+= data[i];
	}
	printf ("Sum = %f mean = %f.",total, total/no_items);
	free(data);
        return 0;
}

