//
//
//  rocks.cc
//
//      AUTHOR: Russell Lowke
//         T.F: Francis Crick
//  CSCIE-s111: Unit Two, Main - question 9 [worth 3pts],  7/3/2002
//
//
// Description: Program averages the "number of rocks thrown at buildings"
//              First asking for the number of days to tally, then asking
//              the number of rocks for each day
//
//       Usage: When prompted, key in the number of days (nDays) to tally
//              When prompted, key in number of rocks for each day
//
//      Method: main calls on three finctions
//              getNumberOfDays() - returns number of days to average
//              getSumOfRocks()   - tallys al the rocks
//              displayAverage()  - prints the avarage of all rocks.
//
//       Notes: All input MUST be integers.
//

	
#include <iostream>
#include <iomanip>

//
//  enter number of days to calculate
//
int getNumberOfDays()
{
	int nDays;
	
	do {
		cout << endl << "How many rock-throwing days do you want averaged up? ";
		cin >> nDays;
		
		if (nDays <= 0)  // check that nDays > 0
			cout << endl << "Number of days must be greater than zero.";
	} while (nDays <= 0);
	
	return nDays;
}


//
//  enter rocks for each day
//
int getSumOfRocks(int nDays)
{
	int nRocksToday = 0,
	    nSumRocks   = 0;
	
	// commence entering data
	cout << endl << "OK -- Please start typing them in, one per line:" << endl;
	
	// loop input each day
	for (int counter = 0; counter < nDays; counter++) {
		do {
			cout << "How many rocks were thrown on day number " << counter+1 << "? ";
			cin >> nRocksToday;
		
			// check if nRocksToday is equal to or less than 0
			if (nRocksToday < 0)
				cout << endl << "Number of rocks thrown should be positive.";
		} while (nRocksToday < 0);
	 
		//  add todays rocks to nSumRocks
		nSumRocks = nSumRocks + nRocksToday;
	}
	
	return nSumRocks;
}


//
//  display averageRocks
//
void displayAverage(double averageRocks)
{
	// set cout parameters
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	
	cout << endl << endl << "There were an average of ";
	cout << averageRocks << " rocks thrown per day!" << endl;
}


void main()
{
	// declare vars and initialize
	
	int    nDays        = 0,
		   nSumRocks    = 0;
	double averageRocks = 0;
	
	nDays     = getNumberOfDays();
	nSumRocks = getSumOfRocks(nDays);
	
	//  calc average of all rocks thrown
	averageRocks = nSumRocks / (double) nDays;
	
	displayAverage(averageRocks);
}