Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual Download
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual Download
Exam Practice!
True/False Problems
1. T
2. F
3. T
4. T
5. F
6. T
7. T
Multiple Choice Problems
8. (b)
9. (a)
10. (d)
Program Analysis
11. 1
12. 0
13. 0
14. Results using negative integers may not be as expected.
Memory Snapshot Problems
15. v1-> [double x->1 double y->1 double orientation->3.1415]
v2-> [double x->1 double y->1 double orientation->3.1415]
16. v1-> [double x->0.0 double y->0.0 double orientation->0.0]
v2-> [double x->1 double y->1 double orientation->3.1415]
17. v1-> [double x->2.1 double y->3.0 double orientation->1.6]
v2-> [double x->2.1 double y->3.0 double orientation->1.6]
Programming Problems
/*--------------------------------------------------------------------*/
/* Problem chapter6_18 */
/* */
/* This program calls a function that detects and prints the first */
#include <iostream>
using namespace std;
int main()
{
/* Declare and initialize variables. */
int input;
return 0;
}
/*--------------------------------------------------------------------*/
/* Function to calculate prime numbers. */
void primeGen(int n){
/* Declare variables. */
int i,j;
bool prime;
for (i=1;i<=n;i++){
prime=true;
for (j=2;j<i;j++){
if (!(i%j)) {
prime=false;
}
}
/* Output number if it is prime. */
if (prime)
cout << i << endl;
}
return;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter6_19 */
/* */
/* This program calls a function that detects and prints the first */
/* n prime integers, where n is an integer input to the function. */
/* The values are printed to a designated output file. */
#include <iostream>
#include <fstream>
int main()
{
/* Declare and initialize variables */
int input;
ofstream outfile;
string filename;
outfile.open(filename.c_str());
if (outfile.fail()) {
cerr << "The output file " << filename << " failed to open.\n";
exit(1);
}
outfile.close();
return 0;
}
/*--------------------------------------------------------------------*/
/* Function to calculate prime numbers. */
void primeGen(int n, ofstream& out){
/* Declare variables. */
int i,j;
bool prime;
for (i=1;i<=n;i++){
prime=true;
for (j=2;j<i;j++){
if (!(i%j)) {
prime=false;
}
}
/* Output number if it is prime. */
if (prime)
out << i << endl;
}
/*--------------------------------------------------------------------*/
/* Problem chapter6_20 */
/* */
/* This program calls a function that counts the integers in a file */
/* until a non-integer value is found. An error message is printed */
/* if non-integer values are found. */
#include <iostream>
#include <fstream>
#include <string>
int main()
{
/* Declare and initialize variables */
ifstream infile;
string filename;
int numInts;
infile.open(filename.c_str());
if (infile.fail()) {
cerr << "The input file " << filename << " failed to open.\n";
exit(1);
}
infile.close();
return 0;
}
/*--------------------------------------------------------------------*/
/* Function to count integers. */
int countInts(ifstream& in){
in >> num;
while (!in.eof()) {
if (!in) {
return count;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter6_21 */
/* */
/* This program calls a function that prints the values of the state */
/* flags of a file stream, which is passed to the function as an */
/* argument. */
#include <iostream>
#include <fstream>
#include <string>
int main()
{
/* Declare and initialize variables */
ifstream infile;
string filename;
infile.open(filename.c_str());
if (infile.fail()) {
cerr << "The input file " << filename << " failed to open.\n";
exit(1);
}
infile.close();
return 0;
}
/*--------------------------------------------------------------------*/
/* Function to count integers. */
void printFlags(ifstream& in){
return;
}
/*--------------------------------------------------------------------*/
Simple Simulations
/*--------------------------------------------------------------------*/
/* Problem chapter6_22 */
/* */
/* This program simulates tossing a "fair" coin. */
/* The user enters the number of tosses. */
#include <iostream>
#include <cstdlib>
int main()
{
/* Declare and initialize variables */
/* and declare function prototypes. */
int tosses=0, heads=0, required=0;
/* Print results. */
cout << "\n\nNumber of tosses: " << tosses << endl;
cout << "Number of heads: "<< heads << endl;
cout << "Number of tails: " << tosses-heads << endl;
cout << "Percentage of heads: " << 100.0 * heads/tosses << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/* (rand_int function from page 257) */
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter6_23 */
/* */
/* This program simulates tossing an "unfair" coin. */
/* The user enters the number of tosses. */
#include <iostream>
#include <cstdlib>
int main()
{
/* Declare variables and function prototypes. */
int tosses=0, heads=0, required=0;
int rand_int(int a, int b);
/* Print results. */
cout << "\n\nNumber of tosses: " << tosses << endl;
cout << "Number of heads: " << heads << endl;
cout << "Number of tails: " << tosses-heads << endl;
cout << "Percentage of heads: " << 100.0 * heads/tosses << endl;
cout << "Percentage of tails: " << 100.0 * (tosses-heads)/tosses <<
endl;
/* Exit program. */
return 0;
}
/*------------------------------------------------------------------*/
/* (rand_int function from page 257) */
/*------------------------------------------------------------------*/
#include <iostream>
#include <cstdlib>
#include "Coin.h"
using namespace std;
int main()
{
/* Declare and initialize variables */
/* and declare function prototypes. */
int tosses=0, heads=0, required=0;
int seed;
/* Prompt user for number of tosses. */
cout << "\n\nEnter number of fair coin tosses: ";
cin >> required;
while (required <= 0)
{
cout << "Tosses must be an integer number, greater than zero.\n\n";
cout << "Enter number of fair coin tosses: ";
cin >> required;
}
/* Print results. */
cout << "\n\nNumber of tosses: " << tosses << endl;
cout << "Number of heads: "<< heads << endl;
cout << "Number of tails: " << tosses-heads << endl;
cout << "Percentage of heads: " << 100.0 * heads/tosses << endl;
cout << "Percentage of tails: " << 100.0 * (tosses-heads)/tosses << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*---------------------------------------------------*
/* Definition of a Coin class */
/* Coin.h
#include <iostream>
//Constructors
Coin() {int randomInt = rand_int(0,1);
if(randomInt == 1)
faceValue = HEADS;
else
faceValue = TAILS;}
//Accessors
char getFaceValue() const {return faceValue;}
//Mutators
//Coins are not mutable
};
/*----------------------------------------------------*/
/* This function generates a random integer */
/* between specified limits a and b (a<b). */
int rand_int(int a, int b)
{
return rand()%(b-a+1) + a;
}
/*----------------------------------------------------*/
/*------------------------------------------------------------------*/
/* Problem chapter6_25 */
/* */
/* This program simulates rolling a six-sided "fair" die. */
/* The user enter the number of rolls. */
#include <iostream>
#include <cstdlib>
int main()
{
/* Declare variables and function prototypes. */
int onedot=0, twodots=0, threedots=0, fourdots=0, fivedots=0,
sixdots=0, rolls=0, required=0;
/*------------------------------------------------------------------*/
/* Problem chapter6_26 */
/* */
/* This program simulates an experiment rolling two six-sided */
/* "fair" dice. The user enters the number of rolls. */
#include <iostream>
#include <cstdlib>
int main()
{
/* Declare variables. */
int rolls=0, die1, die2, required=0, sum=0;
/* Exit program. */
return 0;
}
/*------------------------------------------------------------------*/
/* (rand_int function from text) */
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
/* Problem chapter6_27 */
/* */
/* This program simulates a lottery drawing that uses balls */
/* numbered from 1 to 10. */
#include <iostream>
#include <cstdlib>
/* Get three lottery balls, check for even or odd, and NUMBER. */
/* Also check for the 1-2-3 sequence and its permutations. */
while (lotteries < required)
{
lotteries++;
cout << "Lottery number is: " << first << "-" << second
<< "-" << third << endl;
/* Print results. */
cout << "\nPercentage of time the result contains three even numbers:"
<< 100.0*alleven/lotteries << endl;
cout << "Percentage of time the number " << NUMBER << " occurs in the"
" three numbers: " << 100.0*num_in_sim/lotteries << endl;
cout << "Percentage of time the numbers 1,2,3 occur (not necessarily"
" in order): " << 100.0*onetwothree/lotteries << endl;
/* Exit program. */
}
/*--------------------------------------------------------------------*/
/* (rand_int function from page 257) */
/*--------------------------------------------------------------------*/
Component Reliability
/*--------------------------------------------------------------------*/
/* Problem chapter6_28 */
/* */
/* This program simulates the design in Figure 6-17 using a */
/* component reliability of 0.8 for component 1, 0.85 for */
/* component 2 and 0.95 for component 3. The estimate of the */
/* reliability is computed using 5000 simulations. */
/* (The analytical reliability of this system is 0.794.) */
#include <iostream>
#include <cstdlib>
/* Run simulations. */
for (num_sim=0; num_sim<SIMULATIONS; num_sim++)
{
/* Get the random numbers */
est1 = rand_float(MIN_REL,MAX_REL);
est2 = rand_float(MIN_REL,MAX_REL);
est3 = rand_float(MIN_REL,MAX_REL);
/* Print results. */
cout << "Simulation Reliability for " << num_sim << " trials: "
<< (double)success/num_sim << endl;
/* Exit program. */
return 0;
}
/*-------------------------------------------------------------------*/
/* (rand_float function from page 257) */
/*--------------------------------------------------------------------*/
/* Problem chapter6_29 */
/* */
/* This program simulates the design in Figure 6.18 using a */
/* component reliability of 0.8 for components 1 and 2, */
/* and 0.95 for components 3 and 4. Print the estimate of the */
/* reliability using 5000 simulations. */
/* (The analytical reliability of this system is 0.9649.) */
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
/* Define variables. */
int num_sim=0, success=0;
double est1, est2, est3, est4;
/* Run simulations. */
for (num_sim=0; num_sim<SIMULATIONS; num_sim++)
{
/* Get the random numbers. */
est1 = rand_float(MIN_REL,MAX_REL);
est2 = rand_float(MIN_REL,MAX_REL);
est3 = rand_float(MIN_REL,MAX_REL);
est4 = rand_float(MIN_REL,MAX_REL);
/* Print results. */
cout << "Simulation Reliability for " << num_sim << " trials: "
<< (double)success/num_sim << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/* (rand_float function from page 257) */
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
#include <iostream>
#include <cstdlib>
int main()
{
/* Define variables. */
int num_sim=0, success=0;
double est1, est2, est3, est4;
/* Run simulations. */
for (num_sim=0; num_sim<SIMULATIONS; num_sim++)
{
/* Get the random numbers. */
est1 = rand_float(MIN_REL,MAX_REL);
est2 = rand_float(MIN_REL,MAX_REL);
est3 = rand_float(MIN_REL,MAX_REL);
est4 = rand_float(MIN_REL,MAX_REL);
/* Print results. */
cout << "Simulation Reliability for " << num_sim << " trials: "
<< (double)success/num_sim << endl;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/* (rand_float function from page 257) */
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter6_31 */
/* */
/* This program generates a data file named wind.dat that */
/* contains one hour of simulated wind speeds. */
int main()
{
/* Define variables. */
int timer=START_TIME;
double ave_wind=0.0, gust_min=0.0, gust_max=0.0, windspeed=0.0;
ofstream wind_data;
/*--------------------------------------------------------------------*/
/* Problem chapter6_32 */
/* */
/* This program generates flight simulator wind data with a */
/* 0.5% possiblity of encountering a small storm at each time step. */
/* The average wind speed is increased by 10 mph for 5 minutes when */
/* a storm is encountered. */
#include <iostream>
#include <fstream>
#include <cstdlib>
int main()
{
/* Define variables. */
double ave_wind=0.0, gust_min=0.0, gust_max=0.0, windspeed = 0.0;
int timer=START_TIME, storm_duration=0, storm_flag=NO_STORM;
ofstream wind_data;
windspeed = rand_float(ave_wind+gust_min,ave_wind+gust_max);
if (storm_flag == STORM)
windspeed += STORM_WIND;
/*--------------------------------------------------------------------*/
/* Problem chapter6_33 */
/* */
/* This program generates flight simulator wind data with a */
/* 0.5% possiblity of encountering a small storm at each time step. */
/* The average wind speed is increased by 10 mph for 5 minutes when */
/* a storm is encountered. During a small storm, there is a 1% */
/* chance of encountering a microbust at each time step. The wind */
/* is increased by 50mph for 1 minute when a microburst occurs. */
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
int main()
{
/* Define variables. */
double ave_wind=0.0, gust_min=0.0, gust_max=0.0, windspeed = 0.0;
int timer=START_TIME, storm_duration=0, burst_duration=0;
bool storm=false, burst=false;
ofstream wind_data;
windspeed = rand_float(ave_wind+gust_min,ave_wind+gust_max);
if (storm){
windspeed += STORM_WIND;
if (burst)
windspeed += BURST_WIND;
}
wind_data << timer << ' ' << windspeed << endl;
}
/*--------------------------------------------------------------------*/
/* Problem chapter6_34 */
/* */
/* This program generates flight simulator wind data with a */
/* user-entered possiblity of encountering a small storm at each */
/* time step. The average wind speed is increased by 10 mph for 5 */
/* minutes when a storm is encountered. */
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
int main()
{
/* Define variables. */
double ave_wind=0.0, gust_min=0.0, gust_max=0.0, windspeed = 0.0;
double storm_prob;
int timer=START_TIME, storm_duration=0, storm_flag=NO_STORM;
ofstream wind_data;
windspeed = rand_float(ave_wind+gust_min,ave_wind+gust_max);
if (storm_flag == STORM)
windspeed += STORM_WIND;
wind_data << timer << ' ' << windspeed << endl;
}
/*--------------------------------------------------------------------*/
/* Problem chapter6_35 */
/* */
/* This program generates flight simulator wind data with a */
/* 0.5% possiblity of encountering a small storm at each time step. */
/* The average wind speed is increased by 10 mph for a user-entered */
/* duration when a storm is encountered. */
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
int main()
{
/* Define variables. */
double ave_wind=0.0, gust_min=0.0, gust_max=0.0, windspeed = 0.0;
int max_storm_length = 300;
int timer=START_TIME, storm_duration=0, storm_flag=NO_STORM;
ofstream wind_data;
windspeed = rand_float(ave_wind+gust_min,ave_wind+gust_max);
if (storm_flag == STORM)
windspeed += STORM_WIND;
wind_data << timer << ' ' << windspeed << endl;
}
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
int main()
{
/* Define variables. */
double ave_wind=0.0, gust_min=0.0, gust_max=0.0, windspeed = 0.0;
int max_storm_length;
int timer=START_TIME, storm_duration=0, storm_flag=NO_STORM;
ofstream wind_data;
windspeed = rand_float(ave_wind+gust_min,ave_wind+gust_max);
if (storm_flag == STORM)
windspeed += STORM_WIND;
wind_data << timer << ' ' << windspeed << endl;
}
#include <iostream>
#include <cmath>
int main()
{
/* Declare variables. */
double a, b, c, discriminant, root1,root2;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter6_38 */
/* */
/* This program determines the roots of a quadratic equation. */
#include <iostream>
#include <cmath>
int main()
{
/* Declare variables. */
double a, b, c, discriminant, root1, root2;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter6_39 */
/* */
/* This program evaluates this mathematical function: */
/* f(x) = 0.1 x^2 - x ln x */
int main()
{
/* Declare variables. */
int n, k;
double a, b, step, left, right;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/* This function checks a subinterval for a root. */
double f(double x)
{
/* Return function value. */
return (0.1*x*x - x*log(x));
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter6_40 */
/* */
/* This program finds the roots of this function in a */
/* user-specified interval: f(x) = sinc(x) */
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
/* Declare variables. */
int n, k;
double a, b, step, left, right;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/* This function checks a subinterval for a root. */
/* Void return. */
return;
}
/*--------------------------------------------------------------------*/
/* This function evaluates a sinc function. */
double sinc(double x)
{
/* Return function value. */
if (x == 0)
return 1;
else
return sin(x)/x;
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Problem chapter6_41 */
/* */
/* This program estimates the roots of a subinterval using this */
/* approximation: c = (a*f(b) - b*f(b)) / (f(b) - f(a)) */
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
/* Declare variables. */
int n, k;
double a0, a1, a2, a3, a, b, step, left, right;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/* This function checks a subinterval for a root. */
/* Void return. */
return;
}
/*--------------------------------------------------------------------*/
/* (poly function from page 231) */
/*--------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/* Program chapter6_42 */
/* */
/* This program finds the real roots of a polynomial using the */
/* Newton-Raphson method using functions that evaluate the polynomial */
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
// Declare objects.
int iterations=0;
double a0, a1, a2, a3, x, p, dp, tol;
// Determine tolerance.
tol = abs(p);
while(tol > 0.001 && iterations < 100)
{
// Calculate the derivative.
dp = derivative(a0, a1, a2, a0, x);
/*-------------------------------------------------------------------------*/
/* Program chapter6_43 */
/* */
/* This program estimates the area under a given curve */
/* using trapezoids with equal bases. */
#include <iostream>
#include <cmath>
// Function prototypes.
double integrate(double a, double b, int n);
double f(double x);
int main()
{
// Declare objects
int num_trapezoids;
double a, b, area;
// Print result.
cout << "Using " << num_trapezoids << " trapezoids, the estimated "
<< area is " << area << endl;
return 0;
}
/*------------------------------------------------------------------------*/
double integrate(double a, double b, int n)
{
// Declare objects.
double sum = 0, x, base, area;
base = (b-a)/n;
for(int k=2; k<=n; k++)
{
x = a + base*(k-1);
sum = sum + f(x);
}
area = 0.5*base*(f(a) + 2*sum + f(b));
return area;
}
/*------------------------------------------------------------------------*/
/* Program chapter6_44 */
/* */
/* This program estimates the area under a given curve */
/* using trapezoids with equal bases. The x- and y-coordinates of the */
/* trapezoid endpoints are printed to a data file called */
/* plotTrapezoids.txt so they can be plotted later. */
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
// Function prototypes.
double integrate(double a, double b, int n);
double f(double x);
int main()
{
// Declare objects
int num_trapezoids;
double a, b, area;
// Print result.
cout << "Using " << num_trapezoids << " trapezoids, the estimated "
<< "area is " << area << endl;
return 0;
}
/*-----------------------------------------------------------------------*/
double integrate(double a, double b, int n)
{
// Declare objects.
double sum = 0, x, base, area;
ofstream out;
out.close();
return area;
}
/*-----------------------------------------------------------------------*/
double f(double x)
{
return(4*exp(-x));
}
/*-----------------------------------------------------------------------*/
/*-------------------------------------------------------*/
/* Problem chapter6_45 */
/* */
/* This program estimates the area under a curve */
/* using trapezoids. The curve is defined by a set of */
/* experimental data. The spacing of the independent */
/* variable across the interval is not uniform. */
#include<iostream>
#include<fstream>
//Function prototype.
double integrate(istream&);
int main()
{
ifstream fin;
string filename;
double area;
/*--------------------------------------------------------------------*/
/* Function chapter6_46 */
/* */
/* This function calculates the number of permutations of n things, */
/* taken k at a time. */
/*--------------------------------------------------------------------*/
/* Function chapter6_47 */
/* */
/* This function calculates the number of combinations of n things, */
/* taken k at a time using a factorial function from problem 20. */
/*--------------------------------------------------------------------*/
/* Problem chapter6_48 */
/* */
/* This program compares the cosine of an angle using the library */
/* function with the value computed from the first five terms */
/* of this series: cos x = 1 - x^2/2! + x^4/4! ..... */
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
/* Define variables. */
double x;
/* Exit program. */
return 0;
}
/*-------------------------------------------------------------------*/
/* This function calculates the series for cosine */
/* for a specified number of terms. */
double s_cosine(double x)
{
/* Declare variables. */
int i;
double sum=1;
/*--------------------------------------------------------------------*/
/* Problem chapter5_49 */
/* */
/* This program compares the cosine of an angle using the library */
/* function with the value computed from terms > 0.0001 */
/* of this series: cos x = 1 - x^2/2! + x^4/4! ..... */
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
/* Define variables. */
double x;
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
/* This function caluclates the series for cosine using terms */
/* that are greater than a specified value. */
double s_cosine(double x)
{
/* Declare variables. */
int i=0;
double sum=0, term=1;