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


void usage(void){
	printf("\tUsage: print-chars [input]\n");


	exit(1);
}



int main(int argc, char **argv){
	//FILE *output;
	FILE *input;
	//int num1, num2, num3;
	char linestring[100];
	double tmp1 = 0;
	double tmp2 = 0;
	double tmp3 = 0;
	//double tmp4 = 0;
	//double tmp5 = 0;

	if(argc != 2){
		usage();
	}

    if( ( input = fopen( argv[1], "r" ) ) == NULL ) {
		fprintf( stderr, "Error opening %s\n", argv[2] );
        exit( 1 );
    }

   while( fgets(linestring, sizeof(linestring), input) != NULL){
		tmp3++;
	}
	fclose(input);

	if( ( input = fopen( argv[1], "r" ) ) == NULL ) {
		fprintf( stderr, "Error opening %s\n", argv[2] );
		exit( 1 );
	}
	while( fgets(linestring, sizeof(linestring), input) != NULL ){
		//(void)memset(linestring
		//strcpy(outstr, linestring);
		//outstr[strlen(outstr)-1]='\0';
		//num3++;
		//39926 //156
		//for(counter1=25600; counter1<65536; counter1++){
		tmp1 += atof((const char*)linestring);

		tmp2++;
		//counter1++;
	}



	printf("Blocks since join: %lf\n", tmp3);
	printf("average luck: %lf\n", tmp1/tmp2);


	//tmp3 = tmp1/tmp2;
	double average = 0;
	//double needed_per = 25;
	double times = 0;
	//100 = (tmp1 + 50*tmp5) / (tmp3 + tmp5);
	//tmp5 = 100*tmp2;
	//tmp4 = tmp1 * tmp2;
	//tmp4 = tmp1 - tmp5;
	//times = tmp4 / needed_per;

	double percent = 5;
	//tmp4 = tmp1/tmp2;

	printf("luck\ttimes\taverage\n");
	while(percent < 80){
		for(times=0; times<100; times++){
			average = tmp1 + percent*times;
			average /= 16+times;
			if(average <= 100){
				printf("%lf %lf %lf\n", percent, times, average);
				break;
			}
		}
		percent += 5;
	}

	average = tmp1/tmp2;
	for(percent=5; percent<100; percent+=5){
		times = (tmp2*average - tmp2*100) / (100 - percent); //, if Z != 100
		printf("number of %lf%% blocks to be 100%% %lf\n", percent, times);
	}

	//printf("number of %lf%% blocks to be 100%% %lf\n", needed_per, times);

	fclose(input);
	return 0;
}
