Programming work

EECS 280 Final Exam
Winter 2019
Reference material
Please do not open this booklet before the exam begins.
This booklet contains reference material that you will need in working some
of the sections in the exam.
You will need to turn this in with your exam.
You may write on these pages but nothing you write here will be
considered in grading your exam.
All your answers must be made in the exam booklet.
EECS 280 Winter 2019 Final Exam Reference Material 1/10
This page intentionally left blank.
You may use this space for scratch work, but we will not grade anything here.
EECS 280 Winter 2019 Final Exam Reference Material 2/10
Section 1 Reference Sheet
The “what does this do” question in section 1 refers to this program.
#include
using namespace std;
void PlayCatch( int x )
{
char y = ‘x’;
try
{
try
{
if ( x < 0 )
{
throw x;
cout << “After throw..”;
}
}
catch ( … )
{
cout << “Inner Catch..”;
throw y;
}
throw x;
cout << “Inside try..”;
}
catch ( int x )
{
cout << “Outer Catch..”;
}
catch ( … )
{
cout << “Default Catch..”;
}
cout << endl;
}
int main( )
{
PlayCatch( -1 );
PlayCatch( +1 );
}
EECS 280 Winter 2019 Final Exam Reference Material 3/10
Section 2 Reference Sheet
Section 2 in the exam refers to this code. You may assume all necessary #include directives
and using namespace declarations are present.
Consider the following partial class definitions. You are to write the Grow( ) function and
operator= overload for the Album class.
struct Pixel
{
// 24-bit color.
unsigned char R, G, B;
};
class Image
{
private:
// An image may either be no image (an image of
// 0 pixels and a null pixels pointer) or an actual
// image of rows * columns pixels allocated on
// the heap.
int rows, columns;
Pixel *pixels;
public:
Image( ) : rows( 0 ), columns( 0 ), pixels( nullptr )
{
}
Image( int r, int c ) : rows( r ), columns( c ),
pixels( new Pixel[ r * c ] )
{
}
Image( const Image &img );
~Image( );
// Assignment makes a deep copy.
Image &operator=( const Image &rhs );
};
(continued on the next page)
EECS 280 Winter 2019 Final Exam Reference Material 4/10
(continued from the previous page)
class Album
{
private:
// An album contains a dynamically allocated
// array of images.
// imagesAllocated tells the size of the images
// array. imagesInUse tells how many are in use,
// counting from images[ 0 ].
int imagesAllocated, imagesInUse;
Image *images;
// Grow the images array by doubling it.
void Grow( );
public:
Album( int initialSize = 50 ) :
images( new Image[ initialSize ] ),
imagesAllocated( initialSize ),
imagesInUse( 0 )
{
}
Album( const Album &a );
~Album( );
// The operator+= overload makes a deep copy to the
// images array.
Album &operator+=( const Image &img );
// Assignment makes a deep copy.
Album &operator=( const Album &rhs );
};
EECS 280 Winter 2019 Final Exam Reference Material 5/10
Section 3 Reference Sheet
Section 3 in the exam refers to this
material. You may assume all
necessary #include directives
and using namespace
declarations are present.
A circularly linked list is a linked list
where the last element in the list
links back to the first element. The
diagrams to the right illustrate
circularly linked lists of various
sizes.
Consider the following partial template for a circular list with an iterator. You are to write the
prev( ) and push_front( ) functions for the CircularList class and the operator++
and operator* overloads for the CircularList::Iterator class.
template
class CircularList
{
private:
struct Node
{
Node *next;
T datum;
};
// Any node can be chosen as “first”, or front
// of the list.
Node *first;
// If first is null, the prev function returns null.
// If it’s not null, it should return a pointer to the
// node whose next link == f.
Node *prev( Node *f ) const;
public:
// Create an empty list.
CircularList( ) : first( nullptr )
{
}
(continued on the next page)
EECS 280 Winter 2019 Final Exam Reference Material 6/10
(continued from the previous page)
// Push a node into the front of the list.
void push_front( const T &d );
class Iterator
{
public:
Iterator &operator++( );
T &operator*( ) const;
bool operator!=( Iterator &rhs ) const;
Iterator( ) : list( nullptr ), node_ptr( nullptr )
{
}
~Iterator( )
{
}
private:
Node *node_ptr;
// If node_ptr is not null, list will point
// to the CircularList that node is on.
// Hint: You will need this.
const CircularList *list;
friend class CircularList;
Iterator( const CircularList *cl, Node *p ) :
list( cl ), node_ptr( p )
{
}
};
Iterator begin( ) const
{
return Iterator( this, first );
}
Iterator end( ) const
{
return Iterator( );
}
};
EECS 280 Winter 2019 Final Exam Reference Material 7/10
Section 4 Reference Sheet
Section 4 in the exam refers to this material. You may assume all necessary #include
directives and using namespace declarations are present.
You are to build a Translate functor that swaps characters in a C-string. The constructor takes
two C-string arguments, the characters to be replaced and the corresponding replacement
characters. The functor then does those replacements on any C-string you give it.
Here is an example use, doing a ROT13 translation. rot13( msg ) modifies 1 the C-string msg
by rotating the alphabetic characters in it by 13 positions and returns msg .
int main( )
{
Translate rot13(
“abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”,
“nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM” );
char msg[ ] = “I love EECS 280.”;
cout << msg << endl; // Prints I love EECS 280.
cout << rot13( msg ) << endl; // Prints V ybir RRPF 280.
cout << msg << endl; // Prints V ybir RRPF 280.
cout << rot13( msg ) << endl; // Prints I love EECS 280.
}
To make it run fast, translation will be done using a translate table built by the constructor. The
translate table is an array of 256 characters corresponding to the 256 possible values for an
8-bit character. Index it by an input character taken as an unsigned number and you get the
output character.
To use a char as an unsigned number, cast it to an ( unsigned char ) . The C++ language
standard doesn’t specify whether a char should be treated as a signed (-128 to +127) or
unsigned (0 to 255) number. If you care, you have to specify.
1 Historical trivia. This will not help you solve the problem. ROT13 is a simple substitution cipher
once popular as a way of hiding spoilers, e.g., a movie ending, in Usenet newsgroup posts in
1980s and 1990s, in those dark and dreary days before the Internet. The Unix tr (translate)
utility was commonly used to do the ROT13 translation. It works the way this functor does,
using a table it builds based on the command line arguments.
EECS 280 Winter 2019 Final Exam Reference Material 8/10
Here is the functor class definition. You are to write the constructor and the operator( )
overload that makes it a functor.
class Translate
{
private:
// translateTable[ ( unsigned char )c ] tells how any
// character c on the input should be translated to the
// corresponding character on the output. If it’s not
// being replaced, that entry should equal c.
// This table is built by the constructor and then used
// by the operator( ) overload.
char translateTable[ 256 ];
public:
// The Translate constructor builds the translateTable
// array needed to translate from characters in the
// “from” C-string to the corresponding characters
// in the “to” C-string when the functor is used.
// If the two C-strings passed to the constructor differ
// in length, it should stop when it gets to the end of
// the shorter string and ignore the rest of the longer
// string. This is not an error.
// You may assume valid arguments.
Translate( const char *from, const char *to );
// The operator( ) overload should modify the C-string
// s passed to it, doing the specified replacements.
// Return that same (but modified) string.
// Do not create a new string.
char *operator( )( char *s );
};
EECS 280 Winter 2019 Final Exam Reference Material 9/10
This page intentionally left blank.
You may use this space for scratch work, but we will not grade anything here.
EECS 280 Winter 2019 Final Exam Reference Material 10/10
Order Now

Order from us and get better grades. We are the service you have been looking for.
WeCreativez WhatsApp Support
Our customer support team is here to answer your questions. Ask us anything!
👋 Hi, how can I help?