/*
 ============================================================================
 Name        : C_JavaIntro.c
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

/*
 * Create a C program which reads positive real numbers from the keyboard
 * and prints out their average and the values in reverse order in which
 * they were entered. The user should enter a non-positive number to indicate
 *  the end of the input, but do not include it with the other data.
*/

#include <stdio.h>
#include <stdlib.h>

const int NUM_VALUES = 100;

double getNumber(){
	double result = 0;
	printf("Enter a number: ");
	//use format lf for long float when using doubles
	while(scanf("%lf", &result) == 0){
		//consume the bad line
		char trash[100];
		scanf("%100s", trash);
		printf("Bad input. Try again: ");
	}
	return result;
}

int main(void) {
	double values[NUM_VALUES];
	double n = 0;
	double sum = 0;
	int total = 0;

	//read the data
	while(total < NUM_VALUES && ((n = getNumber()) > 0)){
		values[total++] = n;
		sum += n;
	}

	//printf can use either %f or %lf for doubles
	printf("Average = %.2lf\n", (sum/total));

	for(int i = total-1; i >= 0; i--){
		printf("%2d: %4.2lf\n", i, values[i]);
	}

	return EXIT_SUCCESS;
}
