Professional Documents
Culture Documents
0133591743 9780133591743
Link full download
Solution Manual:
https://testbankpack.com/p/solution-manual-for-problem-solving-with-c-
9th-edition-savitch-0133591743-9780133591743/
Test Bank:
https://testbankpack.com/p/test-bank-for-problem-solving-with-c-9th-
edition-savitch-0133591743-9780133591743/
Chapter 2
C++ Basics
Design: To convert 14 ounces (of cereal) to metric tons, we use the 'ratio of units' to tell
us whether to divide or multiply:
1 metric tons
14 ounces * * = 0.000397 metric tons
35,273 ounces
The use of units will simplify the determination of whether to divide or to multiply in
making a conversion. Notice that ounces/ounce becomes unit-less, so that we are left
with metric ton units. The number of ounces will be very, very much larger than the
number of metric tons. It is then reasonable to divide the number of ounces by the
number of ounces in a metric ton to get the number of metric tons.
Now let metricTonsPerBox be the weight of the cereal box in metric tons. Let
ouncesPerBox the be the weight of the cereal box in ounces. Then in C++ the formula
becomes:
1
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 2
This is metric tons PER BOX, whence the number of BOX(es) PER metric ton should be
the reciprocal:
boxesPerMetricTon = 1 / metricTonsPerBox;
#include <iostream>
using namespace std;
int main()
{
double ouncesPerBox, metricTonsPerbox,
boxesPerMetricTon;
char ans = 'y';
while( 'y' == ans || 'Y' == ans )
{
cout << “enter the weight in ounces of your”
<< “favorite cereal:”
<<endl; cin >> ouncesPerBox;
metricTonsPerbox =
ouncesPerBox / ouncesPerMetricTon;
2
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 2
boxesPerMetricTon = 1 / metricTonsPerbox;
3
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 2
// ====================
// main function
// ====================
int main()
{
double guess;
double previousguess;
double n;
double r;
// Input number to compute the square root of
cout << "Enter number to compute the square root of." <<
endl; cin >> n;
// Initial guess, although note this doesn’t work for the number
1 previousguess = n;
guess = n /2;
// Repeat until guess is within 1% of the previous guess
while (((previousguess - guess) / previousguess) > 0.01)
{
previousguess = guess;
r = n / guess;
guess = (guess + r) / 2;
}
cout << "The estimate of the square root of " << n << " is "
<< guess <<
endl; return 0;
}
4
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 2
// ====================
// main function
// ====================
int main()
{
double milesPerHour;
double hoursPerMile;
double minutesPerMile;
double secondsPace;
int minutesPace;
// Input miles per hour
cout << "Enter speed in miles per hour:" <<
endl; cin >> milesPerHour;
// Compute inverse, which is hours per
mile hoursPerMile = 1.0 / milesPerHour;
// Convert to minutes per mile which is 60 seconds/hour *
hoursPerMile minutesPerMile = 60 * hoursPerMile;
// Extract minutes by converting to an integer, while
// truncates any value after the decimal point
minutesPace = static_cast<int>(minutesPerMile);
// Seconds is the remaining number of minutes * 60
secondsPace = (minutesPerMile - minutesPace) * 60;
cout << milesPerHour << " miles per hour is a pace of " << minutesPace
<< " minutes and " << secondsPace << " seconds. " <<
endl;
return 0;
}
// ====================
// main function
// ====================
int main()
{
5
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 2
string instructorName;
string yourName;
string food;
int num;
string adjective;
string color;
string animal;
cout << "Welcome to Mad Libs! Enter your name: " << endl;
cin >> yourName;
cout << "Enter your instructor's first or last name." <<
endl; cin >> instructorName;
cout << "Enter a food." << endl;
cin >> food;
cout << "Enter a number between 100 and 120." <<
endl; cin >> num;
cout << "Enter an adjective." << endl;
cin >> adjective;
cout << "Enter a color." << endl;
cin >> color;
cout << "Enter an animal." << endl;
cin >> animal;
cout << endl;
cout << "Dear Instructor " << instructorName << "," <<
endl; cout << endl;
cout << "I am sorry that I am unable to turn in my homework at
this time."
<< endl;
cout << "First, I ate a rotten " << food << " which made me turn "
<< color << " and " << endl;
cout << "extremely ill. I came down with a fever of " << num << "." <<
endl;
cout << "Next, my " << adjective << " pet " << animal << " must have "
<<
"smelled the remains " << endl;
cout << "of the " << food << " on my homework, because he ate it. I am
" <<
"currently " << endl;
cout << "rewriting my homework and hope you will accept it late."
<< endl;
cout << endl;
cout << "Sincerely," << endl;
cout << yourName << endl;
return 0;
}
6
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 2
//
// Re-write a program using the style described in the chapter for
// indentation, add comments, and use appropriately named constants.
//**********************************************************************
*
// File Name: volume.cpp
// Author:
// Email Address:
// Project Number: 2.16
// Description: Computes the volume of a sphere given the radius
// Last Changed: October 6, 2007
#include <iostream>
using namespace std;
int main()
{
const double PI = 3.1415;
double radius, volume;
// Prompt the user to enter a radius
cout << "Enter radius of a sphere." <<
endl; cin >> radius;
// Compute and print the volume
volume = (4.0 / 3.0) * PI * radius * radius *
radius; cout << " The volume is " << volume << endl;
return 0;
}
This solution supposes the lethal dose is directly proportional to the weight of the subject,
hence
7
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 2
weightOfDieter
lethalDoseDieter = lethalDoseMouse *
weightOfMouse
This program accepts weight of a lethal dose for a mouse, the weight of the mouse, and
the weight of the dieter, and calculates the amount of sweetener that will just kill the
dieter, based on the lethal dose for a mouse in the lab. If the student has problems with
grams and pounds, a pound is 454 grams.
It is interesting that the result probably wanted is a safe number of cans, while all the data
can provide is the minimum lethal number! Some students will probably realize this, but
my experience is that most will not. I just weighed a can of diet pop and subtracted the
weight of an empty can. The result is about 350 grams. The label claims 355 ml, which
weighs very nearly 355 grams. To get the lethal number of cans from the number of
grams of sweetener, you need the number of grams of sweetener in a can of pop, and the
concentration of sweetener, which the problem assumes 0.1% , that is a conversion factor
of 0.001.
8
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 2
9
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 2
10
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 2
11
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 2
int main()
{
double oldSalary, salary, monthly, retroactive;
char ans;
cout << "Enter current annual salary." << endl
<< "I'll return new annual salary, monthly ”
<< “salary, and retroactive pay." << endl;
cin >> oldSalary;//old annual salary
salary = oldSalary*(1+INCREASE);//new annual
salary monthly = salary/12;
retroactive = (salary – oldSalary)/2;
cout << "new annual salary " << salary << endl;
cout << "new monthly salary " << monthly << endl;
cout << "retroactive salary due: "
<< retroactive << endl;
return 0;
}
17:50:12:~/AW$ a.out
Enter current annual salary.
100000
I'll return new annual salary, monthly salary, and
retroactive pay.
12
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 2
int main()
{
using std::cout;
using std::cin;
using std::endl;
double oldSalary, salary, monthly, oldMonthly, retroactive;
int numberOfMonths; // number of months to pay retroactive
increase
char ans;
cout << "Enter current annual salary and a number of months\n"
<< "for which you wish to compute retroactive pay.\n"
<< "I'll return new annual salary, monthly "
<< "salary, and retroactive pay." << endl;
cin >> oldSalary;//old annual
salary cin >> numberOfMonths;
salary = oldSalary * (1+INCREASE); //new annual salary
oldMonthly = oldSalary/12;
monthly = salary/12;
retroactive = (monthly - oldMonthly) * numberOfMonths;
// retroactive = (salary - oldSalary)/2; // six months
retroactive pay increase.
cout << "new annual salary " << salary << endl;
13
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 2
Typical run
a) Write a program that, on a weekly basis, accepts hours worked then outputs gross
pay, each withholding amount, and net (take-home) pay.
b) Add 'repeat at user discretion' feature.
14
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Another random document with
no related content on Scribd:
groups. To these four divisions we, however, add temporarily a fifth,
viz. Pupipara. This is included by Brauer in Schizophora, but it
appears to be really an unnatural complex, and had better be kept
separate till it has been entirely reconsidered. These great sections
may be thus summarised:—
Series I. Nemocera.—In this section the habit occurs in no less than five
families, viz.:
Blepharoceridae. Curupira; in the female only; larva aquatic.
Culicidae. Culex, Mosquitoes; in the female only; other genera, with one or
two exceptions, do not suck blood; larvae aquatic.
Chironomidae. Ceratopogon, Midge; in the female only; exceptional even
in the genus, though the habit is said to exist in one or two less
known, allied genera; larval habits not certain; often aquatic; in C.
bipunctatus the larva lives under moist bark.
Psychodidae. Phlebotomus: in the female only (?); quite exceptional in the
family; larva aquatic or in liquid filth.
Simuliidae. Simulium, sand-flies; general in the family (?), which, however,
is a very small one; larva aquatic, food probably mixed vegetable and
animal microscopic organisms.
Series II. Brachycera. Tabanidae. Gad-flies: apparently general in the
females of this family; the habits of the exotic forms but little known; in
the larval state, scarcely at all known; some are aquatic.
Series IV. Cyclorrhapha Schizophora: Stomoxys, Haematobia; both sexes
(?); larvae in dung. [The Tse-tse flies, Glossina, are placed in this family,
though their mode of parturition is that of the next section].
Series V. Pupipara. The habit of blood-sucking is probably common to all the
group and to both sexes. The flies, with one exception, frequent
Vertebrates; in many cases living entirely on their bodies, and
apparently imbibing much blood; the larvae are nourished inside the
flies, not on the imbibed blood, but on a milky secretion from the mother.
Sub-Order Aphaniptera. Fleas. The habit of blood-sucking is common to all
the members and to both sexes. The larvae live on dried animal matter.
Thirty years or more ago the Russian naturalist, Wagner, made the
very remarkable discovery that the larva of a Cecidomyiid produces
young; and it has since been found by Meinert and others that this
kind of paedogenesis occurs in several species of the genera
Miastor and Oligarces. The details are briefly as follows:—A female
fly lays a few, very large, eggs, out of each of which comes a larva,
that does not go on to the perfect state, but produces in its interior
young larvae that, after consuming the interior of the body of the
parent larva, escape by making a hole in the skin, and thereafter
subsist externally in a natural manner. This larval reproduction may
be continued for several generations, through autumn, winter, and
spring till the following summer, when a generation of the larvae
goes on to pupation and the mature, sexually perfect fly appears.
Much discussion has taken place as to the mode of origination of the
larvae; Carus and others thought they were produced from the
rudimental, or immature ovaries of the parent larva. Meinert, who
has made a special study of the subject,[370] finds, however, that this
is not the case; in the reproducing larva of the autumn there is no
ovary at all; in the reproducing larvae of the spring-time rudimentary
ovaries or testes, as the case may be, exist; the young are not,
however, produced from these, but from germs in close connection
with the fat-body. In the larvae that go on to metamorphosis the
ovaries continue their natural development. It would thus appear that
the fat-body has, like the leaf of a Begonia, under certain
circumstances, the power, usually limited to the ovaries, of producing
complete and perfect individuals.
The habits of many of the larvae are very peculiar, owing to their
spinning or exuding a mucus, that reminds one of snail-slime; they
are frequently gregarious, and some of them have likewise, as we
shall subsequently mention, migratory habits. Perris has described
the very curious manner in which Sciophila unimaculata forms its
slimy tracks;[371] it stretches its head to one side, fixes the tip of a
drop of the viscous matter from its mouth to the surface of the
substance over which it is to progress, bends its head under itself so
as to affix the matter to the lower face of its own body; then stretches
its head to the other side and repeats the operation, thus forming a
track on which it glides, or perhaps, as the mucus completely
envelops its body, we should rather call it a tunnel through which the
maggot slips along. According to the description of Hudson[372] the
so-called New Zealand Glow-worm is the larva of Boletophila
luminosa; it forms webs in dark ravines, along which it glides, giving
a considerable amount of light from the peculiarly formed terminal
segment of the body. This larva is figured as consisting of about
twenty segments. The pupa is provided with a very long, curiously-
branched dorsal structure: the fly issuing from the pupa is strongly
luminous, though no use can be discovered for the property either in
it or in the larva. The larva of the Australian Ceroplatus mastersi is
also luminous. Another very exceptional larva is that of Epicypta
scatophora; it is of short, thick form, like Cecidomyiid larvae, and has
a very remarkable structure of the dorsal parts of the body; by
means of this its excrement, which is of a peculiar nature, is spread
out and forms a case for enveloping and sheltering the larva.
Ultimately the larval case is converted into a cocoon for pupation.
This larva is so different from that of other Mycetophilidae, that Perris
was at first unable to believe that the fly he reared really came from
this unusually formed larva. The larva of Mycetobia pallipes (Fig.
221) offers a still more remarkable phenomenon, inasmuch as it is
amphipneustic instead of peripneustic (that is to say, it has a pair of
stigmata at the termination of the body and a pair on the first thoracic
segment instead of the lateral series of pairs we have described as
normal in Mycetophilidae). This larva lives in company with the
amphipneustic larva of Rhyphus, a fly of quite another family, and
the Mycetobia larva so closely resembles that of the Rhyphus, that it
is difficult to distinguish the two. This anomalous larva gives rise, like
the exceptional larva of Epicypta, to an ordinary Mycetophilid fly.[373]
But the most remarkable of all the Mycetophilid larvae are those of
certain species of Sciara, that migrate in columns, called by the
Germans, Heerwurm. The larva of Sciara militaris lives under layers
of decomposing leaves in forests, and under certain circumstances,
migrates, sometimes perhaps in search of a fresh supply of food,
though in some cases it is said this cannot be the reason. Millions of
the larvae accumulate and form themselves by the aid of their
viscous mucus into great strings or ribbons, and then glide along like
serpents: these aggregates are said to be sometimes forty to a
hundred feet long, five or six inches wide, and an inch in depth. It is
said that if the two ends of one of these processions be brought into
contact, they become joined, and the monstrous ring may writhe for
many hours before it can again disengage itself and assume a
columnar form. These processional maggots are met with in
Northern Europe and the United States, and there is now an
extensive literature about them.[374] Though they sometimes consist
of almost incredible numbers of individuals, yet it appears that in the
Carpathian mountains the assemblages are usually much smaller,
being from four to twenty inches long. A species of Sciara is the
"Yellow-fever fly" of the Southern United States. It appears that it has
several times appeared in unusual numbers and in unwonted
localities at the same time as the dreaded disease, with which it is
popularly supposed to have some connection.
There are more than 1000 species of these flies known, and many
genera. They form three sub-families, which are by some considered
distinct families, viz.: Ptychopterinae, Limnobiinae or Tipulidae
Brevipalpi, Tipulinae or Tipulidae Longipalpi.