Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
double total = 0.0;
for (month = 0; month < MONTHS; month++) total += sales[month];
cout << \ return 0; }
// pe5-6.cpp
#include
struct car { char name[20]; int year;};
int main(void) {
using namespace std; int n;
cout << \
cin >> n;
while(cin.get() != '\\n') // get rid of rest of line ;
car * pc = new car [n];
int i;
for (i = 0; i < n; i++) {
cout << \ cout << \ cin.getline(pc[i].name,20);
cout << \ cin >> pc[i].year;
while(cin.get() != '\\n') // get rid of rest of line ; }
cout << \ for (i = 0; i < n; i++)
cout << pc[i].year << \
delete [] pc; return 0; }
// pe5-7.cpp -- count words using C-style string
#include
#include
using namespace std; char word[STR_LIM]; int count = 0;
cout << \
while (cin >> word && strcmp(\
SP 6 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
++count;
cout << \ return 0; }
// pe5-9.cpp //nested loops
#include
int main() {
using namespace std; //introduces namespace std int rows; int row; int col;
int periods;
cout << \ cin >> rows;
for (row = 1; row <= rows; row++) {
periods = rows - row;
for (col = 1; col <= periods; col++) cout << '.';
// col already has correct value for next loop for ( ; col <= rows; col++) cout << '*'; cout << endl; }
return 0; }
Chapter 6
// pe6-1.cpp
#include
using namespace std; //introduces namespace std char ch;
cin.get(ch);
while(ch != '@') {
if (!isdigit(ch)) {
if (isupper(ch))
ch = tolower(ch); else if (islower(ch)) ch = toupper(ch); cout << ch; }
cin.get(ch); }
return 0;
SP 7 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
}
// pe6-3.cpp
#include
int main(void) {
using namespace std;
cout << \ cout << \ << \ char ch; cin >> ch;
while (ch != 'c' && ch != 'p' && ch != 't' && ch != 'g') {
cout << \ cin >> ch; }
switch (ch) {
case 'c' : cout << \ break;
case 'p' : cout << \ break;
case 't' : cout << \ break;
case 'g' : cout << \ break;
default : cout << \ }
return 0; }
// pe6-5.cpp
// Neutronia taxation #include
const double LEV1 = 5000; const double LEV2 = 15000; const double LEV3 = 35000; const double RATE1 = 0.10; const double RATE2 = 0.15; const double RATE3 = 0.20; int main( ) {
using namespace std; double income; double tax;
cout << \ cin >> income;
if (income <= LEV1) tax = 0;
else if (income <= LEV2)
tax = (income - LEV1) * RATE1; else if (income <= LEV3)
tax = RATE1 * (LEV2 - LEV1) + RATE2 * (income - LEV2); else
tax = RATE1 * (LEV2 - LEV1) + RATE2 * (LEV3 - LEV2) + RATE3 * (income - LEV3);
SP 8 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
cout << \
return 0; }
// pe6-7.cpp
#include
using namespace std; string word; char ch;
int vowel = 0;
int consonant = 0; int other = 0;
cout << \ cin >> word;
while ( word != \ {
ch = tolower(word[0]); if (isalpha(ch)) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') vowel++; else
consonant++; } else
other++; cin >> word; }
cout << vowel <<\
cout << consonant << \ cout << other << \
return 0; }
// pe6-8.cpp -- counting characters #include
#include
using namespace std; char filename[SIZE]; char ch;
ifstream inFile; // object for handling file input
cout << \ cin.getline(filename, SIZE);
inFile.open(filename); // associate inFile with a file if (!inFile.is_open()) // failed to open file {
cout << \ cout << \ exit(EXIT_FAILURE); }
int count = 0; // number of items read
SP 9 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
inFile >> ch; // get first value
while (inFile.good()) // while input good and not at EOF {
count++; // one more item read inFile >> ch; // get next value }
cout << count << \
inFile.close(); // finished with the file return 0; }
Chapter 7
//pe7-1.cpp -- harmonic mean
#include
double h_mean(double x, double y);
int main(void) {
using namespace std; double x,y;
cout << \ while (cin >> x >> y && x * y != 0)
cout << \ << y << \/* or do the reading and testing in two parts: while (cin >> x && x != 0) {
cin >> y; if (y == 0) break; ... */
cout << \ return 0; }
double h_mean(double x, double y) {
return 2.0 * x * y / (x + y); }
// pe7-3.cpp
#include
struct box {
char maker[40]; float height; float width; float length; float volume; };
SP 10 of 65 September 2, 2004