void divideInts(int op1, int op2, int& quotient, int& remainder) { assert (op2 != 0); quotient = op1 / op2; remainder = op1 % op2; } |
#include <iostream> // <<, cout, endl #include <cassert> // assert() using namespace std; void divideInts(int op1, int op2, int& quotient, int& remainder); int main() { cout << " Division Table\n\n"; int quot, // variables to hold the values rem; // sent back from divideInts() for (int j = 1; j <= 4; j++) { for (int i = 1; i <= 4; i++) { divideInts(i, j, quot, rem); cout << i << " divided by " << j << " gives a quotient of " << quot << " and a remainder of " << rem << endl; } cout << endl; } } /*** Insert definition of divideInts() from Figure 8.1 here. ***/ |
Division Table 1 divided by 1 gives a quotient of 1 and a remainder of 0 2 divided by 1 gives a quotient of 2 and a remainder of 0 3 divided by 1 gives a quotient of 3 and a remainder of 0 4 divided by 1 gives a quotient of 4 and a remainder of 0 1 divided by 2 gives a quotient of 0 and a remainder of 1 2 divided by 2 gives a quotient of 1 and a remainder of 0 3 divided by 2 gives a quotient of 1 and a remainder of 1 4 divided by 2 gives a quotient of 2 and a remainder of 0 1 divided by 3 gives a quotient of 0 and a remainder of 1 2 divided by 3 gives a quotient of 0 and a remainder of 2 3 divided by 3 gives a quotient of 1 and a remainder of 0 4 divided by 3 gives a quotient of 1 and a remainder of 1 1 divided by 4 gives a quotient of 0 and a remainder of 1 2 divided by 4 gives a quotient of 0 and a remainder of 2 3 divided by 4 gives a quotient of 0 and a remainder of 3 4 divided by 4 gives a quotient of 1 and a remainder of 0 |
#include <iostream> // cout, cin, <<, >> #include <string> // string, getline using namespace std; void decomposeName(const string& fullName, string& firstName, string& middleName, string& lastName); int main() { cout << "Enter a full name: "; string fullName; getline(cin, fullName); string fName, mName, lName; decomposeName(fullName, fName, mName, lName); cout << fName << endl << mName << endl << lName << endl; } #include <cassert> // assert() using namespace std; void decomposeName(const string& fullName, string& firstName, string& middleName, string& lastName) { int firstBlankIndex = fullName.find(' ', 0); assert(firstBlankIndex != string::npos); firstName = fullName.substr(0, firstBlankIndex); int secondBlankIndex = fullName.find(' ', firstBlankIndex + 1); assert(secondBlankIndex != string::npos); middleName = fullName.substr(firstBlankIndex + 1, secondBlankIndex - firstBlankIndex - 1); int fullNameSize = fullName.size(); lastName = fullName.substr(secondBlankIndex + 1, fullNameSize - secondBlankIndex - 1); } ------------------------- Sample run: Enter a full name: John Quincy Doe John Quincy Doe |
void makeChange(double purchaseAmount, // amount of purchase double payment, // amount of payment int& dollars, // dollars of change int& quarters, // quarters of change int& dimes, // dimes of change int& nickels, // nickels of change int& pennies) // pennies of change { int change = int(100.0 * (payment - purchaseAmount) + 0.5); if (change > 0) { dollars = change / 100; // 100 pennies per dollar change %= 100; // compute remaining change quarters = change / 25; // 25 pennies per quarter change %= 25; // compute remaining change dimes = change / 10; // 10 pennies per dime change %= 10; // compute remaining change nickels = change / 5; // 5 pennies per nickel pennies = change % 5; // pennies are all that's left } else { cerr << "*** Purchase amount: " << purchaseAmount << " exceeds payment: " << payment << endl; dollars = quarters = dimes = nickels = pennies = 0; } } |
#include <iostream> // cout, cin, <<, >> #include <string> // string using namespace std; void makeChange(double purchaseAmount, double payment, int& dollars, int& quarters, int& dimes, int& nickels, int& pennies); int main() { cout << "This program tests a change-making function...\n\n"; double itemCost, // a purchase amountPaid; // what was paid int numDollars, // variables for numQuarters, // the values numDimes, // to be output numNickels, numPennies; for (;;) { cout << "Enter item cost (negative to quit) and amount paid: "; cin >> itemCost; if (itemCost < 0) break; cin >> amountPaid; makeChange(itemCost, amountPaid, numDollars, numQuarters, numDimes, numNickels, numPennies); cout << "The change from this purchase is:\n" << numDollars << " dollars,\n" << numQuarters << " quarters,\n" << numDimes << " dimes,\n" << numNickels << " nickels, and\n" << numPennies << " pennies\n\n"; } } /*** Insert definition of makeChange() from Figure 8.4 here. ***/------------------------------------------------- Sample Runs This program tests a change-making function... Enter item cost (negative to quit) and amount paid: 1.01 2.00 The change from this purchase is: 0 dollars, 3 quarters, 2 dimes, 0 nickels, and 4 pennies Enter item cost (negative to quit) and amount paid: 1.34 5.00 The change from this purchase is: 3 dollars, 2 quarters, 1 dimes, 1 nickels, and 1 pennies Enter item cost (negative to quit) and amount paid: 9.99 10.00 The change from this purchase is: 0 dollars, 0 quarters, 0 dimes, 0 nickels, and 1 pennies Enter item cost (negative to quit) and amount paid: -1 |
int factorial(int n); inline int sum(int n) { return n * (n + 1) / 2; } // ... other function prototypes and/or inline definitions |
void move(int n, char source, char destination, char spare) { if (n <= 1) // anchor cout << "Move the top disk from " << source << " to " << destination << endl; else { // inductive case move(n-1, source, spare, destination); move(1, source, destination, spare); move(n-1, spare, destination, source); } } |
#include <iostream> using namespace std; void move(int n, char source, char destination, char spare); int main() { const char PEG1 = 'A', // the three pegs PEG2 = 'B', PEG3 = 'C'; cout << "This program solves the Hanoi Towers puzzle.\n\n"; cout << "Enter the number of disks: "; int numDisks; // the number of disks to be moved cin >> numDisks; cout << endl; move(numDisks, PEG1, PEG2, PEG3); // the solution } /*** Insert definition of move() from Figure 8.10 here. ***/ |
This program solves the Hanoi Towers puzzle. Enter the number of disks: 4 Move the top disk from A to B Move the top disk from A to C Move the top disk from B to C Move the top disk from A to B Move the top disk from C to A Move the top disk from C to B Move the top disk from A to B Move the top disk from A to C Move the top disk from B to C Move the top disk from B to A Move the top disk from C to A Move the top disk from B to C Move the top disk from A to B Move the top disk from A to C Move the top disk from B to C |
class Sphere { public: Sphere(); // ... other methods omitted ... static int getNumberOfSpheres(); private: double myRadius, myDensity, myWeight; static int numberOfSpheres; }; int Sphere::numberOfSpheres = 0; inline Sphere::Sphere() { myRadius = myDensity = myWeight = 0.0; numberOfSpheres++; } inline int Sphere::getNumberOfSpheres() { return numberOfSpheres; } |
#include <iostream> using namespace std; #include "Sphere.h" int main() { for (;;) { cout << "\nEnter a positive value (0 to quit): "; int aValue; cin >> aValue; if (aValue < 1) break; Sphere aSphere; cout << "Total number of spheres: " << Sphere::getNumberOfSpheres() << endl; } } ------------------------ Sample Run: Enter a positive value (0 to quit): 1 Total number of spheres: 1 Enter a positive value (0 to quit): 1 Total number of spheres: 2 Enter a positive value (0 to quit): 1 Total number of spheres: 3 Enter a positive value (0 to quit): 1 Total number of spheres: 4 Enter a positive value (0 to quit): 0 |
#include <iostream> using namespace std; #include "Sphere.h" // now contains a destructor int main() { for (;;) { cout << "\nEnter a positive value (0 to quit): "; int aValue; cin >> aValue; if (aValue < 1) break; Sphere s0; // construct a Sphere object cout << "Inside the loop, total number of spheres is: " << Sphere::getNumberOfSpheres() << endl; } // Sphere destructor gets called here for // s0 each repetition cout << "\nOutside the loop, total number of spheres is: " << Sphere::getNumberOfSpheres() << endl; Sphere s1, s2, s3; // construct 3 Sphere objects cout << "At the end, total number of spheres is: " << Sphere::getNumberOfSpheres() << endl; } // Sphere destructor gets called // here for s1, s2, and s3 ------------------------------------------ Sample Run: Enter a positive value (0 to quit): 9 Inside the loop, total number of spheres is: 1 Enter a positive value (0 to quit): 9 Inside the loop, total number of spheres is: 1 Enter a positive value (0 to quit): 9 Inside the loop, total number of spheres is: 1 Enter a positive value (0 to quit): 0 Outside the loop, total number of spheres is: 0 At the end, total number of spheres is: 3 |