Exam Practice!
True/False Problems
1. F
Multiple Choice Problems
Programming Problems
Binary Trees
/*———————————————————-*/
/* Program chapter10_8 */
//numLeafNodes: private, recursive method
int BinaryTree::numLeafNodes(node* theRoot) const
{
if(theRoot == NULL)
}
}
/*———————————————————-*/
/*———————————————————-*/
/* Program chapter10_9.cpp */
#include “node.h”
using namespace std;
int main()
{
}
BinaryTree bt; //create empty tree
//input data
fin >> val;
while(!fin.eof())
{
bt.insert(val);
fin >> val;
Image Processing
/*———————————————————-*/
/* Program chapter10_10 */
void dither(Pixel image[][MAX_COL_SIZE], int rows, int cols)
{
Pixel black(0);
}
}
/*———————————————————-*/
/*———————————————————-*/
/* Program chapter10_11 */
}
{
public:
rational(); //default constructor
//accessor functions
//arithmetic operators
rational operator +(rational& c2) const;
//input/output operators
friend ostream& operator <<(ostream&, rational);
friend istream& operator >>(istream&, rational&);
private:
}
istream& operator >> (istream& in, rational& r)
{
/* Assume the format of the input is a/b were a and b are ints */
char s;
in >> r.numerator >> s >> r.denominator;
if (r.denominator == 0)
{
}
int rational::get_numerator()
{
return(numerator);
}
int rational::get_denominator()
{
return(denominator);
}
rational rational::operator +(rational& r2) const
{
rational x;
x.numerator = numerator*r2.numerator;
x.denominator = denominator*r2.denominator;
return x;
}
rational rational::operator /( rational& r2) const
{
#include <iostream>
#include <ctime>
#include “player.h”
using namespace std;
Status Evaluate(Card, Card);
int main()
{
int cards_on_table=0;
{
mycard = myplayer.Draw();
table[cards_on_table++] = mycard;
yourcard = yourplayer.Draw();
case You_win:
cout << “YOUR CARD WINS!\n ” << endl;
for(int i=0; i<cards_on_table; i++)
}
}//end switch
}//end while
cout << “\nGame is over.\n”;
}
Status Evaluate(Card mycard, Card yourcard)
{
Status winner;
if(mycard.evaluate_rank() == 1 && yourcard.evaluate_rank() != 1)
winner = Iwin; //I have an ace
else if(yourcard.evaluate_rank() == 1 && mycard.evaluate_rank() != 1)
winner = You_win; //You have an ace
else if( mycard.evaluate_rank() > yourcard.evaluate_rank() )
/*———————————————————————–*/
/*———————————————————————–*/
/* Program chapter10_15 */
/* Vending Machine Simulation */
#include<iostream>
#include “Machine.h”
{
switch(menucode)
{
case 0:
return 0;
case 1:
}
/*—————————————————————*/
//filename: item.h
#ifndef ITEM_H
#define ITEM_H
{
private:
string itemName;
int cost;
int quantity;
public:
item() {cost=quantity=0;}
};
#endif
/*—————————————————————–*/
/* filename Machine.h */
#ifndef MACHINE_H
#define MACHINE_H
#include<iostream>
int getCurrentDep() const {return currentDeposit;}
int getTotalDep() const {return totalDeposit;}
void DisplayStatus();
void DisplayMenu();
void DepositMoney();
};
#endif
/*——————————————————————-*/
/* filename Machine.cpp */
#include “Machine.h”
#include<string>
#include<iomanip>
using namespace std;
Machine::Machine(ifstream& file1)
{
string n;
int c, q;
}
void Machine::DepositMoney()
{
int money;
cout<<“Machine accepts nickles(5), dimes(10), quarters(25)”
<< ” and dollars(100) only”;
cout << endl << “(enter -1 to stop)” << endl;
}//end while
cout << “\ttotal deposit: ” << this->currentDeposit << endl;
char ch;
cout << “Would you like to reprint the menu? (y/n)”;
cin >> ch;
}
cout << “enter you selection (Letters must be UPPER CASE):”;
cin >> row >> col;
if(row < ‘A’ || row > ‘D’ || col < 0 || col > 3)
{
cout << “invalid selection”<< endl;
DisplayMenu();
}
}
{
this->items[rownum][col].adjustQuantity(-1);
this->totalDeposit += this->items[rownum][col].getCost();
}
}
return;
}
void Machine::CoinReturn()
{
if(this->currentDeposit)
{
cout << “\tcoin return: ” <<endl;
while(this->currentDeposit >= QUARTER) //return quarters
{
cout << ‘\t’ << DIME <<endl;
this->currentDeposit -= DIME;
}
if(this->currentDeposit)
while(this->currentDeposit >= NICKLE) //return nickles
void Machine::DisplayStatus()
{
char letters[] = {‘A’, ‘B’, ‘C’, ‘D’};
char ch;
for(int j=0;j<COLS;++j)
cout << “\t\t” << j ;
cout << endl;
}
cout << endl;
}
void Machine::DisplayMenu()
{
cout << endl<< endl
<<“PLEASE USE THIS VENDING MACHINE. IF YOU HAVE PROBLEMS:”;
cout << endl << “Call 800-VENDING” << endl<< endl;
}
/*———————————————————————-*/
/*———————————————————————-*/
/* Program chapter10_16 */
/* This program implements an IPD tournament */
int main(i)
{
// Declare vector of pointers to BasePlayer.
{
p->print_name();
cout << ” is playing “;
p->print_algorithm();
cout << endl;
}
}
//Overloaded Tournament Function
void play_game(vector<BasePlayer*> players)
{
//Declare objects.
int max_iterations;
old_p1_move = p1_move;
p1_move = players[i]->play(p2_move); // get the next move
p2_move = players[j]->play(old_p1_move); // get the next move
// Update the scores for this round of play.
}
//Overloaded tournament function
void report(vector<BasePlayer*> players)
{
cout << “the scores are: ” << endl;
for(BasePlayer* p: players)
{
p->print_name();
{
}
}//end for
if(tiedForFirst.size() == 0)
{
cout << “The winner is “;
players[winner]->print_name();
cout << ” using alogorithm “;
players[p1]->print_algorithm();
cout << endl;
}
}
}
/* Program chapter 10_17 */
/* This implementation of the main() and play_game() */
/* allows players to meet each other multiple times. */
for(int r=0; r<max_rounds; r++)
{
cout << “Round ” << (r+1) << endl;
setup(players); //called once per round
play_game(players);
cout << endl;
}
for(int i=0; i<(players.size()-1); ++i)
{
//play all other players
for(int j=i+1; j<players.size(); ++j)
{
p1_move = players[i]->play(); // get the first move.
p2_move = players[j]->play(); // get the first move.
}
}
/* Image Processing */
/* Program chapter10_18 */
/* Class Declaration */
public:
AsciiImage();
void inputImage(istream& fin);
void writeP3Image(ostream& out);
/* */
#include “AsciiImageClass.h”
using namespace std;
AsciiImage::AsciiImage() : width(0), height(0), maxColor(0) {
}
void AsciiImage::inputImage(istream& fin)
{
readHeader(fin);
cout << “Header has been read. \n”
<< magicNumber << endl << “height,width,maxcolor are :”
<< height << ‘ ‘<<width<<” “<<maxColor << endl;
if(strcmp(magicNumber,”P3″) == 0)
}
}
else{
cout << “magicNumber ” << magicNumber
<< ” not supported.” << endl;
}
}
void AsciiImage::writeP3Image(ostream& out)
{
int counter(0);
for(string s: comment){
out << s << endl;
cout << s << endl;
}
}
}
}//end for i
while(ch == ‘#’)
{
fin.getline(aLine,100);
}
void AsciiImage::dither()
{
//visit every row
for(int i=0; i<height; ++i)
{
if(i%2)
{
//odd row, visit even columns
for(int j=0; j<width-2; j+=2)
}
}
void AsciiImage::smooth()
{
Pixel sum;
for(int i=1; i< (height-1); i++)
{
for(int j=1; j< (width-1); j++)
}