int sum (int n)
{
assert(n > 0);
int runningTotal = 0;
for (int count = 1; count <= n; count++)
runningTotal += count;
return runningTotal;
}
|
#include <iostream> // cout, cin, <<, >>
#include <cassert> // assert
using namespace std;
int sum(int n); // prototype for sum()
int main()
{
cout << "This program computes the sum of the integers from "
<<"1 through n.\n";
cout << "Enter a value for n: ";
int n;
cin >> n;
cout << "--> 1 + ... + " << n << " = " << sum(n) << endl;
}
/*** Insert the definition of function sum()
from Figure 7.1 here. ***/
|
This program computes the sum of the integers from 1 through n. Enter a value for n: 5 --> 1 + ... + 5 = 15 This program computes the sum of the integers from 1 through n. Enter a value for n: 100 --> 1 + ... + 100 = 5050 |
#include <iostream> // cout, cin, <<, >>, right
#include <iomanip> // setw()
using namespace std;
int main()
{
cout << "This program constructs a multiplication table\n"
<<"for the values 1*1 through lastX*lastY.\n";
int lastX, // the largest numbers being multiplied
lastY,
product; // the product of the two numbers
cout << "\nEnter two integer limit values (lastX and lastY): ";
cin >> lastX >> lastY;
for (int x = 1; x <= lastX; x++)
for (int y = 1; y <= lastY; y++)
{
product = x * y;
cout << right
<< setw(2) << x << " * "
<< setw(2) << y << " = "
<< setw(3) << product << endl;
}
}
|
This program constructs a multiplication table for the values 1*1 through lastX*lastY. Enter two integer limit values (lastX and lastY): 3 4 1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 |
#include <iostream> // <<, >>, cout, cin
using namespace std;
int main()
{
const double SMALL_NUMBER = 1.0e-3; // 1 millimeter
cout << "This program computes the number and height\n"
<< "of the rebounds of a dropped ball.\n";
cout << "\nEnter the starting height (in meters): ";
double height;
cin >> height;
cout << "\nStarting height: " << height << " meters\n";
int bounce = 0;
while (height >= SMALL_NUMBER)
{
height /= 2.0;
bounce++;
cout << "Rebound # " << bounce << ": "
<< height << " meters" << endl;
}
}
-----------------------
Sample run:
This program computes the number and height
of the rebounds of a dropped ball.
Enter the starting height (in meters): 15
Starting height: 15 meters
Rebound # 1: 7.5 meters
Rebound # 2: 3.75 meters
Rebound # 3: 1.875 meters
Rebound # 4: 0.9375 meters
Rebound # 5: 0.46875 meters
Rebound # 6: 0.234375 meters
Rebound # 7: 0.117188 meters
Rebound # 8: 0.0585938 meters
Rebound # 9: 0.0292969 meters
Rebound # 10: 0.0146484 meters
Rebound # 11: 0.00732422 meters
Rebound # 12: 0.00366211 meters
Rebound # 13: 0.00183105 meters
Rebound # 14: 0.000915527 meters
|
int digitsIn(int intValue)
{
int numDigits = 0;
do
{
numDigits++;
intValue /= 10;
}
while (intValue != 0);
return numDigits;
}
|
#include <iostream> // cout, cin, , <<, >>
using namespace std;
int digitsIn(int intValue);
int main()
{
int theValue;
cout << "Enter an integer value: ";
cin >> theValue;
cout << theValue << " contains "
<< digitsIn(theValue) << " digit(s).\n";
}
/*** Put definition of function digitsIn() from Figure 7.5 here. ***/
|
Please enter an integer value: 5 5 contains 1 digit(s). Please enter an integer value: 12 12 contains 2 digit(s). Please enter an integer value: 12345678 12345678 contains 8 digit(s). |
#include <iostream> // <<, >>, cout, cin
using namespace std;
int main()
{
cout << "Computing Component Mean Time to Failure\n\n";
int numComponents = 0;
double failureTime,
failureTimeSum = 0.0;
for (;;) // or while (true)
{
cout << "Enter a failure time (–1 to quit): ";
cin >> failureTime;
if (failureTime < 0) break;
failureTimeSum += failureTime;
numComponents++;
}
if (numComponents != 0)
cout << "\nThe mean failure time of the "
<< numComponents << " components is "
<< failureTimeSum / numComponents << endl;
else
cerr << "\nNo failure times to process!\n";
}
|
Computing Component Mean Time to Failure Enter a failure time (-1 to quit): 2.3 Enter a failure time (-1 to quit): 2.4 Enter a failure time (-1 to quit): 2.5 Enter a failure time (-1 to quit): 2.6 Enter a failure time (-1 to quit): 2.7 Enter a failure time (-1 to quit): -1 The mean failure time of the 5 components is 2.5 |
#include <iostream> // <<, cout, cin, get, eof()
using namespace std;
int main()
{
cout << "This program displays the numeric codes of whatever\n"
<<"characters you enter. (Type the eof character to quit.)\n";
char ch;
for (;;)
{
cin.get(ch);
if (cin.eof()) break;
cout << '\t' << int(ch) << endl;
}
}
--------------------------
Sample run (Unix System):
This program displays the numeric codes of whatever
characters you enter. (Type the eof character to quit.)
a
97
10
b
98
10
c
99
10
ABC
65
66
67
10
^D
|
#include <iostream> // <<, cout, cin, get, eof(), clear() using namespace std;
int main()
{
cout << "This program reads two lists, summing the first\n"
<<"and finding the product of the numbers in the second.\n\n"
<<"Enter the list to be summed (end list with eof):\n";
double number,
sum = 0;
for (;;) // read 1st list
{
cin >> number;
if (cin.eof()) break;
sum += number;
}
//eof flag is set,
cin.clear(); // so clear it
// This may not work in some versions of C++
cout << "\nEnter the list to be multiplied (end list with eof):\n";
double product = 1;
for (;;) // read 2nd list
{
cin >> number;
if (cin.eof()) break;
product *= number;
}
cout << "\nThe sum of the first list is " << sum << "\nand "
<< "the product of the second list is " << product << endl;
}
-----------------------------
Sample run (Unix System with gnu C++):
This program reads two lists, summing the first
and finding the product of the numbers in the second.
Enter the list to be summed (end list with eof):
1 2 3 4 5 6
^D
Enter the list to be multiplied (end list with eof):
10 9 8 7
^D
The sum of the first list is 21
and the product of the second list is 5040
|
#include <iostream> // <<, >>, cout, cin
using namespace std;
int main()
{
cout << "Computing Component Mean Time to Failure\n\n";
int numComponents;
double failureTime,
failureTimeSum = 0.0;
cout << "How many failure times will be entered? ";
cin >> numComponents;
for (int count = 1; count <= numComponents; count++)
{
cout << "Enter failure time #" << count << ": ";
cin >> failureTime;
failureTimeSum += failureTime;
}
if (numComponents > 0)
cout << "\nThe mean failure time of the "
<< numComponents << " components is "
<< failureTimeSum / numComponents << endl;
else
cerr << "\nNo failure times to process!\n";
}
--------------------------------
Sample run:
Computing Component Mean Time to Failure
How many failure times will be entered? 5
Enter failure time #1: 2.3
Enter failure time #2: 2.4
Enter failure time #3: 2.5
Enter failure time #4: 2.6
Enter failure time #5: 2.7
The mean failure time of the 5 components is 2.5
|
#include <iostream> // <<, >>, cout, cin
using namespace std;
int main()
{
cout << "Computing Component Mean Time to Failure\n";
int numComponents = 0;
double failureTime,
failureTimeSum = 0.0;
char response;
do
{
cout << "\nEnter a failure time: ";
cin >> failureTime;
failureTimeSum += failureTime;
numComponents++;
cout << "Do you have more data to enter (y or n)? ";
cin >> response;
}
while (response == 'y' || response == 'Y');
cout << "\nThe mean failure time of the "
<< numComponents << " components is "
<< failureTimeSum / numComponents << endl;
}
-----------------------------
Sample run:
Computing Component Mean Time to Failure
Enter a failure time: 2.3 Do you have more data to enter (y or n)? y Enter a failure time: 2.4 Do you have more data to enter (y or n)? y Enter a failure time: 2.5 Do you have more data to enter (y or n)? y Enter a failure time: 2.6 Do you have more data to enter (y or n)? y Enter a failure time: 2.7 Do you have more data to enter (y or n)? n The mean failure time of the 5 components is 2.5 |
#include "Name.h" // our Name class
class TitledName : public Name // a TitledName is a Name
{
public:
TitledName();
TitledName(string title, string fName, string mName, string lName);
string getTitle() const;
void setTitle(string newName);
void read(istream& in);
void print(ostream& out) const;
// ... other methods omitted ...
private:
string myTitle;
};
inline TitledName::TitledName()
: Name() // use parent constructor
{ // to initialize inherited vars
myTitle = "";
}
inline TitledName::TitledName(string title, string fName,
string mName, string lName)
: Name(fName, mName, lName) // use parent constructor
{ // to initialize inherited vars
myTitle = title;
}
inline string TitledName::getTitle() const
{
return myTitle;
}
inline void TitledName::setTitle(string newTitle)
{
myTitle = newTitle;
}
inline void TitledName::read(istream& in)
{
in >> myTitle; // read the title -- it's first
Name::read(in); // read the name using parent method
}
inline void TitledName::print(ostream& out) const
{
out << myTitle; // print the title -- it's first
Name::print(out); // print the name using parent method
}
|
#include "TitledName.h" // class TitledName
int main()
{
cout << "\nEnter a titled name: ";
TitledName aTitledName;
aTitledName.read(cin); // overridden
cout << aTitledName.getLastName() << ", " // inherited
<< aTitledName.getTitle() << ' ' // not inherited
<< aTitledName.getFirstName() << ' ' // inherited
<< aTitledName.getMiddleInitial() << ". " // inherited
<< endl;
}
---------------------
Sample Run:
Enter a titled name: President George Walker Bush
Bush, President George W.
|