100% found this document useful (72 votes)
843 views33 pages

Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual 1

This document provides a summary of exam practice problems related to the textbook "Engineering Problem Solving With C++ 4th Edition". It includes true/false questions, multiple choice questions, memory snapshot problems, and programming exercises from chapters 5 and 6 of the textbook. Links are provided to download the solution manual and test bank for the textbook.

Uploaded by

elaine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (72 votes)
843 views33 pages

Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual 1

This document provides a summary of exam practice problems related to the textbook "Engineering Problem Solving With C++ 4th Edition". It includes true/false questions, multiple choice questions, memory snapshot problems, and programming exercises from chapters 5 and 6 of the textbook. Links are provided to download the solution manual and test bank for the textbook.

Uploaded by

elaine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Engineering Problem Solving With C++ 4th

Edition by Etter Ingber ISBN 0134444299


9780134444291
Download solution manual at:
https://testbankpack.com/p/solution-manual-for-engineering-
problem-solving-with-c-4th-edition-by-etter-ingber-isbn-
0134444299-9780134444291/

Download full test bank at :


https://testbankpack.com/p/test-bank-for-engineering-problem-
solving-with-c-4th-edition-by-etter-ingber-isbn-0134444299-
9780134444291/

Exam Practice!
True/False Problems
1. F
2. F
3. F
4. F
5. F
Multiple-Choice Problems
6. (b)and(c)
7. (c)
Memory Snapshot Problems
8. int i->0 int j->0
double x->1.0 double y->5.2
char ch1->a char ch2->,
9. int i->5 int j->0
double x->1.0 double y->?
char ch1->? char ch2->?
10. int i->1 j->5
double x->? double y->?
char ch1->. char ch2->2
11. int i->1 int j->5
double x->0.2 double y->?
char ch1->a char ch2->,
Programming Exercises
Data Filters
/*--------------------------------------------------------------------*/
/* Problem chapter5_12 */
/* */
/* This program reads a data file that should contain only integer */
/* values, and thus should contain only digits, plus or minus */

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


/* signs, and white space. The program prints invalid characters */
/* and a count of any invalid characters located in the file. */

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

using namespace std;

const char NEWLINE = '\n';


const char TAB = '\t';

int main()
{
// Declare variables.
int counter=0;
char c;
string filename;
ifstream file1;

// Prompt user for file name and open input file.


cout << "Enter the name of the input file:";
cin >> filename;
file1.open(filename.c_str());

if(file1.fail())
{
cerr << "error opening file " << filename;
exit(1);
}

// Check file characters to see if legal.


// If not legal, count it.
c = file1.get();
while (!file1.eof())
{
switch (c)
{
case '+':
break;
case '-':
break;
case NEWLINE:
break;
case TAB:
break;
case ' ':
break;
case EOF:
break;
default:
if (!(isdigit(c))) {
counter++;
cout << c << " ";
}
break;

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


}//end switch
c = file1.get();
} //end while

// Print results.
cout << endl << "Number of invalid characters is: " << counter << endl;

// Close file and exit program.


file1.close();
return 0;
}
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_13 */
/* */
/* This program reads a file that contains only integers and white */
/* space. The program prints the number of lines and the number of */
/* integer values. */

#include <iostream>
#include <fstream>
#include <cctype>
#include <string>

using namespace std;

const char NEWLINE='\n';


const char TAB='\t';

int main()
{
// Declare variables.
char c;
int i, linenum(0), integers(0);
ifstream oldfile;
string inname;

// Prompt user for file name and open input file


cout << "Enter name of input file: ";
cin >> inname;
oldfile.open(inname.c_str());
if(oldfile.fail())
{
cerr << "error opening file " << inname << endl;
exit(1);
}

//look for spaces and newlines


c = oldfile.get();
while(!oldfile.eof())
{
switch (c) {
case '\n':
linenum++;
break;
default:

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


break;
}
c = oldfile.get();
}
oldfile.close();
oldfile.open(inname.c_str());

oldfile >> i;
while (!oldfile.eof()) {
integers++;
oldfile >> i;
}

//Print out values.


cout << "There are " << linenum << " lines in the file." << endl;
cout << "There are " << integers << " integers in the file." << endl;

/* Close file and exit program. */


oldfile.close();
return 0;
}
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_14 */
/* *
/* This program reads a file that contains only integers, but */
/* some of the integers have embedded commas, as in 145,020. The */
/* program copies the information to a new file, removing any */
/* commas from the information. */

#include <iostream>
#include <fstream>
#include <cctype>
#include <string>

using namespace std;

const char NEWLINE='\n';


const char TAB='\t';

int main()
{
// Declare variables.
char c;
ofstream newfile;
ifstream oldfile;
string inname, outname;

// Prompt user for file names and


// Open the files for reading and writing respectively.
cout << "Enter name of input file: ";
cin >> inname;
oldfile.open(inname.c_str());
if(oldfile.fail())
{
cerr << "error opening file " << inname << endl;

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


exit(1);
}
outname = "new" + inname;
newfile.open(outname.c_str());

// Look for commas, and write the data to the new file.
c = oldfile.get();
while(!oldfile.eof())
{
if ( c != ',')
newfile << c;
c = oldfile.get();
}

/* Close files and exit program. */


newfile.close();
oldfile.close();
return 0;
}
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_15 */
/* */
/* This program reads a file containing integer and floating-point */
/* values seperated by commas, which may or may not be followed */
/* by additional white space. A new file is generated that contains */
/* the integers and floating-point values seperated only by a */
/* single space between the values. */

#include <iostream>
#include <fstream>
#include <cctype>
#include <string>

using namespace std;

const char NEWLINE = '\n';


const char TAB = '\t';

int main()
{
// Declare variables.
char c;
string inname, outname;
ofstream newfile;
ifstream oldfile;

// Prompt user for file names.


// Open the files for reading and writing respectively.
cout << "enter name of input file ";
cin >> inname;
oldfile.open(inname.c_str());
if(oldfile.fail())
{
cerr << "error opening file " << inname << endl;
exit(1);

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


}
outname = "new" + inname;
newfile.open(outname.c_str());

// Put only a single space between numerical values.


oldfile.get(c);
while(!oldfile.eof())
{
switch (c)
{
case TAB:
case ' ':
/* Ignore white space. */
break;
case ',':
/* Replace comma with a single space. */
newfile << ' ';
break;
default:
/* Either a digit or a new line or EOF. */
newfile << c;
break;
}
oldfile >> c;
} // end while

/* Close files and exit program. */


newfile.close();
oldfile.close();
return 0;
}
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_16 */
/* */
/* This program reads a file containing data values computed by */
/* an accounting software package. While the file contains only */
/* numerical information, the values may contain embedded commas */
/* and dollar signs. The program generates a new file that */
/* contains the values with the commas and dollar signs removed, */
/* and with a leading minus sign instead of parantheses. */

#include <iostream>
#include <fstream>
#include <cctype>
#include <string>

using namespace std;

const char NEWLINE = '\n';


const char TAB = '\t';

int main()
{
/* Declare variables. */
char c;

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


string inname, outname;
ifstream oldfile;
ofstream newfile;

// Prompt user for file name.


cout << "enter name of input file ";
cin >> inname;

// Open the files for reading and writing respectively.


oldfile.open(inname.c_str());
if(oldfile.fail())
{
cerr << "error opening file " << inname << endl;
exit(1);
}
outname = "new" + inname;
newfile.open(outname.c_str());

/* Now change the format. */


oldfile.get(c);
while (!oldfile.eof())
{
switch (c)
{
case ')':
case '$':
case ',':
break;
case '(':
c = '-';
/* fall thru */
default:
newfile << c;
break;
}
oldfile.get(c);
}

/* Close files and exit program. */


newfile.close();
oldfile.close();
return 0;
}
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_17 */
/* */
/* This program compares two files. The program should print a */
/* message indicating that the files are exactly the same, or that */
/* there are differences. If the fields are different, the program */
/* should print the line numbers for lines that are not the same. */

#include <iostream>
#include <fstream>
#include <cctype>
#include <string>

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


using namespace std;

const string AFILE = "decoded.dat";


const string BFILE = "in.dat";
const char NEWLINE = '\n';
const char TAB = '\t';

int main()
{
/* Declare and initialize variables. */
char a,b;
int line=1, diff=0, line_flag=0;
ifstream afile, bfile;

/* Open input files. */


afile.open(AFILE.c_str());
if (afile.fail()){
cerr << AFILE << " failed to open.\n";
exit(1);
}
bfile.open(BFILE.c_str());
if (bfile.fail()){
cerr << BFILE << " failed to open.\n";
exit(1);
}

/* Now compare the values. */


do
{
/* Get the character from each file. */
afile.get(a);
bfile.get(b);

/* If they are not the same, increment a count of differences */


/* and note that there is a difference on this line. */
if (a != b)
{
diff++;
line_flag = 1;
}

/* If we are the end of a line in the a-file */


/* print a message if there was an error on this line */
/* and reset the flag indicating an error on the line. */
if (a == NEWLINE)
{
if (line_flag)
{
cout << "Files differ on line " << line << endl;
line_flag = 0;
}

/* The next character will be on the next line */


line++;
}
} while ((!afile.eof()) && (!bfile.eof()));

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


/* Check that files are the same length. */
if ((!afile.eof() ) || (!bfile.eof()))
cout << "Files differ in length\n";
else
/* Check to see that there are no differences. */
if (diff == 0)
cout << "Files are the same\n";

/* Close files and exit prgram. */


afile.close();
bfile.close();
return 0;
}
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_18 */
/* */
/* This program reads the text in a file, then generates a new */
/* file that contains the coded text in which characters are */
/* replaced by characters using a "shift-by-two" scheme. Do not */
/* change the non-alphanumeric, newline, or EOF characters. */

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cctype>

using namespace std;

const char NEWLINE = '\n';

int main()
{
/* Declare variables. */
int code;
char oldchar;
ifstream original;
ofstream encrypted;

/* Open files for reading and writing respectively. */


original.open("in.dat");
if (original.fail()) {
cerr << "Input file will not open.\n";
exit(1);
}
encrypted.open("out.dat");
if (encrypted.fail()) {
cerr << "Output file will not open.\n";
exit(1);
}

/* Get first character from the file */


original.get(oldchar);

/* Generate coded message. */

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


while (!original.eof())
{
if (!isalpha(oldchar)&&!isdigit(oldchar)) {
encrypted << oldchar;
} else {
switch (oldchar)
{
case NEWLINE:
encrypted << oldchar;
break;
default:
code = (int) oldchar + 2;
encrypted << (char)code;
break;
}
}
/* Retrieve next character from the file */
original.get(oldchar);

/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_19 */
/* */
/* This program reads the text in a file that has been encoded by */
/* a scheme in which characters are replaced by characters two */
/* characters to the right in the collating sequence. */

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cctype>

using namespace std;

const char NEWLINE = '\n';

int main()
{
/* Declare variables. */
int decode;
char oldchar;
ofstream decoded;
ifstream encrypted;

/* Open files for reading and writing respectively. */


decoded.open("decoded.dat");
if (decoded.fail()) {
cerr << "Output file will not open.\n";
exit(1);
}
encrypted.open("out.dat");

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


if (encrypted.fail()) {
cerr << "Input file will not open.\n";
exit(1);
}

/* Get first character from the file */


encrypted.get(oldchar);

/* Generate decoded message. */


while( !encrypted.eof() )
{
if (!isalpha(oldchar)&&!isdigit(oldchar)) {
decoded << oldchar;
} else {
switch (oldchar)
{
case NEWLINE:
decoded << oldchar;
break;
default:
decode = (int) oldchar - 2;
decoded << (char)decode;
break;
}
}
/* Get next character from the file */
encrypted.get(oldchar);
}

/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
Sounding Rocket Trajectory
/*--------------------------------------------------------------------*/
/* Problem chapter5_20 */
/* */
/* This program assumes that the file rocket1.dat contains an */
/* initial line that contains the number of actual datalines */
/* that follows. The program reads these data and determines */
/* the time at which the rocket begins falling back to earth. */
/* Data format is: time, altitude, velocity, accelearation */

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const string FILENAME = "rocket1.dat";

int main()
{
/* Declare variables. */
int number_of_items;
double acceleration, altitude=0, previous_altitude=0.0,
previous_time, time=0, velocity;

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


ifstream rocket1;

/* Open input file. */


rocket1.open(FILENAME.c_str());
if (rocket1.fail()){
cerr << FILENAME << " will not open.\n";
exit(1);
}

if (!(rocket1 >> number_of_items) || number_of_items == 0)


{
cout << "No valid data in file " << FILENAME << endl;
exit(1);
}

while (altitude>=previous_altitude &&!rocket1.eof())


{
previous_altitude = altitude;
previous_time = time;
rocket1 >> time >> altitude >> velocity >> acceleration;

/* Print time at which rocket begins to fall. */


if (altitude < previous_altitude)
cout << "Time at which the rocket begins to fall is between "
<< previous_time << " seconds and " << time << " seconds \n";
else
cout << "No decrease in altitude detected in data file. \n";

/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_21 */
/* */
/* This program reads velocity data from a data file and */
/* determines the number of stages in the rocket. The file */
/* contains a trailer line with -99 for all values. */

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int INCREASE = 1;


const int DECREASE = -1;
const string FILENAME = "rocket2.dat";

int main()
{
/* Declare and initialize variables. */
int dir=INCREASE, stages=1;
double time, altitude, acceleration, velocity=0.0, prev_vel=0;
ifstream rocket;

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


/* Open input file. */
rocket.open(FILENAME.c_str());

/* Read first set of data. */


rocket >> time >> altitude >> velocity >> acceleration;

prev_vel = velocity;

/* Keep looking for the trailer while processing all the data */
while (time > 0)
{
/* Check for a change in direction. */
if ((prev_vel>velocity) && (dir==INCREASE))
dir = DECREASE;
else
/* New stage fired if velocity is increasing again. */
if ((prev_vel<velocity) && (dir==DECREASE))
{
stages++;
dir = INCREASE;
}

/* Save values for comparison next time through loop */


prev_vel=velocity;

/* Read next data set */


rocket >> time >> altitude >> velocity >> acceleration;

/* Print results */
cout << "The number of stages was: " << stages << endl;
return 0;
}
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_22 */
/* */
/* This program reads velocity data from a data file. It */
/* determines the number of stages in the rocket and prints the */
/* times that correspond to the firing of each stage (i.e, when the */
/* velocity begins to increase. The data file contains a trailer */
/* line with -99 for all values. */

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int INCREASE = 1;


const int DECREASE = -1;
const string FILENAME = "rocket2.dat";

int main()
{

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


/* Declare and initialize variables. */
int dir=INCREASE, stages=1;
double time, altitude, acceleration, velocity=0.0, prev_vel=0;
ifstream rocket;

/* Open input file. */


rocket.open(FILENAME.c_str());

/* Read first set of data. */


rocket >> time >> altitude >> velocity >> acceleration;

prev_vel = velocity;

/* Keep looking for the trailer while processing all the data */
while (time > 0)
{
/* Check for a change in direction. */
if ((prev_vel>velocity) && (dir==INCREASE))
dir = DECREASE;
else
/* New stage fired if velocity is increasing again. */
if ((prev_vel<velocity) && (dir==DECREASE))
{
stages++;
dir = INCREASE;
cout << "Stage fired at " << time << endl;
}

/* Save values for comparison next time through loop */


prev_vel=velocity;

/* Read next data set */


rocket >> time >> altitude >> velocity >> acceleration;

/* Print results */
cout << "The number of stages was: " << stages << endl;
return 0;
}
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_23 */
/* */
/* This program reads velocity data from a data file and */
/* determines the times during which the acceleration is due */
/* only to gravity. The file does not contain a header or */
/* trailer line. */

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

const double MAX_GRAVITY = -9.31; /* within -5% of gravity */

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


const double MIN_GRAVITY = -10.29; /* within 5% of gravity */
const double GRAVITY = -9.8;
const string FILENAME = "rocket3.dat";

int main()
{
/* Declare variables. */
double time, altitude, acceleration, velocity;
ifstream rocket;

/* Open input file for reading. */


rocket.open(FILENAME.c_str());
if (rocket.fail()){
cerr << FILENAME << " failed to open.\n";
exit(1);
}

/* Print times when acceleration is due only to gravity. */


while (rocket >> time >> altitude >> velocity >> acceleration)
{
if ((acceleration<=MAX_GRAVITY) && (acceleration>=MIN_GRAVITY))
cout << "Acceleration due to gravity only at time: " << time << endl;
}

/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
Suture Packaging
/*--------------------------------------------------------------------*/
/* Problem chapter5_24 */
/* */
/* This program reads a data file named suture.dat that contains */
/* information on batches of sutures that have been rejected during */
/* a 1-week period. This program generates a report that computes */
/* the percent of the batches rejected due to temperature, the */
/* percent rejected due to pressure, and the percent rejected due */
/* to dwell time. */

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const double MIN_TEMP = 150;


const double MAX_TEMP = 170;
const double MIN_PRESS = 60;
const double MAX_PRESS = 70;
const double MIN_DWELL = 2;
const double MAX_DWELL = 2.5;
const string FILENAME = "suture.dat";
int main()
{
/* Define and initialize variables. */
int temp_rejects=0, press_rejects=0, dwell_rejects=0,
batches_rejected=0, batch;

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


double temp_per, press_per, dwell_per, temperature, pressure,
dwell_time;
ifstream suture;

/* Open input file. */


suture.open(FILENAME.c_str());
if (suture.fail()){
cerr << FILENAME << " failed to open.\n";
exit(1);
}

/* Categorize rejected batches by failures */


/* and record total number of rejected batches, */
/* until there is no more data to process. */
while(suture >> batch >> temperature >> pressure >> dwell_time)
{
if ((temperature<MIN_TEMP) || (temperature>MAX_TEMP))
temp_rejects++;
if ((pressure<MIN_PRESS ) || (pressure>MAX_PRESS ))
press_rejects++;
if ((dwell_time<MIN_DWELL) || (dwell_time>MAX_DWELL))
dwell_rejects++;
batches_rejected++;
}

/* Now calculate the percentages required and print them. */


temp_per = (double)temp_rejects /(double)batches_rejected;
press_per = (double)press_rejects/(double)batches_rejected;
dwell_per = (double)dwell_rejects/(double)batches_rejected;
cout << "Category Percent rejected\n";
cout << "---------------------------------\n";
cout << "temperature " << temp_per << endl;
cout << "pressure " << press_per << endl;
cout << "dwell time " << dwell_per;
cout << "\n\nTotal number of batches: " << batches_rejected << endl;

/* Exit program. */
return 0;
}
/*-------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_25 */
/* */
/* This program reads a data file named suture.dat that contains */
/* information on batches of sutures that have been rejected during */
/* a 1-week period. This program generates a report that presents */
/* the number of the batches rejected due to temperature, the */
/* number rejected due to pressure, the number rejected due to dwell */
/* time and the total number of rejects (with no duplicates) along */
/* with the percentages. */

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


using namespace std;

const double MIN_TEMP = 150;


const double MAX_TEMP = 170;
const double MIN_PRESS = 60;
const double MAX_PRESS = 70;
const double MIN_DWELL = 2;
const double MAX_DWELL = 2.5;
const string FILENAME = "suture.dat";
int main()
{
/* Define and initialize variables. */
int temp_rejects=0, press_rejects=0, dwell_rejects=0,
batches_rejected=0, batch;
double temp_per, press_per, dwell_per, temperature, pressure,
dwell_time;
ifstream suture;

/* Open input file. */


suture.open(FILENAME.c_str());
if (suture.fail()){
cerr << FILENAME << " failed to open.\n";
exit(1);
}

/* Categorize rejected batches by failures */


/* and record total number of rejected batches, */
/* until there is no more data to process. */
while(suture >> batch >> temperature >> pressure >> dwell_time)
{
if ((temperature<MIN_TEMP) || (temperature>MAX_TEMP))
temp_rejects++;
if ((pressure<MIN_PRESS ) || (pressure>MAX_PRESS ))
press_rejects++;
if ((dwell_time<MIN_DWELL) || (dwell_time>MAX_DWELL))
dwell_rejects++;
batches_rejected++;
}

/* Now calculate the percentages required and print them. */


temp_per = (double)temp_rejects /(double)batches_rejected;
press_per = (double)press_rejects/(double)batches_rejected;
dwell_per = (double)dwell_rejects/(double)batches_rejected;

cout.precision(2);
cout << "Category Percent rejected Count rejected\n";
cout << "------------------------------------------------\n";
cout << "temperature " << temp_per << "\t\t\t"
<< temp_rejects << endl;
cout << "pressure " << press_per << "\t\t\t"
<< press_rejects << endl;
cout << "dwell time " << dwell_per << "\t\t\t"
<< dwell_rejects << endl;
cout << "\n\nTotal number of rejected batches: "
<< batches_rejected << endl;

/* Exit program. */

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


return 0;
}
/*-------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_26 */
/* */
/* This program reads the data file suture.dat and makes */
/* sure that the information relates only to batches that should */
/* have been rejected. If any batch should not be in the data */
/* file, an error message with the batch information is printed. */

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const double MIN_TEMP = 150;


const double MAX_TEMP = 170;
const double MIN_PRESS = 60;
const double MAX_PRESS = 70;
const double MIN_DWELL = 2;
const double MAX_DWELL = 2.5;
const string FILENAME = "suture.dat";

int main()
{
/* Define and initialize variables. */
int reject, batch, count(0);
double temperature, pressure, dwell_time;
ifstream suture;

/* Open input file. */


suture.open(FILENAME.c_str());
if (suture.fail()){
cerr << FILENAME << " failed to open.\n";
exit(1);
}

/* Check that the data really is for rejected suture packages. */


while(suture >> batch >> temperature >> pressure >> dwell_time)
{
reject = false;
if ((temperature<MIN_TEMP) || (temperature>MAX_TEMP))
reject = true;
if ((pressure<MIN_PRESS ) || (pressure>MAX_PRESS ))
reject = true;
if ((dwell_time<MIN_DWELL) || (dwell_time>MAX_DWELL))
reject = true;
if (!reject){
count++;
cout << "Batch " << batch << " should not be rejected.\n";
cout << " Temperature: " << temperature << endl;
cout << " Pressure: " << pressure << endl;
cout << " Dwell Time: " << dwell_time << endl << endl;
}

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


}
if (!count)
cout << "All data in this batch is for rejected suture packages.\n";

/* Exit program. */
return 0;
}
/*--------------------------------------------------------------------*/
Timber Regrowth
/*--------------------------------------------------------------------*/
/* Problem chapter5_27 */
/* */
/* Assume that there are 14,000 acres total with 2500 acres */
/* uncut, and that the reforestation rate is 0.02. */
/* This program prints a table showing the number of acres */
/* forested at the end of each year, for a total of 20 years. */

#include <iostream>

using namespace std;

const double REFOREST_RATE = 0.02;


const double UNCUT_ACRES = 2500;
const double MAX_YEARS = 20;

int main()
{
// Declare and initialize variables.
int year=1;
double forested=UNCUT_ACRES;

// Print headings.
cout << "YEAR FORESTED ACRES AT END OF YEAR\n";
cout << "--------------------------------------\n";

// Print amount forsted for MAX_YEARS.


while (year <= MAX_YEARS)
{
forested += REFOREST_RATE*forested;
cout << year << " " << forested << endl;
year++;
}

// Exit program.
return 0;
}
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_28 */
/* */
/* This program modifies the solution to problem 27 so that the */
/* user can enter the number of years to use in the table and the */
/* program will print the reforestation rates. */

#include <iostream>

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


using namespace std;

const double REFOREST_RATE = 0.02;


const double UNCUT_ACRES = 2500;
const double MAX_ACRES = 14000;

int main()
{
// Declane and initialize variables.
int year=1, max_years(0);
double forested=UNCUT_ACRES;

// Print headers.
cout << "MAXIMUM number of acres to reforest: " << MAX_ACRES << endl;
cout << "MINIMUM number of acres: " << UNCUT_ACRES << "\n\n";

// Read years for reforestation table.


while (max_years<=0) {
cout << "Enter years for reforestation table: ";
cin >> max_years;
}

// Set format.
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

// Print headings.
cout << "\n\nYEAR FORESTED ACRES AT END OF YEAR \n";
cout << "-------------------------------------- \n";

// Compute and print yearly reforestation information.

while (year <= max_years)


{
forested += REFOREST_RATE*forested;
cout << year << " " << forested << endl;
year++;

// Exit program.
return 0;
}
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_29 */
/* */
/* This program modifies the solution to problem 27 so that the */
/* user can enter the number of acres and the program will */
/* determine how many years are required for the number */
/* of acres to be completely reforested. */

#include <iostream>

using namespace std;

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


const double REFOREST_RATE = 0.02;
const double UNCUT_ACRES = 2500;
const double MAX_ACRES = 14000;

int main()
{
// Define and initialize variables.
int year=0;
double forested=UNCUT_ACRES, acres=UNCUT_ACRES-1;

// Print headers.
cout << "MAXIMUM number of acres to reforest: " << MAX_ACRES << endl;
cout << "MINIMUM number of acres: " << UNCUT_ACRES << "\n\n";

// Read acres to be reforested.


while ((acres>MAX_ACRES) || (acres<UNCUT_ACRES))
{
cout << "Enter acres to be reforested: ";
cin >> acres;
}

// Set format.
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

// Print headings.
cout << "\n\nYEAR FORESTED ACRES AT END OF YEAR \n";
cout << "-------------------------------------- \n";

// Compute and print yearly reforestation information.

while (forested <= acres)


{
year++;
forested += REFOREST_RATE*forested;
cout << year << " " << forested << endl;
}

// Print years required for reforestation.


cout << "\n\nUp to " << year << " years are required to reforest "
<< acres << " acres\n";

// Exit program.
return 0;
}
/*--------------------------------------------------------------------*/
Weather Patterns
/*--------------------------------------------------------------------*/
/* Problem chapter5_30 */
/* */
/* This program determines the number of days in each of six */
/* temperature categories for the days of January 1991. */

#include <iostream>
#include <fstream>

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


#include <string>
using namespace std;

const int DATA_PER_DAY = 32;


const string FILE1 = "jan91.dat";
const int TEMP_DATA = 3;

int main()
{
/* Declare and initialize variables. */
int i, below0(0), from0to32(0), from33to50(0), from51to60(0),
from61to70(0), above70(0);
double min_temp, max_temp, dummy;
ifstream weather;

/* Open input file. */


weather.open(FILE1.c_str());
if (weather.fail()){
cerr << FILE1 << " failed to open.\n";
exit(1);
}

/* Read data and count days in temperature categories. */


while(weather >> dummy >> max_temp >> min_temp)
{
if (min_temp < 0) {
below0++;
if (max_temp > 70) {
from0to32++; //Since the temperature went through these ranges
from33to50++;
from51to60++;
from61to70++;
above70++;
} else if (max_temp > 60) {
from0to32++; //Since the temperature went through these ranges
from33to50++;
from51to60++;
from61to70++;
} else if (max_temp > 50) {
from0to32++; //Since the temperature went through these ranges
from33to50++;
from51to60++;
} else if (max_temp > 32) {
from0to32++; //Since the temperature went through these ranges
from33to50++;
} else if (max_temp > 0) {
from0to32++; //Since the temperature went through this range
}
} else if (min_temp <= 32 ) {
from0to32++;
if (max_temp > 70) {
from33to50++; //Since the temperature went through these ranges
from51to60++;
from61to70++;
above70++;
} else if (max_temp > 60) {
from33to50++; //Since the temperature went through these ranges

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


from51to60++;
from61to70++;
} else if (max_temp > 50) {
from33to50++; //Since the temperature went through these ranges
from51to60++;
} else if (max_temp > 32) {
from33to50++; //Since the temperature went through this range
}
} else if (min_temp <= 50) {
from33to50++;
if (max_temp > 70) {
from51to60++; //Since the temperature went through these ranges
from61to70++;
above70++;
} else if (max_temp > 60) {
from51to60++; //Since the temperature went through these range
from61to70++;
} else if (max_temp > 50) {
from51to60++; //Since the temperature went through these ranges
}
} else if (min_temp <=60) {
from51to60++;
if (max_temp > 70) {
from61to70++; //Since the temperature went through these ranges
above70++;
} else if (max_temp > 60) {
from61to70++;
}
} else if (min_temp <=70) {
from61to70++;
if (max_temp > 70) {
above70++;
}
} else {
above70++;
}

/* Skip the rest of the line of data. */


for (i=TEMP_DATA+1; i<=DATA_PER_DAY; i++)
weather >> dummy;
}

/* Print results. */
cout << "January of 1991" << endl;
cout << "Temperature Ranges \t Number of Days " << endl;
cout << "Below 0 \t \t\t" << below0 << endl;
cout << "0 to 32 \t \t\t" << from0to32 << endl;
cout << "33 to 50\t \t\t" << from33to50 << endl;
cout << "51 to 60\t \t\t" << from51to60 << endl;
cout << "61 to 70\t \t\t" << from61to70 << endl;
cout << "Above 70 \t \t\t" << above70 << endl;

/* Close file and exit program. */


weather.close();
return 0;
}
/*--------------------------------------------------------------------*/

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


/*--------------------------------------------------------------------*/
/* Problem chapter5_31 */
/* */
/* This program determines the percentage of days in each of six */
/* temperature categories for the days of January 1991. */

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int DATA_PER_DAY = 32;


const string FILE1 = "jan91.dat";
const int TEMP_DATA = 3;

int main()
{
/* Declare and initialize variables. */
int i, below0(0), from0to32(0), from33to50(0), from51to60(0),
from61to70(0), above70(0), days(0);
double min_temp, max_temp, dummy;
ifstream weather;

/* Open input file. */


weather.open(FILE1.c_str());
if (weather.fail()){
cerr << FILE1 << " failed to open.\n";
exit(1);
}

/* Read data and count days in temperature categories. */


while(weather >> dummy >> max_temp >> min_temp)
{
if (min_temp < 0) {
below0++;
if (max_temp > 70) {
from0to32++; //Since the temperature went through these ranges
from33to50++;
from51to60++;
from61to70++;
above70++;
} else if (max_temp > 60) {
from0to32++; //Since the temperature went through these ranges
from33to50++;
from51to60++;
from61to70++;
} else if (max_temp > 50) {
from0to32++; //Since the temperature went through these ranges
from33to50++;
from51to60++;
} else if (max_temp > 32) {
from0to32++; //Since the temperature went through these ranges
from33to50++;
} else if (max_temp > 0) {
from0to32++; //Since the temperature went through this range

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


}
} else if (min_temp <= 32 ) {
from0to32++;
if (max_temp > 70) {
from33to50++; //Since the temperature went through these ranges
from51to60++;
from61to70++;
above70++;
} else if (max_temp > 60) {
from33to50++; //Since the temperature went through these ranges
from51to60++;
from61to70++;
} else if (max_temp > 50) {
from33to50++; //Since the temperature went through these ranges
from51to60++;
} else if (max_temp > 32) {
from33to50++; //Since the temperature went through this range
}
} else if (min_temp <= 50) {
from33to50++;
if (max_temp > 70) {
from51to60++; //Since the temperature went through these ranges
from61to70++;
above70++;
} else if (max_temp > 60) {
from51to60++; //Since the temperature went through these range
from61to70++;
} else if (max_temp > 50) {
from51to60++; //Since the temperature went through these ranges
}
} else if (min_temp <=60) {
from51to60++;
if (max_temp > 70) {
from61to70++; //Since the temperature went through these ranges
above70++;
} else if (max_temp > 60) {
from61to70++;
}
} else if (min_temp <=70) {
from61to70++;
if (max_temp > 70) {
above70++;
}
} else {
above70++;
}

/* Skip the rest of the line of data. */


for (i=TEMP_DATA+1; i<=DATA_PER_DAY; i++)
weather >> dummy;

days++;
}

/* Print results. */
cout << "January of 1991 with " << days << " days "<< endl;
cout << "Temperature Ranges \t Percentage of Days " << endl;

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


cout << "Below 0 \t \t\t" << double(below0)/days*100 << endl;
cout << "0 to 32 \t \t\t" << double(from0to32)/days*100 << endl;
cout << "33 to 50\t \t\t" << double(from33to50)/days*100 << endl;
cout << "51 to 60\t \t\t" << double(from51to60)/days*100 << endl;
cout << "61 to 70\t \t\t" << double(from61to70)/days*100 << endl;
cout << "Above 70 \t \t\t" << double(above70)/days*100 << endl;

/* Close file and exit program. */


weather.close();
return 0;
}
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_32 */
/* */
/* This program determines the number of days in each of six */
/* temperature categories for the days of May - August 1991. */
/* This program requires the user to enter the names of the data */
/* files. */

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int DATA_PER_DAY = 32;


const int TEMP_DATA = 3;

int main()
{
/* Declare and initialize variables. */
int month, j, i, below0(0), from0to32(0), from33to50(0), from51to60(0),
from61to70(0), above70(0);
double min_temp, max_temp, dummy;
string FILE1;
ifstream weather;

for (month=5;month<=8;month++){
/* Open input file. */
switch (month){
case 5:
FILE1="may91.dat";
break;
case 6:
FILE1="jun91.dat";
break;
case 7:
FILE1="jul91.dat";
break;
case 8:
FILE1="aug91.dat";
break;
}

weather.open(FILE1.c_str());

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


/* Read data and count days in temperature categories. */
while(weather >> dummy >> max_temp >> min_temp) {
if (min_temp < 0) {
below0++;
cout << "min_temp below 0";
if (max_temp > 70) {
from0to32++; //Since the temperature went through these ranges
from33to50++;
from51to60++;
from61to70++;
above70++;
} else if (max_temp > 60) {
from0to32++; //Since the temperature went through these ranges
from33to50++;
from51to60++;
from61to70++;
} else if (max_temp > 50) {
from0to32++; //Since the temperature went through these ranges
from33to50++;
from51to60++;
} else if (max_temp > 32) {
from0to32++; //Since the temperature went through these ranges
from33to50++;
} else if (max_temp > 0) {
from0to32++; //Since the temperature went through this range
}
} else if (min_temp <= 32 ) {
from0to32++;
if (max_temp > 70) {
from33to50++; //Since the temperature went through these ranges
from51to60++;
from61to70++;
above70++;
} else if (max_temp > 60) {
from33to50++; //Since the temperature went through these ranges
from51to60++;
from61to70++;
} else if (max_temp > 50) {
from33to50++; //Since the temperature went through these ranges
from51to60++;
} else if (max_temp > 32) {
from33to50++; //Since the temperature went through this range
}
} else if (min_temp <= 50) {
from33to50++;
if (max_temp > 70) {
from51to60++; //Since the temperature went through these ranges
from61to70++;
above70++;
} else if (max_temp > 60) {
from51to60++; //Since the temperature went through these range
from61to70++;
} else if (max_temp > 50) {
from51to60++; //Since the temperature went through these ranges
}
} else if (min_temp <=60) {
from51to60++;

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


if (max_temp > 70) {
from61to70++; //Since the temperature went through these ranges
above70++;
} else if (max_temp > 60) {
from61to70++;
}
} else if (min_temp <=70) {
from61to70++;
if (max_temp > 70) {
above70++;
}
} else {
above70++;
}

/* Skip the rest of the line of data. */


for (i=TEMP_DATA+1; i<=DATA_PER_DAY; i++)
weather >> dummy;
}
/* Close file. */
weather.close();
}
/* Print results. */
cout << "May through August of 1991" << endl;
cout << "Temperature Ranges \t Number of Days " << endl;
cout << "Below 0 \t \t\t" << below0 << endl;
cout << "0 to 32 \t \t\t" << from0to32 << endl;
cout << "33 to 50\t \t\t" << from33to50 << endl;
cout << "51 to 60\t \t\t" << from51to60 << endl;
cout << "61 to 70\t \t\t" << from61to70 << endl;
cout << "Above 70 \t \t\t" << above70 << endl;

/* Close file and exit program. */


return 0;
}
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_33 */
/* */
/* This program determines the average temperature for */
/* days with fog in November 1991. */

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int DATA_PER_DAY = 32;


const string FILE1 = "nov91.dat";
const int FOG_DATA = 8;
const int FOG = 1;

int main()
{
/* Declare and initialize variables. */
int i, types, foggy=0;

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


double ave_foggy = 0.0, ave_temp, dummy;
ifstream weather;

/* Open input file. */


weather.open(FILE1.c_str());

/* Read data and count foggy days. */


while(weather >> dummy >> dummy >> dummy >> ave_temp )
{
/* Ignore data up to the fog information. */
for (i=4; i<FOG_DATA; i++)
weather >> dummy;

/* Read this column for fog information. */


weather >> types;
if (types == FOG)
{
foggy++;
ave_foggy += ave_temp;
}

/* Skip the rest of the line of data. */


for (i=FOG_DATA+1; i<DATA_PER_DAY; i++)
weather >> dummy;
}

/* Print results. */
if (foggy >0 )
cout << "Average temperature for the " << foggy << " foggy days in "
<< "Nov 1991: " << ave_foggy/foggy << endl;
else
cout << "No foggy days in Nov 1991.\n";

/* Close file and exit program. */


weather.close();
return 0;
}
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_34 */
/* */
/* This program determines the date in December 1991 with the */
/* largest difference between the maximum temperature and the */
/* minimum temperature. The date, the temperatures, and the */
/* differences are printed. */

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int DATA_PER_DAY = 32;


const string FILE1 = "dec91.dat";

int main()
{

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


/* Declare and initialize variables. */
int dayofmonth, day, i;
double max_temp, min_temp, dummy, difference, big_difference=0,
result_max, result_min;
ifstream weather;

/* Open input file. */


weather.open(FILE1.c_str());

/* Read data and find largest difference. */


while(weather >> day >> max_temp >> min_temp)
{
difference = max_temp - min_temp;
if (big_difference < difference)
{
dayofmonth = day;
big_difference = difference;
result_max = max_temp;
result_min = min_temp;
}

/* Skip the rest of the line of data. */


for (i=3; i<DATA_PER_DAY; i++)
weather >> dummy;
}

/* Print results. */
cout << "The maximum difference in temperatures was: " << big_difference
<< " degrees Farenheit.\n";
cout << "It occured on December " << dayofmonth << endl;
cout << "Max temperature was: " << result_max <<
" and the min temperature was " << result_min << endl;
/* Close file and exit program. */
weather.close();
return 0;
}
/*--------------------------------------------------------------------*/
Critical-Path Analysis
/*--------------------------------------------------------------------*/
/* Problem chapter5_35 */
/* */
/* This program reads the critical path information */
/* and prints a project completion timetable that lists */
/* each even number, the maximum number of days for a */
/* task within the even, and the total number of days for */
/* the project completion. */

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const string FILENAME = "path.dat";

int main()
{
/* Declare and initialize variables. */

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


int event, previous_event=1, task=0, days=0, max_days=0,
project_days=0;
ifstream path;

/* Open data file. */


path.open(FILENAME.c_str());
if (path.fail()){
cerr << FILENAME << " failed to open.\n";
exit(1);
}

/* Set up the headers before the loop. */


cout << "Event Max Number Days for a Task \n";
cout << "------------------------------------ \n";
while(path >> event >> task >> days)
{
if (event == previous_event)
{
/* Count tasks for the current event. */
if (days>max_days)
max_days = days;
}
else
{ /* A new event, so we can finish up the current one */
/* and start keeping values for the new one. */
project_days += max_days;
cout << previous_event << " " << max_days << endl;
previous_event = event;
max_days = days;
}
}

/* Count the last event, and print the total project duration. */
project_days += max_days;
cout << event << " " << max_days << endl;
cout << "Project duration: " << project_days << endl;

/* Close file and exit program. */


path.close();
return 0;
}
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_36 */
/* */
/* This program reads the critical path information */
/* and print a report that lists the event number and */
/* task number for all tasks requiring more than 5 days. */

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const string FILENAME="path.dat";

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


const int MAX_DAYS=5;

int main()
{
/* Declare variables. */
int event, task, days;
ifstream path;

/* Open input file. */


path.open(FILENAME.c_str());
if (path.fail()){
cerr << FILENAME << " failed to open.\n";
exit(1);
}

/* Set up the headers before the loop. */


cout << "Tasks requiring more than " << MAX_DAYS << " days:\n";
cout << "Event Task Days \n";
cout << "----------------------- \n";

/* Print tasks requiring more than MAX_DAYS days. */


while(path >> event >> task >> days)
{
if (days > MAX_DAYS)
cout << event << " " << task << " " << days << endl;
}

/* Close file and exit program. */


path.close();
return 0;
}
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/* Problem chapter5_37 */
/* */
/* This program reads the critical path information */
/* and prints a report that lists the number of each event */
/* and a count of the number of tasks within the event. */

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const string FILENAME="path.dat";

int main()
{
/* Declare and initialize variables. */
int event, previous_event=1, task, days, number_tasks=0;;
ifstream path;

/* Open input file. */


path.open(FILENAME.c_str());
if (path.fail()){
cerr << FILENAME << " failed to open.\n";

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


exit(1);
}

/* Set up the headers before the loop */


printf("Event Number of Tasks \n");
printf("------------------------------------ \n");

/* Read data and print event information. */


while(path >> event >> task >> days)
{
if (event == previous_event)
/* Count tasks for the current event. */
number_tasks++;
else
{ /* A new event, so we can finish up the one and */
/* start keeping values for the new one. */
cout << previous_event << " " << number_tasks << endl;
previous_event = event;
number_tasks = 1;
}
}

/* Print the last event. */


cout << event << " " << number_tasks << endl;

/* Close file and exit program. */


path.close();
return 0;
}
/*--------------------------------------------------------------------*/

© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

You might also like