Figure 6.1

string mascot(string university)
{
   if (university == "Illinois")
	return "Fighting Illini";
   else if (university == "Indiana")
	return "Hoosiers";
   else if (university == "Iowa")
	return "Hawkeyes";
   else if (university == "Michigan")
	return "Wolverines";
   else if (university == "Michigan State")
	return "Spartans";
   else if (university == "Minnesota")
	return "Golden Gophers";
   else if (university == "Northwestern")
	return "Wildcats";
   else if (university == "Ohio State")
	return "Buckeyes";
   else if (university == "Penn State")
	return "Nittany Lions";
   else if (university == "Purdue")
	return "Boilermakers";
   else if (university == "Wisconsin")
	return "Badgers";
   else
   {
	cerr << "mascot: " << university
	     << " is not known by this program!";
	return "";
   }
}

 

Figure 6.2

#include <iostream> 		// cin, cout, <<, >>
#include <string> 		// string, ==, getline()
using namespace std;

string mascot(string university);

int main()
{
   string school;
   cout << "\nEnter a Big 10 school (Q to quit): ";
   getline(cin, school);
   while (school != "Q" && school != "q")
   {
	cout << mascot(school) << endl;
	cout << "\nEnter a Big 10 school (Q to quit): ";
	getline(cin, school);
   }
}
/*** Insert definition of mascot() from Figure 6.1 here ***/
 

 

 

Sample Run Figure 6.2

Enter a Big 10 school (Q to quit): Michigan
Wolverines

Enter a Big 10 school (Q to quit): Ohio State
Buckeyes

Enter a Big 10 school (Q to quit): Missouri
Mascot: Missouri is not known by this program!

Enter a Big 10 school (Q to quit): Minnesota
Golden Gophers

Enter a Big 10 school (Q to quit): Q

 

 

Figure 6.3

#include <iostream> 			// cin, cout, <<, >>
#include <string> 			// string
using namespace std;
#include "Heat.h" 			// fahrToCelsius(), celsiusToFahr(), ...
int main()
{
    const string MENU = "To convert arbitrary temperatures, enter:\n"
			" A - to convert Fahrenheit to Celsius;\n"
			" B - to convert Celsius to Fahrenheit;\n"
			" C - to convert Celsius to Kelvin;\n"
			" D - to convert Kelvin to Celsius;\n"
			" E - to convert Fahrenheit to Kelvin; or\n"
			" F - to convert Kelvin to Fahreneht.\n"
			"--> ";
   cout << MENU;
   char conversion;
   cin >> conversion;
   cout << "\nEnter the temperature to be converted: ";
   double temperature;
   cin >> temperature;
   double result;
   switch (conversion)
   {
   case 'A': case 'a':
	result = fahrToCelsius(temperature);
	break;
   case 'B': case 'b':
	result = celsiusToFahr(temperature);
	break;
   case 'C': case 'c':
 	result = celsiusToKelvin(temperature);
	break;
   case 'D': case 'd':
	result = kelvinToCelsius(temperature);
	break;
   case 'E': case 'e':
	result = fahrToKelvin(temperature);
	break;
   case 'F': case 'f':
	result = kelvinToFahr(temperature);
	break;
   default:
	cerr << "\n*** Invalid conversion: "
	     << conversion << endl;
	result = 0.0;
   }
   cout << "The converted temperature is " << result << endl;
}

 

Sample Run Figure 6.3

 To convert arbitrary temperatures, enter:
   A - to convert Fahrenheit to Celsius;
   B - to convert Celsius to Fahrenheit;
   C - to convert Celsius to Kelvin;
   D - to convert Kelvin to Celsius;
   E - to convert Fahrenheit to Kelvin; or
   F - to convert Kelvin to Fahrenheit.
   --> B

Enter the temperature to be converted: 100

The converted temperature is 212

 

 

Figure 6.4

string yearName(int yearCode)
{
   switch (yearCode)
   { 
   case 1:
	return "Freshman";
   case 2:
	return "Sophomore";
   case 3:
	return "Junior";
   case 4:
	return "Senior";
   case 5:
	return "Graduate";
   default:
	cerr << "yearName: code error: " << yearCode << endl;
	return "";
   }
}

 

Figure 6.5

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

string yearName(int yearCode);

int main()
{
   int number;
   cout << "Enter the number of a class year: ";
   cin >> number;
   cout << yearName(number) << endl;
}

/*** Insert the definition of yearName() from Figure 6.4 here ***/

 

Sample Run Figure 6.5

Enter the number of a class year: 1
Freshman

Enter the number of a class year: 3
Junior

Enter the number of a class year: 6
YearName: code error: 6

 

 

Figure 6.6

char letterGrade(double weightedAverage)
{
   switch ( int(weightedAverage) / 10 )
   {
   case 10: 		// int(100)/10 -> 10
   case 9: 		// int(90-99)/10 -> 9
	return 'A';
   case 8: 		// int(80-89)/10 -> 8
	return 'B';
   case 7: 		// int(70-79)/10 -> 7
	return 'C';
   case 6: 		// int(60-69)/10 -> 6
	return 'D';
   default: 		// not so good!
	return 'F';
   }
}

 

 

Figure 6.7

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

char letterGrade(double weightedAverage);

int main()
{
   const double HOMEWORK_WEIGHT = 0.2, 		// weights for homework,
   TEST_WEIGHT = 0.5, 				// tests,
   EXAM_WEIGHT = 0.3; 				// and the exam.
   cout << "This program computes a final course grade using the\n"
   	<<"homework average, test average, and a final exam "
	<<"score.\n";
   cout << "\nEnter the homework average, test average, "
	<< "and exam score:\n";
   double homeworkAverage, 			// the average of the homework scores
   testAverage, 				// the average of the test scores
   examScore; 					// the final-exam score
   cin >> homeworkAverage >> testAverage >> examScore;
   double finalAverage = HOMEWORK_WEIGHT * homeworkAverage +
   TEST_WEIGHT * testAverage +
	EXAM_WEIGHT * examScore;
   char grade = letterGrade(finalAverage); 	// the letter grade received
   cout << "Final Average = " << finalAverage
	<< ", Grade = " << grade << "\n\n";
}

/*** Insert definition of letterGrade() from Figure 6.6 here ***/
 

 

 

Sample Run Figure 6.7

This program computes a final course grade using the
homework average, test average, and a final-exam score.

Enter the homework average, test average, and exam score:
100 100 100
Final Average = 100, Grade = A

. . .
Enter the homework average, test average, and exam score:
30 40 50
Final Average = 41, Grade = F

. . .
Enter the homework average, test average, and exam score:
56.2 62.7 66.5
Final Average = 62.54, Grade = D

. . .
Enter the homework average, test average, and exam score:
87.5 91.3 80
Final Average = 87.15, Grade = B

 

 

 

Figure 6.8

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

int main()
{
   cout << "Enter two binary inputs: ";
   short digit1, digit2; 		// the two binary inputs
   cin >> digit1 >> digit2;
					// the two circuit outputs
   bool sum = (digit1 || digit2) && !(digit1 && digit2),
   carry = (digit1 && digit2);
   cout << "Carry = " << carry << " Sum = " << sum << "\n\n";
}

 

 

 

Sample Run Figure 6.8

Enter two binary inputs: 0 0
Carry = 0 Sum = 0

Enter two binary inputs: 0 1
Carry = 0 Sum = 1

Enter two binary inputs: 1 0
Carry = 0 Sum = 1
Enter two binary inputs: 1 1
Carry = 1 Sum = 0

 

 

Figure 6.9

#include <iostream> 				// ostream class
#include <string> 				// the string class
#include <cassert> 				// assert()
using namespace std;

class Name
{
   public:
   Name(); 					// constructors
   Name(string first, string middle, string last);
   string getFirstName() const; 		// accessors
   string getLastName() const;
   char getMiddleInitial() const;
   string getSignature() const;
   void setFirstName(string newFirstName);	// mutators
	// setMiddleName() left as an exercise
   void setLastName(string newLastName);
   void print(ostream& out) const; 		// I/O methods
   void read(istream& in);
	// ... other Name-operation declarations go here
   private:
   string myFirstName,
   myMiddleName,
   myLastName;
};

// ... constructor, accessor definitions omitted ...

inline void Name::setFirstName(string newFirstName)
{
   myFirstName = newFirstName;
}

// setMiddleName() left as an exercise

inline void Name::setLastName(string newLastName)
{
   myLastName = newLastName;
}

inline void Name::read(istream& in)
{
   in >> myFirstName >> myMiddleName >> myLastName;
}

 

 

 

Figure 6.10

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

int main()
{
   cout << "Enter a full name (first, middle, last): ";
   Name aName;
   aName.read(cin);
   aName.print(cout);
   cout << endl;
   aName.setFirstName("Yertle");
   aName.setLastName("Turtle");
   aName.print(cout);
}
-----------
Sample run:

Enter a full name (first, middle, last): Popeye the Sailorman
Popeye the Sailorman
Yertle the Turtle

 

 

 

Figure 6.11

#include <iostream> 			// istream
#include <cmath> 			// pow()
using namespace std;

const double PI = 3.14159;

class Sphere
{
   public:
   Sphere();
   double getWeight() const;
  	// ... other Sphere accessors omitted...
   void setRadiusAndDensity(double radius, double density);
   void setRadiusAndWeight(double radius, double weight);
   void setDensityAndWeight(double density, double weight);
   void print(ostream& out) const;
   void readRadiusAndDensity(istream& in);
   void readRadiusAndWeight(istream& in);
   void readDensityAndWeight(istream& in);
	// ... other Sphere operations omitted...

   private:
   double myRadius, myWeight, myDensity;
};

// ... "simple" Sphere method definitions...

 

 

 

Figure 6.12

#include "Sphere.h" 					// our Sphere class

void Sphere::setRadiusAndDensity(double radius, double density)
{
   if (radius > 0 && density > 0) 			// validate arguments
   { 							// update attributes
	myWeight = density * 4.0 * PI * pow(radius, 3) / 3.0;
	myRadius = radius;
	myDensity = density;
   }
   else 						// precondition failed
   {
	cerr << "setRadiusAndDensity(r, d): invalid arguments "
	     << " r = " << radius << ", d = " << density;
 	myRadius = myDensity = myWeight = 0.0;
   }
}


// ... other Sphere mutators left as exercises ...

void Sphere::readRadiusAndDensity(istream& in)
{
   double newRadius, newDensity;
   in >> newRadius >> newDensity; 			// read inputs from in
   if (newRadius > 0 && newDensity > 0) 		// validate inputs
   { 							// update attributes
	myWeight = newDensity * 4.0 * PI * pow(newRadius, 3) / 3.0;
	myRadius = newRadius;
	myDensity = newDensity;
   }
   else 						// precondition failed
   {
	cerr << "readRadiusAndDensity(in): invalid input values "
	     << " r = " << newRadius << ", d = " << newDensity;
	myRadius = myDensity = myWeight = 0.0;
   }
}

 

 

 

Figure 6.13

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

int main()
{
   cout << "Enter a sphere's radius and density: ";
   Sphere aSphere;
   aSphere.readRadiusAndDensity(cin);
   cout << "The sphere's weight is "
	<< aSphere.getWeight() << endl;
}
-------------------
Sample run:

Enter a sphere's radius and density: 6.5 14.6
The sphere's weight is 16795