Figure 10.1

#include <iostream> // cout, <<, fixed, showpoint
#include <fstream> // ifstream, >>, eof(), close()
#include <iomanip> // setprecision()
#include <string> // string, getline()
#include "Query.h" // interactiveOpen() Fig. 9.3
using namespace std;

const int CAPACITY = 6; // # of array elements

int main()
{
   cout << "Quality Control: "
	<<"Component Failure Frequency Distribution.\n\n";
   ifstream inStream;
   interactiveOpen(inStream);
   int count[CAPACITY] = {0}, // array of counters
   numFailures, // input variable
   numCircuitBoards = 0; // # of input values
   for (;;) // loop:
   {
	inStream >> numFailures; // read input value
	if (inStream.eof()) break; // if done, stop reading
	    count[numFailures]++; // increment its counter
	numCircuitBoards++; // one more input value
   } // end loop
   inStream.close(); // close the stream
   cout << "\nOut of " << numCircuitBoards << " circuit boards:\n"
   	<< setprecision(1) << fixed << showpoint;
   for (int i = 0; i < CAPACITY; i++) // output counters
	cout << count[i] << " had " << i
	     << " failed components (" // and percentages
	     << double(count[i]) / numCircuitBoards * 100
	     << "%)" << endl;
}

-------------------------
Sample run:

Quality Control: Component Failure Frequency Distribution.
Enter the name of the input file: failureData.txt
Out of 71 circuit boards:
57 had 0 failed components (80.3%)
7 had 1 failed components (9.9%)
4 had 2 failed components (5.6%)
2 had 3 failed components (2.8%)
1 had 4 failed components (1.4%)
0 had 5 failed components (0.0%)

 

Figure 10.2

#include <iostream>		//cout,<<
using namespace std;

const int CAPACITY =4;
typedef int IntArray [CAPACITY ];

void printArray(char name,IntArray x,int numElements);

int main()
{
   IntArray a ={0,1,2,3},
   b ={4,5,6,7},
   c ={8,9,10,11};
   int below =-3,
   above =6;
   printArray('a',a,4);
   printArray('b',b,4);
   printArray('c',c,4);
   b [below ] =-999;
   b [above ] =999;
   cout <<endl;
   printArray('a',a,4);
   printArray('b',b,4);
   printArray('c',c,4);
}

#include <iomanip>			//setw()

void printArray(char name,IntArray x,int numElements)
{
   cout <<name <<"=";
   for (int i =0;i <numElements;i++)
  	cout <<setw(5)<<x [i ];
   cout <<endl;
}
-----------------------
Sample run:
a =0 1 2 3
b =4 5 6 7
c =8 9 10 11
a =0 - 2 3
b =4 5 6 7
c =8 9 999 11
 

 

Figure 10.3

#include <iostream>//cout,<<,>>
#include <fstream>//ifstream,ofstream
#include <string>//string
#include <cassert>//assert()
#include <vector>//vector<T>
#include <algorithm>//sort()
using namespace std;

int main()
{
   cout <<"This program sorts the data in a file"
  	<<"named 'employeeData.txt'.\n";
   ifstream inStream("employeeData.txt");//input stream
   assert(inStream.is_open());
   vector<string>empVector;//sequence-holder
   string empLine;//input variable
   for (;;)//loop:
   {
	getline(inStream,empLine);//read a name
	if (inStream.eof())break;//quit if eof
  	   empVector.push_back(empLine);//append name
   }//end loop
   inStream.close();//close stream
   sort(empVector.begin(),empVector.end());//sort vector
   ofstream outStream("payrollData.txt");//output stream
   assert(outStream.is_open());
   for (int i =0;i <empVector.size();i++)//display
	outStream <<empVector [i ] <<endl;//vector
   outStream.close();
   cout <<"\nProcessing complete.See 'payrollData.txt'.\n";
}
------------------

Listing of input file employeeData.txt :

Somebody,Jane 950.00
Yahyah,Noah 845.00
Newman,Alfred 735.00
Bigfoot,Ben 425.00
Valyou,Mary 300.00
Smith,Nancy 195.00
Juan,John 175.00
Doe,John 150.00
Buck,John 150.00
Deere,John 150.00
-----------------------
Sample run:

This program sorts the data in a file named 'employeeData.txt'.
Processing complete.
-----------------------

Listing of output file payrollData.txt :

Bigfoot,Ben 425.00
Buck,John 150.00
Deere,John 150.00
Doe,John 150.00
Juan,John 175.00
Newman,Alfred 735.00
Smith,Nancy 195.00
Somebody,Jane 950.00
Valyou,Mary 300.00
Yahyah,Noah 845.00

 

Figure 10.4

template <typename T>
void read(istream& in, vector<T>& theVector)
{
   T inputValue;
   for (;;)
   {
	in >> inputValue;
	if ( in.eof() ) break;
	    theVector.push_back(inputValue);
   }
}

 

Figure 10.5

template <typename T>
void print(ostream& out, const vector<T>& theVector)
{
    for (int i = 0; i < theVector.size(); i++)
	out << theVector[i] << ' ';
}

 

Figure 10.6

double mean(const vector<double>& vec)
{
    if ( vec.empty() )
    {
	cerr << "\n***mean(vector): vector is empty!\n" << endl;
	return 0.0;
    }
    else
	return accumulate(vec.begin(), vec.end(), 0.0) / vec.size();
}

 

 

#include <iostream> 	// ostream
#include <fstream> 	// ifstream
#include <vector> 	// vector
#include <string> 	// string
#include <cassert> 	// assert()
using namespace std;

class Dataset
{
   public:
	// constructor
   Dataset(const string& fileName);
	// accessors
   string getTitle() const;
   vector<double> getValues() const;
   double getValue(int index) const;
	// output
   void print(ostream& out) const;
	// statistics
   double getMin() const;
   double getMax() const;
   double getMean() const;
   double getMedian() const;
   double getStandardDev() const;

// ... other operations omitted ...

   private:
   string myTitle;
   vector<double> myValues;
};

 

Figure 10.8

#include "Dataset.h"
#include <cctype> 				// isalpha()
#include <numeric> 				// accumulate()
#include <algorithm> 				// sort()
using namespace std;

Dataset::Dataset(const string& fileName)
{
   ifstream fin( fileName.data() ); 		// open stream
   assert( fin.is_open() ); 			// validate
   char ch = fin.peek(); 			// check for title
   if ( isalpha(ch) ) 				// if present
	getline(fin, myTitle); 			// initialize myTitle
   else
	myTitle = "Untitled data set"; 		// set default title
   double aValue;
   for (;;) 					// initialize myValues
   {
	fin >> aValue; 				// read a value
	if ( fin.eof() ) break; 		// if successful
	    myValues.push_back(aValue); 	// append it to myValues
   }
   fin.close(); 				// close the stream
}

 

Figure 10.9

// ... #includes and other Dataset operations omitted
	...
// ... end of class declaration

inline string Dataset::getTitle() const
{
   return myTitle;
}

inline double Dataset::getValue(int index) const
{
   assert(0 <= index && index < myValues.size());
   return myValues[index];
}

 

Figure 10.10

// ... #includes and other Dataset operations omitted

double Dataset::getMean() const
{
   assert( !myValues.empty() );
   double sum = accumulate(myValues.begin(), myValues.end(), 0.0);
   return sum / myValues.size();
}

 

Figure 10.11

// ... #includes and other Dataset operations omitted
inline bool even(int i)
{
   return i % 2 == 0;
}

double Dataset::getMedian() const
{
   vector<double> copy = myValues;
   sort( copy.begin(), copy.end() );
   int mid = copy.size() / 2, 		// integer division
   mid1 = mid - 1;
   if ( even( copy.size() ) )
	return (copy[mid] + copy[mid1]) / 2;
   else
	return copy[mid];
}

 

Figure 10.12

#include "Dataset.h" 		// Dataset
#include <iostream> 		// cin, cout
using namespace std;

int main()
{
   cout << "\nEnter the name of the data file: ";
   string fileName;
   getline(cin, fileName);
   Dataset dSet(fileName);
   cout << "Dataset: " << dSet.getTitle()
	<< "\n The mean value is: " << dSet.getMean()
	<< ",\n and the median value is: " << dSet.getMedian()
	<< endl;
}

----------------------------

Sample Runs:
Enter the name of the data file: data1.txt
Dataset: Rainfall Readings
the mean value is: 1.5,
and the median value is: 1.5

Enter the name of the data file: data2.txt
Dataset: Temperature Readings
the mean value is: 33.3333,
and the median value is: 30