1. F
3. F
5. F
Multiple-Choice Problems
7. (c)
Memory Snapshot Problems
8. int i->0 int j->0
9. int i->5 int j->0
double x->1.0 double y->?
char ch1->? char ch2->?
10. int i->1 j->5
Programming Exercises
Data Filters
/*——————————————————————–*/
/* Problem chapter5_12 */
/* */
using namespace std;
const char NEWLINE = ‘\n’;
exit(1);
}
// Check file characters to see if legal.
// If not legal, count it.
c = file1.get();
while (!file1.eof())
{
switch (c)
{
}//end switch
c = file1.get();
} //end while
}
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter5_13 */
int main()
{
{
cerr << “error opening file ” << inname << endl;
exit(1);
}
//look for spaces and newlines
c = oldfile.get();
while(!oldfile.eof())
{
switch (c) {
}
//Print out values.
cout << “There are ” << linenum << ” lines in the file.” << endl;
cout << “There are ” << integers << ” integers in the file.” << endl;
/* commas from the information. */
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
// Prompt user for file names and
// Open the files for reading and writing respectively.
}
outname = “new” + inname;
newfile.open(outname.c_str());
// Look for commas, and write the data to the new file.
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 */
using namespace std;
const char NEWLINE = ‘\n’;
const char TAB = ‘\t’;
int main()
{
// Declare variables.
char c;
string inname, outname;
ofstream newfile;
ifstream oldfile;
exit(1);
}
outname = “new” + inname;
newfile.open(outname.c_str());
// Put only a single space between numerical values.
oldfile.get(c);
default:
/* Either a digit or a new line or EOF. */
newfile << c;
break;
}
oldfile >> c;
/* 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. */
string inname, outname;
ifstream oldfile;
ofstream newfile;
// Prompt user for file name.
cout << “enter name of input file “;
exit(1);
}
outname = “new” + inname;
newfile.open(outname.c_str());
newfile << c;
break;
}
oldfile.get(c);
}
/* Close files and exit program. */
newfile.close();
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
using namespace std;
exit(1);
}
exit(1);
}
/* Now compare the values. */
do
{
/* Get the character from each file. */
afile.get(a);
bfile.get(b);
{
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()));
return 0;
}
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter5_18 */
/* */
int main()
{
/* Declare variables. */
int code;
char oldchar;
ifstream original;
exit(1);
}
exit(1);
}
/* Get first character from the file */
original.get(oldchar);
/* Generate coded message. */
while (!original.eof())
{
break;
}
}
/* Retrieve next character from the file */
original.get(oldchar);
}
/* Exit program. */
return 0;
}
#include <cctype>
using namespace std;
const char NEWLINE = ‘\n’;
int main()
{
/* Declare variables. */
exit(1);
}
exit(1);
}
/* Get first character from the file */
encrypted.get(oldchar);
/* Generate decoded message. */
while( !encrypted.eof() )
{
break;
}
}
/* Get next character from the file */
encrypted.get(oldchar);
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const string FILENAME = “rocket1.dat”;
exit(1);
}
exit(1);
}
while (altitude>=previous_altitude &&!rocket1.eof())
{
previous_altitude = altitude;
previous_time = time;
rocket1 >> time >> altitude >> velocity >> acceleration;
}
<< previous_time << ” seconds and ” << time << ” seconds \n”;
/* */
/* 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>
ifstream rocket;
/* Open input file. */
rocket.open(FILENAME.c_str());
/* Read first set of data. */
rocket >> time >> altitude >> velocity >> acceleration;
dir = INCREASE;
}
/* Save values for comparison next time through loop */
prev_vel=velocity;
/* Read next data set */
rocket >> time >> altitude >> velocity >> acceleration;
}
#include <iostream>
/* Read first set of data. */
rocket >> time >> altitude >> velocity >> acceleration;
prev_vel = velocity;
/* Keep looking for the trailer while processing all the data */
}
/* Save values for comparison next time through loop */
prev_vel=velocity;
/* Read next data set */
rocket >> time >> altitude >> velocity >> acceleration;
}
/* Print results */
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
const double MAX_GRAVITY = -9.31; /* within -5% of gravity */
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;
}
}
/* 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>
{
/* 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;
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)
{
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;
/* 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. */
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,
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)
{
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;
/* 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>