Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual 1
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual 1
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 */
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
int main()
{
// Declare variables.
int counter=0;
char c;
string filename;
ifstream file1;
if(file1.fail())
{
cerr << "error opening file " << filename;
exit(1);
}
// Print results.
cout << endl << "Number of invalid characters is: " << counter << endl;
/*--------------------------------------------------------------------*/
/* 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>
int main()
{
// Declare variables.
char c;
int i, linenum(0), integers(0);
ifstream oldfile;
string inname;
oldfile >> i;
while (!oldfile.eof()) {
integers++;
oldfile >> i;
}
/*--------------------------------------------------------------------*/
/* 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>
int main()
{
// Declare variables.
char c;
ofstream newfile;
ifstream oldfile;
string inname, outname;
// Look for commas, and write the data to the new file.
c = oldfile.get();
while(!oldfile.eof())
{
if ( c != ',')
newfile << c;
c = oldfile.get();
}
/*--------------------------------------------------------------------*/
/* 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>
int main()
{
// Declare variables.
char c;
string inname, outname;
ofstream newfile;
ifstream oldfile;
/*--------------------------------------------------------------------*/
/* 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>
int main()
{
/* Declare variables. */
char c;
/*--------------------------------------------------------------------*/
/* 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>
int main()
{
/* Declare and initialize variables. */
char a,b;
int line=1, diff=0, line_flag=0;
ifstream afile, bfile;
/*--------------------------------------------------------------------*/
/* 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>
int main()
{
/* Declare variables. */
int code;
char oldchar;
ifstream original;
ofstream encrypted;
/* 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>
int main()
{
/* Declare variables. */
int decode;
char oldchar;
ofstream decoded;
ifstream encrypted;
/* 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>
int main()
{
/* Declare variables. */
int number_of_items;
double acceleration, altitude=0, previous_altitude=0.0,
previous_time, time=0, velocity;
/* 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;
int main()
{
/* Declare and initialize variables. */
int dir=INCREASE, stages=1;
double time, altitude, acceleration, velocity=0.0, prev_vel=0;
ifstream rocket;
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;
}
/* 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;
int main()
{
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;
}
/* 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>
int main()
{
/* Declare variables. */
double time, altitude, acceleration, velocity;
ifstream rocket;
/* 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>
/* 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>
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. */
/*--------------------------------------------------------------------*/
/* 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>
int main()
{
/* Define and initialize variables. */
int reject, batch, count(0);
double temperature, pressure, dwell_time;
ifstream suture;
/* 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>
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";
// 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>
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";
// 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";
// 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>
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";
// 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";
// 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>
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;
/* 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;
#include <iostream>
#include <fstream>
#include <string>
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;
days++;
}
/* Print results. */
cout << "January of 1991 with " << days << " days "<< endl;
cout << "Temperature Ranges \t Percentage of Days " << endl;
/*--------------------------------------------------------------------*/
/* 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;
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());
/*--------------------------------------------------------------------*/
/* 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;
int main()
{
/* Declare and initialize variables. */
int i, types, foggy=0;
/* 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";
/*--------------------------------------------------------------------*/
/* 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;
int main()
{
/* 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;
int main()
{
/* Declare and initialize variables. */
/* 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;
/*--------------------------------------------------------------------*/
/* 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>
int main()
{
/* Declare variables. */
int event, task, days;
ifstream path;
/*--------------------------------------------------------------------*/
/* 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;
int main()
{
/* Declare and initialize variables. */
int event, previous_event=1, task, days, number_tasks=0;;
ifstream path;