Figure 13.1

#include <iostream> // cin, cout, <<, >>
#include <string> // string
using namespace std;
int main()
{
  const int NUMBER_OF_CITIES = 6;
  const int MILEAGE_CHART[NUMBER_OF_CITIES][NUMBER_OF_CITIES]
 = { { 0, 97, 90, 268, 262, 130 }, // Daytona Beach
     { 97, 0, 74, 337, 144, 128 }, // Gainesville
     { 90, 74, 0, 354, 174, 201 }, // Jacksonville
     { 268, 337, 354, 0, 475, 269 }, // Miami
     { 262, 144, 174, 475, 0, 238 }, // Tallahassee
     { 130, 128, 201, 269, 238, 0 } }; // Tampa
const string CITY_MENU
    = "To determine the mileage between two cities,\n"
      "please enter the numbers of 2 cities from this menu:\n\n"
      " 0 for Daytona Beach, 1 for Gainesville\n"
      " 2 for Jacksonville, 3 for Miami\n"
      " 4 for Tallahassee, 5 for Tampa\n\n"
      "--> ";
	cout << CITY_MENU;
	int city1, city2;
	cin >> city1 >> city2;
	int mileage = MILEAGE_CHART[city1][city2];
	cout << "\nThe mileage between those 2 cities is "
	     << mileage << " miles.\n";
}
--------
Sample run:

To determine the mileage between two cities,
please enter the numbers of 2 cities from this menu:
0 for Daytona Beach, 1 for Gainesville
2 for Jacksonville, 3 for Miami
4 for Tallahassee, 5 for Tampa
--> 2 5
The mileage between those 2 cities is 201 miles.
 

 

Figure 13.3

#include "Table.h"

void print(ostream& out, const Table& aTable)
{
	for (int row = 0; row < aTable.size(); row++)
	{
	   for (int col = 0; col < aTable[row].size(); col++)
  	   out << aTable[row][col] << '\t';
  	   out << endl;
	}
} 	

 

 

Figure 13.4

#include <fstream> // ifstream
#include <cassert> // assert()

using namespace std;

void fill(const string& fileName, Table& aTable)
{
	ifstream in(fileName.data()); 		// open stream to file
	assert(in.is_open()); 			// verify
	int rows, 				// variables
	cols; 					// for dimensions
	in >> rows >> cols; 			// read dimensions
	Table locTable(rows, TableRow(cols)); 	// construct a local
	// rows x cols table
	for (int r = 0; r < rows; r++) 		// for each row
	   for (int c = 0; c < cols; c++) 	// input cols values
		in >> locTable[r][c]; 		// into row
	// assign locTable to aTable
	aTable = locTable; 			// so it has correct
						// dimensions
	in.close(); 				// close stream
}
 

 

 

Figure 13.5

void load(const string& fileName, Table& aTable)
{
	ifstream in(fileName.data()); 		// open stream to file
	assert(in.is_open()); 			// verify
	Table locTable; 			// an empty local table
	double aValue;				// input variable
	char separator; 			// to test for '\n'
	for (;;) 				// loop:
	{
	   TableRow aRow; 			// start with an empty row
	   for (;;) 				// loop:
	   {
		separator = in.peek(); 		// peek at the next char
		if (separator == '\n') break; 	// if at end of row, exit
		   in >> aValue; 		// read a value
		if (in.eof()) return; 		// if eof, quit
 		   aRow.push_back(aValue); 	// append value to row
	   } 					// end loop
	   in.get(separator); 			// consume the newline
	   locTable.push_back(aRow); 		// append row to table
	} // end loop
	aTable = locTable; 			// assign locTable to aTable
	in.close(); 				// close stream
}

 

 

Figure 13.7

#include "CartesianSystem.h"
#include <cmath> // sin(), cos(), ...
using namespace std;

// functions to be plotted
double f(double x);
double g(double x);

int main()
{
	cout << "This program graphs two functions\n"
	   << " (currently y = x*cos(x) and y = x)."
	   << "\n\nEnter the first and last X values: ";
	double xFirst, xLast;
	cin >> xFirst >> xLast;
	cout << "\nEnter the first and last Y values: ";
	double yFirst, yLast;
	cin >> yFirst >> yLast;
	CartesianSystem grid(xFirst, yFirst, xLast, yLast);
	grid.setPenWidth(3);
	grid.drawFunction(g, BLUE);
	grid.drawFunction(f, RED);
}

double f(double x) { return x * cos(x); }

double g(double x) { return x; }
---------------------
Sample run:

This program graphs two functions...
(currently y = x*cos(x) and y = x.)

Please enter the minimum and maximum x values: -8 8

Please enter the minimum and maximum y values: -7 7

 

 

Figure 13.9

#include "CartesianSystem.h"
#include <cmath> // sin(), cos()
using namespace std;

// functions to be plotted
double f(double x);
double g(double x);

int main()
{
	CartesianSystem grid;
	grid.setPenWidth(3);
	grid.drawRectangle(-1, -1, 1, 1, PURPLE, FRAME);
	grid.drawCircle(0, 0, 1, GREEN, FRAME);
	grid.drawFunction(g, BLUE);
	grid.drawFunction(f, RED);
	grid.waitForMouseClick();
	grid.clear(YELLOW);
	grid.setPenWidth(3);
	grid.drawFunction(g, DKGREEN);
	grid.drawFunction(f, BLUE);
	grid.waitForMouseClick();
	grid.clear(BLACK);
	grid.drawAxis(YELLOW, 3);
	grid.drawFunction(g, AQUA);
	grid.drawFunction(f, RED);
}

double f(double x) { return x * cos(x); }

double g(double x) { return x; }

 

 

Figure 13.11

void plotPoint(double x, double y, 			// CartesianSystem coords,
		int column, int row, 			// window (pixel) coords.
		CartesianSystem& cs) 			// where to plot
{
	ostringstream sout; 				// 1. build stringstream
	sout << setprecision(DEFAULT_PRECISION) 	// containing the
	     << '(' << x << ',' << y << ") : [" 	// info we want to
 	     << column << ',' << row << ']'; 		// display
	cs.drawString(x, y, sout.str()); 		// 2. display it
}