Exam Practice!
True/False Problems
1. T
Multiple Choice Problems
8. (b)
Program Analysis
11. 1
Programming Problems
/*——————————————————————–*/
/* Problem chapter6_18 */
cout << “\nEnter number of primes you would like: “;
cin >> input;
for (i=1;i<=n;i++){
prime=true;
for (j=2;j<i;j++){
if (!(i%j)) {
prime=false;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void primeGen(int n, ofstream& file);
int main()
/* Call the function. */
primeGen(input, outfile);
outfile.close();
return 0;
}
/*——————————————————————–*/
/* Function to calculate prime numbers. */
void primeGen(int n, ofstream& out){
}
return;
}
/*——————————————————————–*/
using namespace std;
int countInts(ifstream& file);
int main()
{
/* Declare and initialize variables */
ifstream infile;
exit(1);
}
/* Call the function. */
numInts = countInts(infile);
}
in >> num;
}
return count;
}
/*——————————————————————–*/
int main()
{
/* Declare and initialize variables */
exit(1);
}
/* Call the function. */
printFlags(infile);
infile.close();
return 0;
}
/*——————————————————————–*/
/* Function to count integers. */
void printFlags(ifstream& in){
/*——————————————————————–*/
Simple Simulations
/*——————————————————————–*/
/* Problem chapter6_22 */
/* */
/* This program simulates tossing a “fair” coin. */
/* The user enters the number of tosses. */
{
/* 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;
#include <iostream>
#include <cstdlib>
using namespace std;
const int HEADS = 10;
const int TAILS = 1;
while (tosses < required)
{
tosses++;
if (rand_int(TAILS,HEADS) <= WEIGHT)
heads++;
}
/* Print results. */
/*——————————————————————*/
/*——————————————————————–*/
/* Problem chapter6_24 */
/* */
/* This program simulates tossing a “fair” coin using Coin class. */
/* 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”;
Coin c1;
if (c1.getFaceValue() == Coin::HEADS)
heads++;
}
/* Print results. */
cout << “\n\nNumber of tosses: ” << tosses << endl;
#include <iostream>
#include <cstdlib> //required for rand()
using namespace std;
int rand_int(int a, int b);
class Coin
{
faceValue = TAILS;}
//Accessors
char getFaceValue() const {return faceValue;}
//Mutators
//Coins are not mutable
};
{
/* The user enter the number of rolls. */
#include <iostream>
#include <cstdlib>
using namespace std;
int rand_int(int a, int b);
while (rolls < required)
{
rolls++;
switch(rand_int(MIN,MAX))
{
case 1: onedot++;
break;
exit(1);
break;
}
}
/* Print the results. */
cout << “\nNumber of rolls: ” << rolls << endl;
}
/*——————————————————————*/
/* (rand_int function from page 257) */
/*——————————————————————*/
/*——————————————————————*/
/* Problem chapter6_26 */
/* */
/* This program simulates an experiment rolling two six-sided */
/* “fair” dice. The user enters the number of rolls. */
{
/* Declare variables. */
int rolls=0, die1, die2, required=0, sum=0;
/* Prompt user for number of rolls. */
cout << “\n\nEnter number of fair dice rolls: “;
cin >> required;
#include <iostream>
#include <cstdlib>
using namespace std;
const int MIN = 1;
{
cout << “The number of lotteries must be an integer number, ”
<< “greater than zero.\n\n”;
cout << “Enter number of lotteries: “;
cin >> required;
}
/* Are any of them equal to NUMBER? */
if ((first == NUMBER) || (second==NUMBER) || (third == NUMBER))
num_in_sim++;
/* Are they 1-2-3 in any order? */
if ((first <= 3) && (second <= 3) && (third <= 3))
if ((first != second) && (first != third) && (second != third))
onetwothree++;
}
}
/*——————————————————————–*/
/* (rand_int function from page 257) */
/*——————————————————————–*/
Component Reliability
/*——————————————————————–*/
/* Problem chapter6_28 */
}
/* 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 */
double est1, est2, est3, est4;
/* Run simulations. */
/* Exit program. */
return 0;
}
/*——————————————————————–*/
/* (rand_float function from page 257) */
/*——————————————————————–*/
/* 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;
/* Open output file. */
wind_data.open(FILENAME.c_str());
}
/* Close file and exit program. */
wind_data.close();
return 0;
}
/*——————————————————————–*/
/* (rand_float function from page 257) */
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter6_32 */
using namespace std;
const int DELTA_TIME = 10;
const int START_TIME = 0;
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;
/* Open file for writing */
wind_data.open(FILENAME.c_str());
/* Compute wind speeds. */
for (timer=START_TIME; timer<=STOP_TIME; timer+=DELTA_TIME)
{
if (storm_flag == STORM)
/* There is a storm, is it time to stop? */
if (storm_duration < MAX_DURATION)
storm_duration += DELTA_TIME;
else
storm_flag = NO_STORM;
}
/* Close file and exit program. */
wind_data.close();
return 0;
}
/*——————————————————————–*/
/* (rand_float function from page 257) */
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter6_33 */
using namespace std;
const int DELTA_TIME = 10;
const int START_TIME = 0;
double rand_float(double a, double b);
int main()
{
/* Define variables. */
/* Compute wind speeds. */
for (timer=START_TIME; timer<=STOP_TIME; timer+=DELTA_TIME)
{
if (storm)
/* There is a storm, is it time to stop? */
if (storm_duration < STORM_TIME) {