// Ever wonder how much louder your computer will be if you add a couple extra fans?
//
//

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

double fans[40]; 
int    count = 0;


void do_help( int argc, char **argv )
{
	printf("usage:  %s [-h] [fan1 [fan2 [fan3[...]]]\n", argv[0]);
	printf("   estimates the sound level in decibels created by multiple computer fans\n");
	printf("   'fan' values should be given in db\n\n");
}


void do_compute()
{
	double I0 = 1.0e-12;   // watts/m^2 
	double db, itotal;
	int j;

	// db = (10db) * log( I / I0 )
	// 

	itotal = 0;

	for(j=0; j<count; j++) 
		itotal += pow(10, (fans[j] / 10.0));
	
	db = 10.0 * log10( itotal );

	printf("Total sound pressure:  %4.2lf db\n", db );
}


int main( int argc, char **argv )
{
	int i;

	memset( fans, 0, sizeof(fans) );

	if (argc == 1) {
		do_help(argc, argv);
		return 0;
	}

	for (i=1; i < argc; i++) {
		if (strncmp(argv[i], "-h", 2) == 0) {
			do_help(argc, argv);
			return 0;
		}

		sscanf( argv[i], "%lf", &fans[count++] );
	}

	do_compute();
	return 1;
}


