SlideShare a Scribd company logo
Using c++
I'm also using a the ide editor called CodeLite
The header and implementation file our already given to us, just need to make changes to the
main file.
Were suppose to chnage it to dynamic array (there is no upper bound on the size of the
sequence).
You need to use the files sequence2.h and sequence_exam2.cpp.
Add comments to were you made changes in the file.
#include "sequence1.h"
#include
#include
namespace main_savitch_3
// main file
{
sequence::sequence()
{
used = 0;
current_index = 0;
}
void sequence::start()
{
if (used > 0)
{
current_index = 0;
}
else
{
current_index = used;
}
}
void sequence::advance()
{
if (is_item())
{
current_index++;
}
}
void sequence::insert(const value_type& entry)
{
if (size() < CAPACITY)
{
if (!is_item())
{
start();
}
for (size_t i = used; i > current_index; i--)
{
data[i] = data[i-1];
}
data[current_index] = entry;
used++;
}
}
void sequence::attach(const value_type& entry)
{
if (size() < CAPACITY)
{
if (is_item())
{
for (size_t i = used; i > current_index + 1; i--)
{
data[i] = data[i-1];
}
data[current_index + 1] = entry;
current_index++;
used++;
}
else
{
data[used] = entry;
current_index = used;
used++;
}
}
}
void sequence::remove_current()
{
if (is_item())
{
for (size_t i = current_index; i < used; i++)
{
data[i] = data[i+1];
}
used--;
}
}
sequence::size_type sequence::size() const
{
return used;
}
bool sequence::is_item() const
{
return ((current_index >= 0) && (current_index < used) && (used != 0));
}
sequence::value_type sequence::current() const
{
if (is_item())
{
return data[current_index];
}
}
}
// FILE: sequence2.h
// CLASS PROVIDED: sequence (part of the namespace main_savitch_4)
// There is no implementation file provided for this class since it is
// an exercise from Chapter 4 of "Data Structures and Other Objects Using C++"
//
// TYPEDEFS and MEMBER CONSTANTS for the sequence class:
// typedef ____ value_type
// sequence::value_type is the data type of the items in the sequence. It
// may be any of the C++ built-in types (int, char, etc.), or a class with a
// default constructor, an assignment operator, and a copy constructor.
//
// typedef ____ size_type
// sequence::size_type is the data type of any variable that keeps track of
// how many items are in a sequence.
//
// static const size_type DEFAULT_CAPACITY = _____
// sequence::DEFAULT_CAPACITY is the initial capacity of a sequence that is
// created by the default constructor.
//
// CONSTRUCTOR for the sequence class:
// sequence(size_t initial_capacity = DEFAULT_CAPACITY)
// Postcondition: The sequence has been initialized as an empty sequence.
// The insert/attach functions will work efficiently (without allocating
// new memory) until this capacity is reached.
//
// MODIFICATION MEMBER FUNCTIONS for the sequence class:
// void resize(size_type new_capacity)
// Postcondition: The sequence's current capacity is changed to the
// new_capacity (but not less that the number of items already on the
// list). The insert/attach functions will work efficiently (without
// allocating new memory) until this new capacity is reached.
//
// void start( )
// Postcondition: The first item on the sequence becomes the current item
// (but if the sequence is empty, then there is no current item).
//
// void advance( )
// Precondition: is_item returns true.
// Postcondition: If the current item was already the last item in the
// sequence, then there is no longer any current item. Otherwise, the new
// current item is the item immediately after the original current item.
//
// void insert(const value_type& entry)
// Postcondition: A new copy of entry has been inserted in the sequence
// before the current item. If there was no current item, then the new entry
// has been inserted at the front of the sequence. In either case, the newly
// inserted item is now the current item of the sequence.
//
// void attach(const value_type& entry)
// Postcondition: A new copy of entry has been inserted in the sequence after
// the current item. If there was no current item, then the new entry has
// been attached to the end of the sequence. In either case, the newly
// inserted item is now the current item of the sequence.
//
// void remove_current( )
// Precondition: is_item returns true.
// Postcondition: The current item has been removed from the sequence, and the
// item after this (if there is one) is now the new current item.
//
// CONSTANT MEMBER FUNCTIONS for the sequence class:
// size_type size( ) const
// Postcondition: The return value is the number of items in the sequence.
//
// bool is_item( ) const
// Postcondition: A true return value indicates that there is a valid
// "current" item that may be retrieved by activating the current
// member function (listed below). A false return value indicates that
// there is no valid current item.
//
// value_type current( ) const
// Precondition: is_item( ) returns true.
// Postcondition: The item returned is the current item in the sequence.
//
// VALUE SEMANTICS for the sequence class:
// Assignments and the copy constructor may be used with sequence objects.
//
// DYNAMIC MEMORY USAGE by the List
// If there is insufficient dynamic memory, then the following functions
// throw a BAD_ALLOC exception: The constructors, insert, attach.
#ifndef MAIN_SAVITCH_SEQUENCE_H
#define MAIN_SAVITCH_SEQUENCE_H
#include // Provides size_t
namespace main_savitch_4
{
class sequence
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef double value_type;
typedef std::size_t size_type;
static const size_type DEFAULT_CAPACITY = 30;
// CONSTRUCTORS and DESTRUCTOR
sequence(size_type initial_capacity = DEFAULT_CAPACITY);
sequence(const sequence& source);
~sequence( );
// MODIFICATION MEMBER FUNCTIONS
void resize(size_type new_capacity);
void start( );
void advance( );
void insert(const value_type& entry);
void attach(const value_type& entry);
void remove_current( );
void operator =(const sequence& source);
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const;
bool is_item( ) const;
value_type current( ) const;
private:
value_type* data;
size_type used;
size_type current_index;
size_type capacity;
};
}
#endif
// FILE: sequence_exam2.cxx
// Written by: Michael Main (main@colorado.edu) - Oct 29, 1997
// Non-interactive test program for the sequence class using a dynamic array,
// with improved test for heap leaks.
//
// DESCRIPTION:
// Each function of this program tests part of the sequence class, returning
// some number of points to indicate how much of the test was passed.
// A description and result of each test is printed to cout.
// Maximum number of points awarded by this program is determined by the
// constants POINTS[1], POINTS[2]...
#include // Provides cout.
#include // Provides memcpy.
#include // Provides size_t.
#include "sequence2.h" // Provides the Sequence class with double items.
using namespace std;
using namespace main_savitch_4;
// Descriptions and points for each of the tests:
const size_t MANY_TESTS = 7;
const int POINTS[MANY_TESTS+1] = {
21, // Total points for all tests.
4, // Test 1 points
4, // Test 2 points
4, // Test 3 points
2, // Test 4 points
2, // Test 5 points
2, // Test 6 points
3 // Test 7 points
};
const char DESCRIPTION[MANY_TESTS+1][256] = {
"tests for sequence class with a dynamic array",
"Testing insert, attach, and the constant member functions",
"Testing situations where the cursor goes off the sequence",
"Testing remove_current",
"Testing the resize member function",
"Testing the copy constructor",
"Testing the assignment operator",
"Testing insert/attach when current DEFAULT_CAPACITY exceeded"
};
// **************************************************************************
// bool test_basic(const sequence& test, size_t s, bool has_cursor)
// Postcondition: A return value of true indicates:
// a. test.size() is s, and
// b. test.is_item() is has_cursor.
// Otherwise the return value is false.
// In either case, a description of the test result is printed to cout.
// **************************************************************************
bool test_basic(const sequence& test, size_t s, bool has_cursor)
{
bool answer;
cout << "Testing that size() returns " << s << " ... ";
cout.flush( );
answer = (test.size( ) == s);
cout << (answer ? "Passed." : "Failed.") << endl;
if (answer)
{
cout << "Testing that is_item() returns ";
cout << (has_cursor ? "true" : "false") << " ... ";
cout.flush( );
answer = (test.is_item( ) == has_cursor);
cout << (answer ? "Passed." : "Failed.") << endl;
}
return answer;
}
// **************************************************************************
// bool test_items(sequence& test, size_t s, size_t i, double items[])
// The function determines if the test sequence has the correct items
// Precondition: The size of the items array is at least s.
// Postcondition: A return value of true indicates that test.current()
// is equal to items[i], and after test.advance() the result of
// test.current() is items[i+1], and so on through items[s-1].
// At this point, one more advance takes the cursor off the sequence.
// If any of this fails, the return value is false.
// NOTE: The test sequence has been changed by advancing its cursor.
// **************************************************************************
bool test_items(sequence& test, size_t s, size_t i, double items[])
{
bool answer = true;
cout << "The cursor should be at item [" << i << "]" << " of the sequence ";
cout << "(counting the first item as [0]). I will advance the cursor ";
cout << "to the end of the sequence, checking that each item is correct...";
cout.flush( );
while ((i < s) && test.is_item( ) && (test.current( ) == items[i]))
{
i++;
test.advance( );
}
if ((i != s) && !test.is_item( ))
{ // The test.is_item( ) function returns false too soon.
cout << " Cursor fell off the sequence too soon." << endl;
answer = false;
}
else if (i != s)
{ // The test.current( ) function returned a wrong value.
cout << " The item [" << i << "] should be " << items[i] << ", ";
cout << " but it was " << test.current( ) << " instead. ";
answer = false;
}
else if (test.is_item( ))
{ // The test.is_item( ) function returns true after moving off the sequence.
cout << " The cursor was moved off the sequence,";
cout << " but is_item still returns true." << endl;
answer = false;
}
cout << (answer ? "Passed." : "Failed.") << endl;
return answer;
}
// **************************************************************************
// bool correct(sequence& test, size_t s, size_t cursor_spot, double items[])
// This function determines if the sequence (test) is "correct" according to
// these requirements:
// a. it has exactly s items.
// b. the items (starting at the front) are equal to
// items[0] ... items[s-1]
// c. if cursor_spot < s, then test's cursor must be at
// the location given by cursor_spot.
// d. if cursor_spot >= s, then test must not have a cursor.
// NOTE: The function also moves the cursor off the sequence.
// **************************************************************************
bool correct(sequence& test, size_t size, size_t cursor_spot, double items[])
{
bool has_cursor = (cursor_spot < size);
// Check the sequence's size and whether it has a cursor.
if (!test_basic(test, size, has_cursor))
{
cout << "Basic test of size() or is_item() failed." << endl << endl;
return false;
}
// If there is a cursor, check the items from cursor to end of the sequence.
if (has_cursor && !test_items(test, size, cursor_spot, items))
{
cout << "Test of the sequence's items failed." << endl << endl;
return false;
}
// Restart the cursor at the front of the sequence and test items again.
cout << "I'll call start() and look at the items one more time..." << endl;
test.start( );
if (has_cursor && !test_items(test, size, 0, items))
{
cout << "Test of the sequence's items failed." << endl << endl;
return false;
}
// If the code reaches here, then all tests have been passed.
cout << "All tests passed for this sequence." << endl << endl;
return true;
}
// **************************************************************************
// int test1( )
// Performs some basic tests of insert, attach, and the constant member
// functions. Returns POINTS[1] if the tests are passed. Otherwise returns 0.
// **************************************************************************
int test1( )
{
sequence empty; // An empty sequence
sequence test; // A sequence to add items to
double items1[4] = { 5, 10, 20, 30 }; // These 4 items are put in a sequence
double items2[4] = { 10, 15, 20, 30 }; // These are put in another sequence
// Test that the empty sequence is really empty
cout << "Starting with an empty sequence." << endl;
if (!correct(empty, 0, 0, items1)) return 0;
// Test the attach function to add something to an empty sequence
cout << "I am now using attach to put 10 into an empty sequence." << endl;
test.attach(10);
if (!correct(test, 1, 0, items2)) return 0;
// Test the insert function to add something to an empty sequence
cout << "I am now using insert to put 10 into an empty sequence." << endl;
test = empty;
test.insert(10);
if (!correct(test, 1, 0, items2)) return 0;
// Test the insert function to add an item at the front of a sequence
cout << "I am now using attach to put 10,20,30 in an empty sequence. ";
cout << "Then I move the cursor to the start and insert 5." << endl;
test = empty;
test.attach(10);
test.attach(20);
test.attach(30);
test.start( );
test.insert(5);
if (!correct(test, 4, 0, items1)) return 0;
// Test the insert function to add an item in the middle of a sequence
cout << "I am now using attach to put 10,20,30 in an empty sequence. ";
cout << "Then I move the cursor to the start, advance once, ";
cout << "and insert 15." << endl;
test = empty;
test.attach(10);
test.attach(20);
test.attach(30);
test.start( );
test.advance( );
test.insert(15);
if (!correct(test, 4, 1, items2)) return 0;
// Test the attach function to add an item in the middle of a sequence
cout << "I am now using attach to put 10,20,30 in an empty sequence. ";
cout << "Then I move the cursor to the start and attach 15 ";
cout << "after the 10." << endl;
test = empty;
test.attach(10);
test.attach(20);
test.attach(30);
test.start( );
test.attach(15);
if (!correct(test, 4, 1, items2)) return 0;
// All tests have been passed
cout << "All tests of this first function have been passed." << endl;
return POINTS[1];
}
// **************************************************************************
// int test2( )
// Performs a test to ensure that the cursor can correctly be run off the end
// of the sequence. Also tests that attach/insert work correctly when there is
// no cursor. Returns POINTS[2] if the tests are passed. Otherwise returns 0.
// **************************************************************************
int test2( )
{
sequence test;
size_t i;
// Put three items in the sequence
cout << "Using attach to put 20 and 30 in the sequence, and then calling ";
cout << "advance, so that is_item should return false ... ";
cout.flush( );
test.attach(20);
test.attach(30);
test.advance( );
if (test.is_item( ))
{
cout << "failed." << endl;
return 0;
}
cout << "passed." << endl;
// Insert 10 at the front and run the cursor off the end again
cout << "Inserting 10, which should go at the sequence's front." << endl;
cout << "Then calling advance three times to run cursor off the sequence ...";
cout.flush( );
test.insert(10);
test.advance( ); // advance to the 20
test.advance( ); // advance to the 30
test.advance( ); // advance right off the sequence
if (test.is_item( ))
{
cout << " failed." << endl;
return false;
}
cout << " passed." << endl;
// Attach more items until the sequence becomes full.
// Note that the first attach should attach to the end of the sequence.
cout << "Calling attach to put the numbers 40, 50, 60 ...";
cout << test.DEFAULT_CAPACITY*10 << " at the sequence's end." << endl;
for (i = 4; i <= test.DEFAULT_CAPACITY; i++)
test.attach(i*10);
// Test that the sequence is correctly filled.
cout << "Now I will test that the sequence has 10, 20, 30, ...";
cout << test.DEFAULT_CAPACITY*10 << "." << endl;
test.start( );
for (i = 1; i <= test.DEFAULT_CAPACITY; i++)
{
if ((!test.is_item( )) || test.current( ) != i*10)
{
cout << " Test failed to find " << i*10 << endl;
return 0;
}
test.advance( );
}
if (test.is_item( ))
{
cout << " There are too many items on the sequence." << endl;
return false;
}
// All tests passed
cout << "All tests of this second function have been passed." << endl;
return POINTS[2];
}
// **************************************************************************
// int test3( )
// Performs basic tests for the remove_current function.
// Returns POINTS[3] if the tests are passed. Returns POINTS[3] / 4 if almost
// all the tests are passed. Otherwise returns 0.
// **************************************************************************
int test3( )
{
// In the next declarations, I am declaring a sequence called test.
// Both before and after the sequence, I declare a small array of characters,
// and I put the character 'x' into each spot of these arrays.
// Later, if I notice that one of the x's has been changed, or if
// I notice an 'x' inside of the sequence, then the most
// likely reason was that one of the sequence's member functions accessed
// the sequence's array outside of its legal indexes.
char prefix[4] = {'x', 'x', 'x', 'x'};
sequence test;
char suffix[4] = {'x', 'x', 'x', 'x'};
// Within this function, I create several different sequences using the
// items in these arrays:
double items1[1] = { 30 };
double items2[2] = { 10, 30 };
double items3[3] = { 10, 20, 30 };
size_t i; // for-loop control variable
char *char_ptr; // Variable to loop at each character in a sequence's memory
// Build a sequence with three items 10, 20, 30, and remove the middle,
// and last and then first.
cout << "Using attach to build a sequence with 10,30." << endl;
test.attach(10);
test.attach(30);
cout << "Insert a 20 before the 30, so entire sequence is 10,20,30." << endl;
test.insert(20);
if (!correct(test, 3, 1, items3)) return 0;
cout << "Remove the 20, so entire sequence is now 10,30." << endl;
test.start( );
test.advance( );
test.remove_current( );
if (!correct(test, 2, 1, items2)) return 0;
cout << "Remove the 30, so entire sequence is now just 10 with no cursor.";
cout << endl;
test.start( );
test.advance( );
test.remove_current( );
if (!correct(test, 1, 1, items2)) return 0;
cout << "Set the cursor to the start and remove the 10." << endl;
test.start( );
test.remove_current( );
if (!correct(test, 0, 0, items2)) return 0;
// Build a sequence with three items 10, 20, 30, and remove the middle,
// and then first and then last.
cout << "Using attach to build another sequence with 10,30." << endl;
test.attach(10);
test.attach(30);
cout << "Insert a 20 before the 30, so entire sequence is 10,20,30." << endl;
test.insert(20);
if (!correct(test, 3, 1, items3)) return 0;
cout << "Remove the 20, so entire sequence is now 10,30." << endl;
test.start( );
test.advance( );
test.remove_current( );
if (!correct(test, 2, 1, items2)) return 0;
cout << "Set the cursor to the start and remove the 10," << endl;
cout << "so the sequence should now contain just 30." << endl;
test.start( );
test.remove_current( );
if (!correct(test, 1, 0, items1)) return 0;
cout << "Remove the 30 from the sequence, resulting in an empty sequence." << endl;
test.start( );
test.remove_current( );
if (!correct(test, 0, 0, items1)) return 0;
// Build a sequence with three items 10, 20, 30, and remove the first.
cout << "Build a new sequence by inserting 30, 10, 20 (so the sequence ";
cout << "is 20, then 10, then 30). Then remove the 20." << endl;
test.insert(30);
test.insert(10);
test.insert(20);
test.remove_current( );
if (!correct(test, 2, 0, items2)) return 0;
test.start( );
test.remove_current( );
test.remove_current( );
// Just for fun, fill up the sequence, and empty it!
cout << "Just for fun, I'll empty the sequence then fill it up, then ";
cout << "empty it again. During this process, I'll try to determine ";
cout << "whether any of the sequence's member functions access the ";
cout << "array outside of its legal indexes." << endl;
for (i = 0; i < test.DEFAULT_CAPACITY; i++)
test.insert(0);
for (i = 0; i < test.DEFAULT_CAPACITY; i++)
test.remove_current( );
// Make sure that the character 'x' didn't somehow get into the sequence,
// as that would indicate that the sequence member functions are
// copying data from before or after the sequence into the sequence.
char_ptr = (char *) &test;
for (i = 0; i < sizeof(sequence); i++)
if (char_ptr[i] == 'x')
{
cout << "Illegal array access detected." << endl;
return POINTS[3] / 4;
}
// Make sure that the prefix and suffix arrays still have four
// x's each. Otherwise one of the sequence operations wrote outside of
// the legal boundaries of its array.
for (i = 0; i < 4; i++)
if ((suffix[i] != 'x') || (prefix[i] != 'x'))
{
cout << "Illegal array access detected." << endl;
return POINTS[3] / 4;
}
// All tests passed
cout << "All tests of this third function have been passed." << endl;
return POINTS[3];
}
// **************************************************************************
// int test4( )
// Performs some tests of resize.
// Returns POINTS[4] if the tests are passed. Otherwise returns 0.
// **************************************************************************
int test4( )
{
sequence test;
size_t i;
char bytes[sizeof(sequence)];
char newbytes[sizeof(sequence)];
size_t mismatches;
cout << "I will now resize a sequence to a larger capacity, and then ";
cout << "attach that many items. The sequence should NOT need to ";
cout << "resize itself under this situation." << endl;
test.resize(2*test.DEFAULT_CAPACITY);
test.attach(0);
memcpy(bytes, (char *) &test, sizeof(sequence));
// At this point, I should be able to insert 2*DEFAULT_CAPACITY-1
// more items without calling resize again. Therefore, at most 1 byte
// of the object will change (the least significant byte of used).
for (i = 1; i < 2*test.DEFAULT_CAPACITY; i++)
test.attach(i);
test.start( );
memcpy(newbytes, (char *) &test, sizeof(sequence));
for (i = 0; i < 2*test.DEFAULT_CAPACITY; i++)
{
if (test.current( ) != i)
{
cout << " sequence does not contain correct items." << endl;
return 0;
}
test.advance( );
}
test.start( );
mismatches = 0;
for (i = 0; i < sizeof(sequence); i++)
if (bytes[i] != newbytes[i])
mismatches++;
if (mismatches > 1)
{
cout << " sequence was resized when it should not be." << endl;
return 0;
}
else
cout << " Test passed." << endl;
cout << "Now I will call resize(1) for the sequence, but the actual ";
cout << "sequence should not change because the sequence already has  ";
cout << test.DEFAULT_CAPACITY*2 << " items." << endl;
memcpy(bytes, (char *) &test, sizeof(sequence));
test.resize(1);
mismatches = 0;
for (i = 0; i < sizeof(sequence); i++)
if (bytes[i] != newbytes[i])
mismatches++;
if (mismatches > 0)
{
cout << " sequence was resized when it should not be." << endl;
return 0;
}
else
cout << " Test passed." << endl;
// All tests passed
cout << "All tests of this fourth function have been passed." << endl;
return POINTS[4];
}
// **************************************************************************
// int test5( )
// Performs some tests of the copy constructor.
// Returns POINTS[5] if the tests are passed. Otherwise returns 0.
// **************************************************************************
int test5( )
{
sequence original; // A sequence that we'll copy.
double items[2*original.DEFAULT_CAPACITY];
size_t i;
// Set up the items array to conatin 1...2*DEFAULT_CAPACITY.
for (i = 1; i <= 2*original.DEFAULT_CAPACITY; i++)
items[i-1] = i;
// Test copying of an empty sequence. After the copying, we change the original.
cout << "Copy constructor test: for an empty sequence." << endl;
sequence copy1(original);
original.attach(1); // Changes the original sequence, but not the copy.
if (!correct(copy1, 0, 0, items)) return 0;
// Test copying of a sequence with current item at the tail.
cout << "Copy constructor test: for a sequence with cursor at tail." << endl;
for (i=2; i <= 2*original.DEFAULT_CAPACITY; i++)
original.attach(i);
sequence copy2(original);
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy2, 2*original.DEFAULT_CAPACITY, 2*original.DEFAULT_CAPACITY-1, items)
)
return 0;
// Test copying of a sequence with cursor near the middle.
cout << "Copy constructor test: for a sequence with cursor near middle." << endl;
original.insert(2);
for (i = 1; i < original.DEFAULT_CAPACITY; i++)
original.advance( );
// Cursor is now at location [DEFAULT_CAPACITY] (counting [0] as the first spot).
sequence copy3(original);
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy3, 2*original.DEFAULT_CAPACITY, original.DEFAULT_CAPACITY, items)
)
return 0;
// Test copying of a sequence with cursor at the front.
cout << "Copy constructor test: for a sequence with cursor near middle." << endl;
original.insert(2);
original.start( );
// Cursor is now at the front.
sequence copy4(original);
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy4, 2*original.DEFAULT_CAPACITY, 0, items)
)
return 0;
// Test copying of a sequence with no current item.
cout << "Copy constructor test: for a sequence with no current item." << endl;
original.insert(2);
while (original.is_item( ))
original.advance( );
// There is now no current item.
sequence copy5(original);
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy5, 2*original.DEFAULT_CAPACITY, 2*original.DEFAULT_CAPACITY, items)
)
return 0;
// All tests passed
cout << "All tests of this fifth function have been passed." << endl;
return POINTS[5];
}
// **************************************************************************
// int test6( )
// Performs some tests of the assignment operator.
// Returns POINTS[6] if the tests are passed. Otherwise returns 0.
// **************************************************************************
int test6( )
{
sequence original; // A sequence that we'll copy.
double items[2*original.DEFAULT_CAPACITY];
size_t i;
// Set up the items array to conatin 1...2*DEFAULT_CAPACITY.
for (i = 1; i <= 2*original.DEFAULT_CAPACITY; i++)
items[i-1] = i;
// Test copying of an empty sequence. After the copying, we change the original.
cout << "Assignment operator test: for an empty sequence." << endl;
sequence copy1;
copy1 = original;
original.attach(1); // Changes the original sequence, but not the copy.
if (!correct(copy1, 0, 0, items)) return 0;
// Test copying of a sequence with current item at the tail.
cout << "Assignment operator test: for a sequence with cursor at tail." << endl;
for (i=2; i <= 2*original.DEFAULT_CAPACITY; i++)
original.attach(i);
sequence copy2;
copy2 = original;
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy2, 2*original.DEFAULT_CAPACITY, 2*original.DEFAULT_CAPACITY-1, items)
)
return 0;
// Test copying of a sequence with cursor near the middle.
cout << "Assignment operator test: for a sequence with cursor near middle." << endl;
original.insert(2);
for (i = 1; i < original.DEFAULT_CAPACITY; i++)
original.advance( );
// Cursor is now at location [DEFAULT_CAPACITY] (counting [0] as the first spot).
sequence copy3;
copy3 = original;
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy3, 2*original.DEFAULT_CAPACITY, original.DEFAULT_CAPACITY, items)
)
return 0;
// Test copying of a sequence with cursor at the front.
cout << "Assignment operator test: for a sequence with cursor near middle." << endl;
original.insert(2);
original.start( );
// Cursor is now at the front.
sequence copy4;
copy4 = original;
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy4, 2*original.DEFAULT_CAPACITY, 0, items)
)
return 0;
// Test copying of a sequence with no current item.
cout << "Assignment operator test: for a sequence with no current item." << endl;
original.insert(2);
while (original.is_item( ))
original.advance( );
// There is now no current item.
sequence copy5;
copy5 = original;
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy5, 2*original.DEFAULT_CAPACITY, 2*original.DEFAULT_CAPACITY, items)
)
return 0;
cout << "Checking correctness of a self-assignment x = x;" << endl;
original.insert(2);
original = original;
if (!correct
(original, 2*original.DEFAULT_CAPACITY, 1, items)
)
return 0;
// All tests passed
cout << "All tests of this sixth function have been passed." << endl;
return POINTS[6];
}
// **************************************************************************
// int test7( )
// Performs some basic tests of insert and attach for the case where the
// current capacity has been reached.
// Returns POINTS[7] if the tests are passed. Otherwise returns 0.
// **************************************************************************
int test7( )
{
sequence testa, testi;
double items[2*testa.DEFAULT_CAPACITY];
size_t i;
// Set up the items array to conatin 1...2*DEFAULT_CAPACITY.
for (i = 1; i <= 2*testa.DEFAULT_CAPACITY; i++)
items[i-1] = i;
cout << "Testing to see that attach works correctly when the ";
cout << "current capacity is exceeded." << endl;
for (i = 1; i <= 2*testa.DEFAULT_CAPACITY; i++)
testa.attach(i);
if (!correct
(testa, 2*testa.DEFAULT_CAPACITY, 2*testa.DEFAULT_CAPACITY-1, items)
)
return 0;
cout << "Testing to see that insert works correctly when the ";
cout << "current capacity is exceeded." << endl;
for (i = 2*testi.DEFAULT_CAPACITY; i >= 1; i--)
testi.insert(i);
if (!correct
(testi, 2*testi.DEFAULT_CAPACITY, 0, items)
)
return 0;
// All tests passed
cout << "All tests of this seventh function have been passed." << endl;
return POINTS[7];
}
int run_a_test(int number, const char message[], int test_function( ), int max)
{
int result;
cout << endl << "START OF TEST " << number << ":" << endl;
cout << message << " (" << max << " points)." << endl;
result = test_function( );
if (result > 0)
{
cout << "Test " << number << " got " << result << " points";
cout << " out of a possible " << max << "." << endl;
}
else
cout << "Test " << number << " failed." << endl;
cout << "END OF TEST " << number << "." << endl << endl;
return result;
}
// **************************************************************************
// int main( )
// The main program calls all tests and prints the sum of all points
// earned from the tests.
// **************************************************************************
int main( )
{
int sum = 0;
cout << "Running " << DESCRIPTION[0] << endl;
sum += run_a_test(1, DESCRIPTION[1], test1, POINTS[1]);
sum += run_a_test(2, DESCRIPTION[2], test2, POINTS[2]);
sum += run_a_test(3, DESCRIPTION[3], test3, POINTS[3]);
sum += run_a_test(4, DESCRIPTION[4], test4, POINTS[4]);
sum += run_a_test(5, DESCRIPTION[5], test5, POINTS[5]);
sum += run_a_test(6, DESCRIPTION[6], test6, POINTS[6]);
sum += run_a_test(7, DESCRIPTION[7], test7, POINTS[7]);
cout << "If you submit this sequence to Dora now, you will have ";
cout << sum << " points out of the " << POINTS[0];
cout << " points from this test program. ";
return EXIT_SUCCESS;
}
Solution
when it comes to IDEs, i take advantage of CodeLite (on Unbuntu and windows) and visual
Studio (on windows.) CodeLite has a very comparable characteristic set to Code::Blocks, but the
way it handles projects is a bit towards visual Studio, that is what i'm most used to.
however if you need to code the usage of a programmer's editor and the command line, rather
than an IDE, then you definately've got ScITE, Notepad++, GEdit, Programmer's Notepad, ... in
addition to the heavier responsibility, trad editors.
there is also Geany, which can be visible to be either a completely lightweight IDE or a
complicated editor.
Andy
playstation I run CodeLite on home windows both immediately (from a windows command
console or a shortcut) and launching it from a MSYS terminal session. The latter approach is
needed to get a few open source projects to construct, except you're keen/masochistic sufficient
to rework their construct systems.
PPS ScITE is the textual content editor i like at the moment. however this ballot places
Notepad++ within the lead.
The 15 most popular textual content Editors for developers

More Related Content

PDF
Implement the sequence class from Section 3.2 of the textbook. The d.pdf
PDF
(C++) Change the following program so that it uses a dynamic array i.pdf
PDF
Ive posted 3 classes after the instruction that were given at star.pdf
PDF
Files that you must write and turn in Please do not turn in.pdf
DOCX
PQTimer.java A simple driver program to run timing t.docx
DOCX
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
PDF
Given below is the code for the question. Since the test files (ment.pdf
PDF
Given below is the code for the question. Since the test files (ment.pdf
Implement the sequence class from Section 3.2 of the textbook. The d.pdf
(C++) Change the following program so that it uses a dynamic array i.pdf
Ive posted 3 classes after the instruction that were given at star.pdf
Files that you must write and turn in Please do not turn in.pdf
PQTimer.java A simple driver program to run timing t.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
Given below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdf

Similar to Using c++Im also using a the ide editor called CodeLiteThe hea.pdf (20)

PPTX
Oop lect3.pptx
PDF
How Does Kubernetes Build OpenAPI Specifications?
DOCX
all i need is these two filesCreate VectorContainer.hppCreat.docx
PDF
AnswerNote Driver class is not given to test the DoubleArraySeq..pdf
PDF
public class DoubleArraySeq implements Cloneable {    Priva.pdf
PDF
Given the following errors and class in Java- How are these errors fix.pdf
DOCX
Cmis 212 module 2 assignment
DOCX
Cmis 212 module 2 assignment
DOCX
Cmis 212 module 2 assignment
PDF
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
DOCX
Page 1 of 2 Updated on 2020-01-23 CPS 151Spring 2020 .docx
PDF
Given the following class in Java- public class ThreeTenDynArray-T- {.pdf
TXT
Salesforce, APEX Concepts
PDF
MT_01_unittest_python.pdf
PDF
vitest-en.pdf
PDF
READ BEFORE YOU START You are given a partially completed pr.pdf
PDF
svelte-en.pdf
KEY
Object-Oriented JavaScript
KEY
Object-Oriented Javascript
DOCX
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
Oop lect3.pptx
How Does Kubernetes Build OpenAPI Specifications?
all i need is these two filesCreate VectorContainer.hppCreat.docx
AnswerNote Driver class is not given to test the DoubleArraySeq..pdf
public class DoubleArraySeq implements Cloneable {    Priva.pdf
Given the following errors and class in Java- How are these errors fix.pdf
Cmis 212 module 2 assignment
Cmis 212 module 2 assignment
Cmis 212 module 2 assignment
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
Page 1 of 2 Updated on 2020-01-23 CPS 151Spring 2020 .docx
Given the following class in Java- public class ThreeTenDynArray-T- {.pdf
Salesforce, APEX Concepts
MT_01_unittest_python.pdf
vitest-en.pdf
READ BEFORE YOU START You are given a partially completed pr.pdf
svelte-en.pdf
Object-Oriented JavaScript
Object-Oriented Javascript
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
Ad

More from fashiongallery1 (20)

PDF
For this lab you will complete the class MyArrayList by implementing.pdf
PDF
Discuss the character and impact of the economic and legal revouluti.pdf
PDF
Discrete Math -Use induction to prove. The city of Inductionapolis.pdf
PDF
Describe polarization and why it is important to WLANs.Solution.pdf
PDF
Describe the four basic elements of most communication systems.So.pdf
PDF
Can someone please help me figure out how to do this Required Resou.pdf
PDF
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdf
PDF
All computer are configured for TCPIP connectivity. This exercise i.pdf
PDF
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf
PDF
Briefly Explain all these points belowA. Marketing channels VS cha.pdf
PDF
Write a class that implements the BagInterface. BagInterface should .pdf
PDF
write an equation for the transformation of the graph of y =f(x) tra.pdf
PDF
Why are helper T-cells called helper cellsThey help pathogens i.pdf
PDF
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdf
PDF
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdf
PDF
What data would illustrate whether these underlying problems are occ.pdf
PDF
1. What are distinct characteristics of baby boomers, generation X, .pdf
PDF
VERSION The cells denoted by the letter Ain the figure to.pdf
PDF
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdf
PDF
The __________ is the preeminent organization for developing and pub.pdf
For this lab you will complete the class MyArrayList by implementing.pdf
Discuss the character and impact of the economic and legal revouluti.pdf
Discrete Math -Use induction to prove. The city of Inductionapolis.pdf
Describe polarization and why it is important to WLANs.Solution.pdf
Describe the four basic elements of most communication systems.So.pdf
Can someone please help me figure out how to do this Required Resou.pdf
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdf
All computer are configured for TCPIP connectivity. This exercise i.pdf
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf
Briefly Explain all these points belowA. Marketing channels VS cha.pdf
Write a class that implements the BagInterface. BagInterface should .pdf
write an equation for the transformation of the graph of y =f(x) tra.pdf
Why are helper T-cells called helper cellsThey help pathogens i.pdf
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdf
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdf
What data would illustrate whether these underlying problems are occ.pdf
1. What are distinct characteristics of baby boomers, generation X, .pdf
VERSION The cells denoted by the letter Ain the figure to.pdf
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdf
The __________ is the preeminent organization for developing and pub.pdf
Ad

Recently uploaded (20)

PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Computing-Curriculum for Schools in Ghana
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Presentation on HIE in infants and its manifestations
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Chinmaya Tiranga quiz Grand Finale.pdf
RMMM.pdf make it easy to upload and study
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
VCE English Exam - Section C Student Revision Booklet
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
human mycosis Human fungal infections are called human mycosis..pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
O7-L3 Supply Chain Operations - ICLT Program
Computing-Curriculum for Schools in Ghana
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Anesthesia in Laparoscopic Surgery in India
Microbial diseases, their pathogenesis and prophylaxis
Final Presentation General Medicine 03-08-2024.pptx
Presentation on HIE in infants and its manifestations
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...

Using c++Im also using a the ide editor called CodeLiteThe hea.pdf

  • 1. Using c++ I'm also using a the ide editor called CodeLite The header and implementation file our already given to us, just need to make changes to the main file. Were suppose to chnage it to dynamic array (there is no upper bound on the size of the sequence). You need to use the files sequence2.h and sequence_exam2.cpp. Add comments to were you made changes in the file. #include "sequence1.h" #include #include namespace main_savitch_3 // main file { sequence::sequence() { used = 0; current_index = 0; } void sequence::start() { if (used > 0) { current_index = 0; } else { current_index = used; } } void sequence::advance() { if (is_item()) { current_index++;
  • 2. } } void sequence::insert(const value_type& entry) { if (size() < CAPACITY) { if (!is_item()) { start(); } for (size_t i = used; i > current_index; i--) { data[i] = data[i-1]; } data[current_index] = entry; used++; } } void sequence::attach(const value_type& entry) { if (size() < CAPACITY) { if (is_item()) { for (size_t i = used; i > current_index + 1; i--) { data[i] = data[i-1]; } data[current_index + 1] = entry; current_index++; used++; } else { data[used] = entry; current_index = used;
  • 3. used++; } } } void sequence::remove_current() { if (is_item()) { for (size_t i = current_index; i < used; i++) { data[i] = data[i+1]; } used--; } } sequence::size_type sequence::size() const { return used; } bool sequence::is_item() const { return ((current_index >= 0) && (current_index < used) && (used != 0)); } sequence::value_type sequence::current() const { if (is_item()) { return data[current_index]; } } } // FILE: sequence2.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_4) // There is no implementation file provided for this class since it is // an exercise from Chapter 4 of "Data Structures and Other Objects Using C++" //
  • 4. // TYPEDEFS and MEMBER CONSTANTS for the sequence class: // typedef ____ value_type // sequence::value_type is the data type of the items in the sequence. It // may be any of the C++ built-in types (int, char, etc.), or a class with a // default constructor, an assignment operator, and a copy constructor. // // typedef ____ size_type // sequence::size_type is the data type of any variable that keeps track of // how many items are in a sequence. // // static const size_type DEFAULT_CAPACITY = _____ // sequence::DEFAULT_CAPACITY is the initial capacity of a sequence that is // created by the default constructor. // // CONSTRUCTOR for the sequence class: // sequence(size_t initial_capacity = DEFAULT_CAPACITY) // Postcondition: The sequence has been initialized as an empty sequence. // The insert/attach functions will work efficiently (without allocating // new memory) until this capacity is reached. // // MODIFICATION MEMBER FUNCTIONS for the sequence class: // void resize(size_type new_capacity) // Postcondition: The sequence's current capacity is changed to the // new_capacity (but not less that the number of items already on the // list). The insert/attach functions will work efficiently (without // allocating new memory) until this new capacity is reached. // // void start( ) // Postcondition: The first item on the sequence becomes the current item // (but if the sequence is empty, then there is no current item). // // void advance( ) // Precondition: is_item returns true. // Postcondition: If the current item was already the last item in the // sequence, then there is no longer any current item. Otherwise, the new // current item is the item immediately after the original current item.
  • 5. // // void insert(const value_type& entry) // Postcondition: A new copy of entry has been inserted in the sequence // before the current item. If there was no current item, then the new entry // has been inserted at the front of the sequence. In either case, the newly // inserted item is now the current item of the sequence. // // void attach(const value_type& entry) // Postcondition: A new copy of entry has been inserted in the sequence after // the current item. If there was no current item, then the new entry has // been attached to the end of the sequence. In either case, the newly // inserted item is now the current item of the sequence. // // void remove_current( ) // Precondition: is_item returns true. // Postcondition: The current item has been removed from the sequence, and the // item after this (if there is one) is now the new current item. // // CONSTANT MEMBER FUNCTIONS for the sequence class: // size_type size( ) const // Postcondition: The return value is the number of items in the sequence. // // bool is_item( ) const // Postcondition: A true return value indicates that there is a valid // "current" item that may be retrieved by activating the current // member function (listed below). A false return value indicates that // there is no valid current item. // // value_type current( ) const // Precondition: is_item( ) returns true. // Postcondition: The item returned is the current item in the sequence. // // VALUE SEMANTICS for the sequence class: // Assignments and the copy constructor may be used with sequence objects. // // DYNAMIC MEMORY USAGE by the List
  • 6. // If there is insufficient dynamic memory, then the following functions // throw a BAD_ALLOC exception: The constructors, insert, attach. #ifndef MAIN_SAVITCH_SEQUENCE_H #define MAIN_SAVITCH_SEQUENCE_H #include // Provides size_t namespace main_savitch_4 { class sequence { public: // TYPEDEFS and MEMBER CONSTANTS typedef double value_type; typedef std::size_t size_type; static const size_type DEFAULT_CAPACITY = 30; // CONSTRUCTORS and DESTRUCTOR sequence(size_type initial_capacity = DEFAULT_CAPACITY); sequence(const sequence& source); ~sequence( ); // MODIFICATION MEMBER FUNCTIONS void resize(size_type new_capacity); void start( ); void advance( ); void insert(const value_type& entry); void attach(const value_type& entry); void remove_current( ); void operator =(const sequence& source); // CONSTANT MEMBER FUNCTIONS size_type size( ) const; bool is_item( ) const; value_type current( ) const; private: value_type* data; size_type used; size_type current_index; size_type capacity; };
  • 7. } #endif // FILE: sequence_exam2.cxx // Written by: Michael Main (main@colorado.edu) - Oct 29, 1997 // Non-interactive test program for the sequence class using a dynamic array, // with improved test for heap leaks. // // DESCRIPTION: // Each function of this program tests part of the sequence class, returning // some number of points to indicate how much of the test was passed. // A description and result of each test is printed to cout. // Maximum number of points awarded by this program is determined by the // constants POINTS[1], POINTS[2]... #include // Provides cout. #include // Provides memcpy. #include // Provides size_t. #include "sequence2.h" // Provides the Sequence class with double items. using namespace std; using namespace main_savitch_4; // Descriptions and points for each of the tests: const size_t MANY_TESTS = 7; const int POINTS[MANY_TESTS+1] = { 21, // Total points for all tests. 4, // Test 1 points 4, // Test 2 points 4, // Test 3 points 2, // Test 4 points 2, // Test 5 points 2, // Test 6 points 3 // Test 7 points }; const char DESCRIPTION[MANY_TESTS+1][256] = { "tests for sequence class with a dynamic array", "Testing insert, attach, and the constant member functions", "Testing situations where the cursor goes off the sequence", "Testing remove_current",
  • 8. "Testing the resize member function", "Testing the copy constructor", "Testing the assignment operator", "Testing insert/attach when current DEFAULT_CAPACITY exceeded" }; // ************************************************************************** // bool test_basic(const sequence& test, size_t s, bool has_cursor) // Postcondition: A return value of true indicates: // a. test.size() is s, and // b. test.is_item() is has_cursor. // Otherwise the return value is false. // In either case, a description of the test result is printed to cout. // ************************************************************************** bool test_basic(const sequence& test, size_t s, bool has_cursor) { bool answer; cout << "Testing that size() returns " << s << " ... "; cout.flush( ); answer = (test.size( ) == s); cout << (answer ? "Passed." : "Failed.") << endl; if (answer) { cout << "Testing that is_item() returns "; cout << (has_cursor ? "true" : "false") << " ... "; cout.flush( ); answer = (test.is_item( ) == has_cursor); cout << (answer ? "Passed." : "Failed.") << endl; } return answer; } // ************************************************************************** // bool test_items(sequence& test, size_t s, size_t i, double items[]) // The function determines if the test sequence has the correct items
  • 9. // Precondition: The size of the items array is at least s. // Postcondition: A return value of true indicates that test.current() // is equal to items[i], and after test.advance() the result of // test.current() is items[i+1], and so on through items[s-1]. // At this point, one more advance takes the cursor off the sequence. // If any of this fails, the return value is false. // NOTE: The test sequence has been changed by advancing its cursor. // ************************************************************************** bool test_items(sequence& test, size_t s, size_t i, double items[]) { bool answer = true; cout << "The cursor should be at item [" << i << "]" << " of the sequence "; cout << "(counting the first item as [0]). I will advance the cursor "; cout << "to the end of the sequence, checking that each item is correct..."; cout.flush( ); while ((i < s) && test.is_item( ) && (test.current( ) == items[i])) { i++; test.advance( ); } if ((i != s) && !test.is_item( )) { // The test.is_item( ) function returns false too soon. cout << " Cursor fell off the sequence too soon." << endl; answer = false; } else if (i != s) { // The test.current( ) function returned a wrong value. cout << " The item [" << i << "] should be " << items[i] << ", "; cout << " but it was " << test.current( ) << " instead. "; answer = false; } else if (test.is_item( )) { // The test.is_item( ) function returns true after moving off the sequence. cout << " The cursor was moved off the sequence,"; cout << " but is_item still returns true." << endl;
  • 10. answer = false; } cout << (answer ? "Passed." : "Failed.") << endl; return answer; } // ************************************************************************** // bool correct(sequence& test, size_t s, size_t cursor_spot, double items[]) // This function determines if the sequence (test) is "correct" according to // these requirements: // a. it has exactly s items. // b. the items (starting at the front) are equal to // items[0] ... items[s-1] // c. if cursor_spot < s, then test's cursor must be at // the location given by cursor_spot. // d. if cursor_spot >= s, then test must not have a cursor. // NOTE: The function also moves the cursor off the sequence. // ************************************************************************** bool correct(sequence& test, size_t size, size_t cursor_spot, double items[]) { bool has_cursor = (cursor_spot < size); // Check the sequence's size and whether it has a cursor. if (!test_basic(test, size, has_cursor)) { cout << "Basic test of size() or is_item() failed." << endl << endl; return false; } // If there is a cursor, check the items from cursor to end of the sequence. if (has_cursor && !test_items(test, size, cursor_spot, items)) { cout << "Test of the sequence's items failed." << endl << endl; return false; } // Restart the cursor at the front of the sequence and test items again. cout << "I'll call start() and look at the items one more time..." << endl; test.start( );
  • 11. if (has_cursor && !test_items(test, size, 0, items)) { cout << "Test of the sequence's items failed." << endl << endl; return false; } // If the code reaches here, then all tests have been passed. cout << "All tests passed for this sequence." << endl << endl; return true; } // ************************************************************************** // int test1( ) // Performs some basic tests of insert, attach, and the constant member // functions. Returns POINTS[1] if the tests are passed. Otherwise returns 0. // ************************************************************************** int test1( ) { sequence empty; // An empty sequence sequence test; // A sequence to add items to double items1[4] = { 5, 10, 20, 30 }; // These 4 items are put in a sequence double items2[4] = { 10, 15, 20, 30 }; // These are put in another sequence // Test that the empty sequence is really empty cout << "Starting with an empty sequence." << endl; if (!correct(empty, 0, 0, items1)) return 0; // Test the attach function to add something to an empty sequence cout << "I am now using attach to put 10 into an empty sequence." << endl; test.attach(10); if (!correct(test, 1, 0, items2)) return 0; // Test the insert function to add something to an empty sequence cout << "I am now using insert to put 10 into an empty sequence." << endl; test = empty; test.insert(10); if (!correct(test, 1, 0, items2)) return 0; // Test the insert function to add an item at the front of a sequence
  • 12. cout << "I am now using attach to put 10,20,30 in an empty sequence. "; cout << "Then I move the cursor to the start and insert 5." << endl; test = empty; test.attach(10); test.attach(20); test.attach(30); test.start( ); test.insert(5); if (!correct(test, 4, 0, items1)) return 0; // Test the insert function to add an item in the middle of a sequence cout << "I am now using attach to put 10,20,30 in an empty sequence. "; cout << "Then I move the cursor to the start, advance once, "; cout << "and insert 15." << endl; test = empty; test.attach(10); test.attach(20); test.attach(30); test.start( ); test.advance( ); test.insert(15); if (!correct(test, 4, 1, items2)) return 0; // Test the attach function to add an item in the middle of a sequence cout << "I am now using attach to put 10,20,30 in an empty sequence. "; cout << "Then I move the cursor to the start and attach 15 "; cout << "after the 10." << endl; test = empty; test.attach(10); test.attach(20); test.attach(30); test.start( ); test.attach(15); if (!correct(test, 4, 1, items2)) return 0; // All tests have been passed cout << "All tests of this first function have been passed." << endl; return POINTS[1];
  • 13. } // ************************************************************************** // int test2( ) // Performs a test to ensure that the cursor can correctly be run off the end // of the sequence. Also tests that attach/insert work correctly when there is // no cursor. Returns POINTS[2] if the tests are passed. Otherwise returns 0. // ************************************************************************** int test2( ) { sequence test; size_t i; // Put three items in the sequence cout << "Using attach to put 20 and 30 in the sequence, and then calling "; cout << "advance, so that is_item should return false ... "; cout.flush( ); test.attach(20); test.attach(30); test.advance( ); if (test.is_item( )) { cout << "failed." << endl; return 0; } cout << "passed." << endl; // Insert 10 at the front and run the cursor off the end again cout << "Inserting 10, which should go at the sequence's front." << endl; cout << "Then calling advance three times to run cursor off the sequence ..."; cout.flush( ); test.insert(10); test.advance( ); // advance to the 20 test.advance( ); // advance to the 30 test.advance( ); // advance right off the sequence if (test.is_item( )) { cout << " failed." << endl;
  • 14. return false; } cout << " passed." << endl; // Attach more items until the sequence becomes full. // Note that the first attach should attach to the end of the sequence. cout << "Calling attach to put the numbers 40, 50, 60 ..."; cout << test.DEFAULT_CAPACITY*10 << " at the sequence's end." << endl; for (i = 4; i <= test.DEFAULT_CAPACITY; i++) test.attach(i*10); // Test that the sequence is correctly filled. cout << "Now I will test that the sequence has 10, 20, 30, ..."; cout << test.DEFAULT_CAPACITY*10 << "." << endl; test.start( ); for (i = 1; i <= test.DEFAULT_CAPACITY; i++) { if ((!test.is_item( )) || test.current( ) != i*10) { cout << " Test failed to find " << i*10 << endl; return 0; } test.advance( ); } if (test.is_item( )) { cout << " There are too many items on the sequence." << endl; return false; } // All tests passed cout << "All tests of this second function have been passed." << endl; return POINTS[2]; } // ************************************************************************** // int test3( ) // Performs basic tests for the remove_current function.
  • 15. // Returns POINTS[3] if the tests are passed. Returns POINTS[3] / 4 if almost // all the tests are passed. Otherwise returns 0. // ************************************************************************** int test3( ) { // In the next declarations, I am declaring a sequence called test. // Both before and after the sequence, I declare a small array of characters, // and I put the character 'x' into each spot of these arrays. // Later, if I notice that one of the x's has been changed, or if // I notice an 'x' inside of the sequence, then the most // likely reason was that one of the sequence's member functions accessed // the sequence's array outside of its legal indexes. char prefix[4] = {'x', 'x', 'x', 'x'}; sequence test; char suffix[4] = {'x', 'x', 'x', 'x'}; // Within this function, I create several different sequences using the // items in these arrays: double items1[1] = { 30 }; double items2[2] = { 10, 30 }; double items3[3] = { 10, 20, 30 }; size_t i; // for-loop control variable char *char_ptr; // Variable to loop at each character in a sequence's memory // Build a sequence with three items 10, 20, 30, and remove the middle, // and last and then first. cout << "Using attach to build a sequence with 10,30." << endl; test.attach(10); test.attach(30); cout << "Insert a 20 before the 30, so entire sequence is 10,20,30." << endl; test.insert(20); if (!correct(test, 3, 1, items3)) return 0; cout << "Remove the 20, so entire sequence is now 10,30." << endl; test.start( ); test.advance( ); test.remove_current( ); if (!correct(test, 2, 1, items2)) return 0;
  • 16. cout << "Remove the 30, so entire sequence is now just 10 with no cursor."; cout << endl; test.start( ); test.advance( ); test.remove_current( ); if (!correct(test, 1, 1, items2)) return 0; cout << "Set the cursor to the start and remove the 10." << endl; test.start( ); test.remove_current( ); if (!correct(test, 0, 0, items2)) return 0; // Build a sequence with three items 10, 20, 30, and remove the middle, // and then first and then last. cout << "Using attach to build another sequence with 10,30." << endl; test.attach(10); test.attach(30); cout << "Insert a 20 before the 30, so entire sequence is 10,20,30." << endl; test.insert(20); if (!correct(test, 3, 1, items3)) return 0; cout << "Remove the 20, so entire sequence is now 10,30." << endl; test.start( ); test.advance( ); test.remove_current( ); if (!correct(test, 2, 1, items2)) return 0; cout << "Set the cursor to the start and remove the 10," << endl; cout << "so the sequence should now contain just 30." << endl; test.start( ); test.remove_current( ); if (!correct(test, 1, 0, items1)) return 0; cout << "Remove the 30 from the sequence, resulting in an empty sequence." << endl; test.start( ); test.remove_current( ); if (!correct(test, 0, 0, items1)) return 0; // Build a sequence with three items 10, 20, 30, and remove the first. cout << "Build a new sequence by inserting 30, 10, 20 (so the sequence "; cout << "is 20, then 10, then 30). Then remove the 20." << endl; test.insert(30);
  • 17. test.insert(10); test.insert(20); test.remove_current( ); if (!correct(test, 2, 0, items2)) return 0; test.start( ); test.remove_current( ); test.remove_current( ); // Just for fun, fill up the sequence, and empty it! cout << "Just for fun, I'll empty the sequence then fill it up, then "; cout << "empty it again. During this process, I'll try to determine "; cout << "whether any of the sequence's member functions access the "; cout << "array outside of its legal indexes." << endl; for (i = 0; i < test.DEFAULT_CAPACITY; i++) test.insert(0); for (i = 0; i < test.DEFAULT_CAPACITY; i++) test.remove_current( ); // Make sure that the character 'x' didn't somehow get into the sequence, // as that would indicate that the sequence member functions are // copying data from before or after the sequence into the sequence. char_ptr = (char *) &test; for (i = 0; i < sizeof(sequence); i++) if (char_ptr[i] == 'x') { cout << "Illegal array access detected." << endl; return POINTS[3] / 4; } // Make sure that the prefix and suffix arrays still have four // x's each. Otherwise one of the sequence operations wrote outside of // the legal boundaries of its array. for (i = 0; i < 4; i++) if ((suffix[i] != 'x') || (prefix[i] != 'x')) { cout << "Illegal array access detected." << endl; return POINTS[3] / 4; }
  • 18. // All tests passed cout << "All tests of this third function have been passed." << endl; return POINTS[3]; } // ************************************************************************** // int test4( ) // Performs some tests of resize. // Returns POINTS[4] if the tests are passed. Otherwise returns 0. // ************************************************************************** int test4( ) { sequence test; size_t i; char bytes[sizeof(sequence)]; char newbytes[sizeof(sequence)]; size_t mismatches; cout << "I will now resize a sequence to a larger capacity, and then "; cout << "attach that many items. The sequence should NOT need to "; cout << "resize itself under this situation." << endl; test.resize(2*test.DEFAULT_CAPACITY); test.attach(0); memcpy(bytes, (char *) &test, sizeof(sequence)); // At this point, I should be able to insert 2*DEFAULT_CAPACITY-1 // more items without calling resize again. Therefore, at most 1 byte // of the object will change (the least significant byte of used). for (i = 1; i < 2*test.DEFAULT_CAPACITY; i++) test.attach(i); test.start( ); memcpy(newbytes, (char *) &test, sizeof(sequence)); for (i = 0; i < 2*test.DEFAULT_CAPACITY; i++) { if (test.current( ) != i) {
  • 19. cout << " sequence does not contain correct items." << endl; return 0; } test.advance( ); } test.start( ); mismatches = 0; for (i = 0; i < sizeof(sequence); i++) if (bytes[i] != newbytes[i]) mismatches++; if (mismatches > 1) { cout << " sequence was resized when it should not be." << endl; return 0; } else cout << " Test passed." << endl; cout << "Now I will call resize(1) for the sequence, but the actual "; cout << "sequence should not change because the sequence already has "; cout << test.DEFAULT_CAPACITY*2 << " items." << endl; memcpy(bytes, (char *) &test, sizeof(sequence)); test.resize(1); mismatches = 0; for (i = 0; i < sizeof(sequence); i++) if (bytes[i] != newbytes[i]) mismatches++; if (mismatches > 0) { cout << " sequence was resized when it should not be." << endl; return 0; } else cout << " Test passed." << endl; // All tests passed
  • 20. cout << "All tests of this fourth function have been passed." << endl; return POINTS[4]; } // ************************************************************************** // int test5( ) // Performs some tests of the copy constructor. // Returns POINTS[5] if the tests are passed. Otherwise returns 0. // ************************************************************************** int test5( ) { sequence original; // A sequence that we'll copy. double items[2*original.DEFAULT_CAPACITY]; size_t i; // Set up the items array to conatin 1...2*DEFAULT_CAPACITY. for (i = 1; i <= 2*original.DEFAULT_CAPACITY; i++) items[i-1] = i; // Test copying of an empty sequence. After the copying, we change the original. cout << "Copy constructor test: for an empty sequence." << endl; sequence copy1(original); original.attach(1); // Changes the original sequence, but not the copy. if (!correct(copy1, 0, 0, items)) return 0; // Test copying of a sequence with current item at the tail. cout << "Copy constructor test: for a sequence with cursor at tail." << endl; for (i=2; i <= 2*original.DEFAULT_CAPACITY; i++) original.attach(i); sequence copy2(original); original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy2, 2*original.DEFAULT_CAPACITY, 2*original.DEFAULT_CAPACITY-1, items) ) return 0; // Test copying of a sequence with cursor near the middle.
  • 21. cout << "Copy constructor test: for a sequence with cursor near middle." << endl; original.insert(2); for (i = 1; i < original.DEFAULT_CAPACITY; i++) original.advance( ); // Cursor is now at location [DEFAULT_CAPACITY] (counting [0] as the first spot). sequence copy3(original); original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy3, 2*original.DEFAULT_CAPACITY, original.DEFAULT_CAPACITY, items) ) return 0; // Test copying of a sequence with cursor at the front. cout << "Copy constructor test: for a sequence with cursor near middle." << endl; original.insert(2); original.start( ); // Cursor is now at the front. sequence copy4(original); original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy4, 2*original.DEFAULT_CAPACITY, 0, items) ) return 0; // Test copying of a sequence with no current item. cout << "Copy constructor test: for a sequence with no current item." << endl; original.insert(2); while (original.is_item( )) original.advance( ); // There is now no current item. sequence copy5(original); original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy.
  • 22. if (!correct (copy5, 2*original.DEFAULT_CAPACITY, 2*original.DEFAULT_CAPACITY, items) ) return 0; // All tests passed cout << "All tests of this fifth function have been passed." << endl; return POINTS[5]; } // ************************************************************************** // int test6( ) // Performs some tests of the assignment operator. // Returns POINTS[6] if the tests are passed. Otherwise returns 0. // ************************************************************************** int test6( ) { sequence original; // A sequence that we'll copy. double items[2*original.DEFAULT_CAPACITY]; size_t i; // Set up the items array to conatin 1...2*DEFAULT_CAPACITY. for (i = 1; i <= 2*original.DEFAULT_CAPACITY; i++) items[i-1] = i; // Test copying of an empty sequence. After the copying, we change the original. cout << "Assignment operator test: for an empty sequence." << endl; sequence copy1; copy1 = original; original.attach(1); // Changes the original sequence, but not the copy. if (!correct(copy1, 0, 0, items)) return 0; // Test copying of a sequence with current item at the tail. cout << "Assignment operator test: for a sequence with cursor at tail." << endl; for (i=2; i <= 2*original.DEFAULT_CAPACITY; i++) original.attach(i); sequence copy2; copy2 = original; original.start( );
  • 23. original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy2, 2*original.DEFAULT_CAPACITY, 2*original.DEFAULT_CAPACITY-1, items) ) return 0; // Test copying of a sequence with cursor near the middle. cout << "Assignment operator test: for a sequence with cursor near middle." << endl; original.insert(2); for (i = 1; i < original.DEFAULT_CAPACITY; i++) original.advance( ); // Cursor is now at location [DEFAULT_CAPACITY] (counting [0] as the first spot). sequence copy3; copy3 = original; original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy3, 2*original.DEFAULT_CAPACITY, original.DEFAULT_CAPACITY, items) ) return 0; // Test copying of a sequence with cursor at the front. cout << "Assignment operator test: for a sequence with cursor near middle." << endl; original.insert(2); original.start( ); // Cursor is now at the front. sequence copy4; copy4 = original; original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy4, 2*original.DEFAULT_CAPACITY, 0, items) ) return 0; // Test copying of a sequence with no current item.
  • 24. cout << "Assignment operator test: for a sequence with no current item." << endl; original.insert(2); while (original.is_item( )) original.advance( ); // There is now no current item. sequence copy5; copy5 = original; original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy5, 2*original.DEFAULT_CAPACITY, 2*original.DEFAULT_CAPACITY, items) ) return 0; cout << "Checking correctness of a self-assignment x = x;" << endl; original.insert(2); original = original; if (!correct (original, 2*original.DEFAULT_CAPACITY, 1, items) ) return 0; // All tests passed cout << "All tests of this sixth function have been passed." << endl; return POINTS[6]; } // ************************************************************************** // int test7( ) // Performs some basic tests of insert and attach for the case where the // current capacity has been reached. // Returns POINTS[7] if the tests are passed. Otherwise returns 0. // ************************************************************************** int test7( ) { sequence testa, testi; double items[2*testa.DEFAULT_CAPACITY];
  • 25. size_t i; // Set up the items array to conatin 1...2*DEFAULT_CAPACITY. for (i = 1; i <= 2*testa.DEFAULT_CAPACITY; i++) items[i-1] = i; cout << "Testing to see that attach works correctly when the "; cout << "current capacity is exceeded." << endl; for (i = 1; i <= 2*testa.DEFAULT_CAPACITY; i++) testa.attach(i); if (!correct (testa, 2*testa.DEFAULT_CAPACITY, 2*testa.DEFAULT_CAPACITY-1, items) ) return 0; cout << "Testing to see that insert works correctly when the "; cout << "current capacity is exceeded." << endl; for (i = 2*testi.DEFAULT_CAPACITY; i >= 1; i--) testi.insert(i); if (!correct (testi, 2*testi.DEFAULT_CAPACITY, 0, items) ) return 0; // All tests passed cout << "All tests of this seventh function have been passed." << endl; return POINTS[7]; } int run_a_test(int number, const char message[], int test_function( ), int max) { int result; cout << endl << "START OF TEST " << number << ":" << endl; cout << message << " (" << max << " points)." << endl; result = test_function( ); if (result > 0) { cout << "Test " << number << " got " << result << " points"; cout << " out of a possible " << max << "." << endl;
  • 26. } else cout << "Test " << number << " failed." << endl; cout << "END OF TEST " << number << "." << endl << endl; return result; } // ************************************************************************** // int main( ) // The main program calls all tests and prints the sum of all points // earned from the tests. // ************************************************************************** int main( ) { int sum = 0; cout << "Running " << DESCRIPTION[0] << endl; sum += run_a_test(1, DESCRIPTION[1], test1, POINTS[1]); sum += run_a_test(2, DESCRIPTION[2], test2, POINTS[2]); sum += run_a_test(3, DESCRIPTION[3], test3, POINTS[3]); sum += run_a_test(4, DESCRIPTION[4], test4, POINTS[4]); sum += run_a_test(5, DESCRIPTION[5], test5, POINTS[5]); sum += run_a_test(6, DESCRIPTION[6], test6, POINTS[6]); sum += run_a_test(7, DESCRIPTION[7], test7, POINTS[7]); cout << "If you submit this sequence to Dora now, you will have "; cout << sum << " points out of the " << POINTS[0]; cout << " points from this test program. "; return EXIT_SUCCESS; } Solution when it comes to IDEs, i take advantage of CodeLite (on Unbuntu and windows) and visual
  • 27. Studio (on windows.) CodeLite has a very comparable characteristic set to Code::Blocks, but the way it handles projects is a bit towards visual Studio, that is what i'm most used to. however if you need to code the usage of a programmer's editor and the command line, rather than an IDE, then you definately've got ScITE, Notepad++, GEdit, Programmer's Notepad, ... in addition to the heavier responsibility, trad editors. there is also Geany, which can be visible to be either a completely lightweight IDE or a complicated editor. Andy playstation I run CodeLite on home windows both immediately (from a windows command console or a shortcut) and launching it from a MSYS terminal session. The latter approach is needed to get a few open source projects to construct, except you're keen/masochistic sufficient to rework their construct systems. PPS ScITE is the textual content editor i like at the moment. however this ballot places Notepad++ within the lead. The 15 most popular textual content Editors for developers