Exam Practice!
True/False Problems
1. T
Multiple Choice
6. (a)
Memory Snapshot Problems
9. t ->
H
e
l
l
\0
Program Output
11. 1
Programming Exercises
Linear Interpolation
/*——————————————————————–*/
/* Problem chapter7_17 */
/* */
5
8
11
14
17
int main()
}
/* Read data. */
while ( (tunnel >> angle[index] >> coef[index]) && !tunnel.eof())
index++;
/* Prompt user for input. */
cout << “Enter angle: “;
cin >> request;
/*——————————————————————-*/
/* This function interpolates y values, given x and a new value. */
/* It is assumed that x is in ascending order. */
double interpolate(double value, int x[], double y[])
{
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter7_18 */
/* */
double interpolate(double, int x[], double y[]);
int main()
{
/* Define variables and function prototypes. */
/* Read data. */
while ( (tunnel >> angle[index] >> coef[index]) && !tunnel.eof())
index++;
/* Tell user the range of angles. */
<< interpolate(request, angle, coef) << endl;
/* Exit program. */
return 0;
}
double interpolate(double value, int x[], double y[])
else
/* value is between x[i-1] and x[i] */
return (y[i-1] + (value x[i-1])/
(x[i]-x[i-1]) * (y[i] y[i-1]));
}
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter7_19 */
/* Determine if angles are out of order. */
for (i=1; i<num_pts; i++)
if ( x[i-1] > x[i] )
ascending=0;
/* Return in order value. */
return ascending;
}
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter7_20 */
/* */
for (k=0; k<num_pts-1; k++)
{
/* Exchange minimum with next array value. */
m = k;
/* void return */
return;
}
/*———————————————————————-*/
/*——————————————————————–*/
/* Problem chapter7_21 */
/* */
int ordered(double x[], int num_pts);
void reorder(double x[], double y[], int num_pts);
double interpolate(double, double x[],double y[]);
int main()
cout << “Flight-path angles are in ascending order.” << endl;
else
{
cout << “Out of order flight-path angles are being reordered.” << endl;
reorder(angle,coef,index);
}
/* Tell user the range of angles. */
return 0;
}
/*——————————————————————–*/
/* This function verifies that the flight-path angles are in */
/* ascending order. The function returns a zero if the angles */
/* are not in order and a 1 if they are in order. */
}
/*——————————————————————–*/
/* This function reorders the values in x so that they are */
/* in ascending order and the relationship between x and y is */
/* preserved. */
void reorder(double x[], double y[], int num_pts)
{
}
hold = x[m];
x[m] = x[k];
x[k] = hold;
double interpolate(double value, double x[], double y[])
{
/* Declare variables. */
int i=0;
/* Find the angles that value is between. */
while (value > x[i])
i++;
/* */
/* This program generates sequences of random floating-point */
/* values between 4 and 10. It then compare the computed mean */
/* and variance to the theoretical values. */
#include <iostream>
#include <cstdlib>
/* Get number of samples from user. */
cout << “Enter number of random numbers to use (< ” << MAX_TIMES << “): “;
cin >> times;
/* Generate random numbers in the sample. */
}
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter7_23 */
/* */
/* This program generates two sequences of 500 points. Each */
const int MAX_POINTS = 500;
double rand_float(double, double);
{
for (index=0; index<MAX_POINTS; index++)
{
first_sequence[index] = rand_float(a1,b1);
second_sequence[index] = rand_float(a2,b2);
}
/* Calculate sample means and variances. */
var1 = variance(first_sequence,MAX_POINTS);
/*——————————————————————–*/
/* chapter7_24 */
/* */
/* This program generates two sequences of 500 points. Each */
/* sequence should have a theoretical variance of 3, but one */
double rand_float(double, double);
double variance(double x[], int);
double mean(double x[], int);
int main()
{
/* Define variables and function prototypes. */
{
first_sequence[index] = rand_float(a1,b1);
second_sequence[index] = rand_float(a2,b2);
}
/* Calculate sample means and variances. */
var1 = variance(first_sequence,MAX_POINTS);
var2 = variance(second_sequence,MAX_POINTS);
/* Function chapter7_25 */
/* */
/* This function generates a random number */
/* with a specified mean and variance. */
double rand_mv(double mu, double sigma_sq)
{
/* Problem chapter7_26 */
/* */
/* This program reads a data file and determines the number of */
/* occurances of each character in the file. It then prints the */
/* characters and the number of times that they occurred. */
int occur[NUM_ASC], i;
char c;
ifstream encrypted;
string inname;
// Prompt user for file names.
exit(1);
}
/* Initialize array. */
for (i = 0; i<NUM_ASC; i++)
occur[i] = 0;
/* Count characters */
/* Problem chapter7_27 */
/* */
/* This program reads a data file and determines the secret */
/* message stored by the sequence of first letters of the words. */
#include <iostream>
char c;
ifstream encrypted;
string inname;
// Prompt user for file names.
// Open the file for reading.
exit(1);
}
/* Get first character from file */
encrypted.get(c);
/* Determine message. */
while( !encrypted.eof() )
{
default:
break;
}
/* Retrieve next character from the file */
encrypted.get(c);
}
/* Exit program. */
#include <string>
using namespace std;
const char NEWLINE = ‘\n’;
const char SPACE = ‘ ‘;
const char TAB = ‘\t’;
exit(1);
}
/* Get first character from the file */
encrypted.get(c);
/* Determine secret message. */
while( !encrypted.eof())
{
cout << c;
case TAB:
case SPACE:
inword = false;
was_first = false;
/* This program reads a data file and determines the secret */
/* message stored by characters that are three characters to the */
/* right in the collating sequence from the first letters of the */
/* words in the data file. */
#include <iostream>
bool inword=false;
char c;
ifstream encrypted;
string inname;
// Prompt user for file names.
exit(1);
}
/* Get first character from the file */
encrypted.get(c);
/* Determine secret message. */
while(!encrypted.eof())
{
inword = false;
default:
break;
}
/* Get next character from the file */
encrypted.get(c);
}
/* contains the letter that is to replace the letter b in the data */
/* file, and so on. Assume that all the punctuation is to be */
/* replaced by spaces. */
#include <iostream>
#include <cstdlib>
int key[NUM_LET], i=0, j;
char c;
ofstream encrypted;
ifstream original;
string inname, outname;
break;
default:
/* Insure unique decodability */
if ((c<‘!’) || (c>’~’))
cout << “Character out of range, try again.” << endl;
{
cout << “Key[” << i << “] is not unique.” << endl;
return 0;
}
}
// Prompt user for file names.
// Open the files for reading and writing.
cout << “enter name of input file ” << endl;
exit(1);
}
exit(1);
}
/* Get first character from the file */
original.get(c);
{
switch(c)
{
case NEWLINE:
case SPACE:
case TAB:
encrypted << c;
break;
case ‘.’:
case ‘,’:
case ‘?’:
exit(1);
}
exit(1);
}
/* Get first character from the file */
encrypted.get(c);
/* Now code the file. */
while(!encrypted.eof())
{
default:
decoded << c;
break;
}
/* Get next character from the file */
encrypted.get(c);
}
#include<cctype>
using namespace std;
//Function Prototypes
bool is_palindrome(char string[]);
//Global constant for string maximum string length
{
count = 0;
for(int k=0; k<strlen(buffer); ++k)
{
if(isalnum(buffer[k]))
{
new_string[count] = buffer[k];
++count;
}
}