Question
Hi everyone I posted this Project # 6 before and I got the answer but the instructor add other Requirements Project #7 , please I
Hi everyone I posted this Project # 6 before and I got the answer but the instructor add other Requirements
Project #7,
please I need some help
CISP 400 C++ Programming
Project #7
For this project you will improve our template class from project #6 so that it will contain the following:
You will throw an exception of type unsigned if the index is out-of-range. We will define these in class.
Add two member functions:
a. void write ( ofstream& )const;
b. void read ( ifstream& );
These two functions will write to and read from a binary file. You should be able to write one array of type T and then read that file into another array of type T. You will use delete/new in read.
Project# 6 :
ExpandArray class For this project you will create a template class ExpandArray. It will be a fully functional type. It will contain the following: // Constructors and Destructor
ExpandArray( int i = 0, float p = .5 );
// i is the index of the first element in your ExpandArray
// p is the percentage that this ExpandArray will grow or shrink
ExpandArray( const T*, unsigned s, int i = 0, float p = .5 );
// i is the index of the first element in your ExpandArray
// p is the percentage that this ExpandArray will grow or shrink
// s is the number of items pointed to by T*
ExpandArray( const ExpandArray& );
~ExpandArray( );//[1] ExpandArray& operator=( const ExpandArray& );
// Insert the array or a pointer to an array into an out stream
friend ostream& operator<<( ostream&, const ExpandArray& );
friend ostream& operator<<( ostream&, const ExpandArray* );
// access the item at index 0 <= i < size (assuming standard indexing[2])
T& operator[]( int i );
const T& operator[]( int i ) const;
// Returns a sub array beginning at index first and going to index last-1
ExpandArray operator( )( int first, int last )const;
// Append either a new item or another ExpandArray beginning at index size
void append( const T& ); //[3]
void append( const ExpandArray& );
// return the number of items in the array
unsigned size( ) const
// Remove the item at index 0 <= i < size (assuming standard indexing)
void remove( int i );
// Remove the items from index first through index last-1
void remove( int first, int last );
// Remove the item at index size-1 (assuming standard indexing)
void remove( );
// Insert at index 0 <= i < size (assuming standard indexing)
void insert( int i, const T& );
// Insert at index 0 (assuming standard indexing)
void insert( const T& );
Expert Answer :
#ifndef __EXPAND_ARRAY_H #define __EXPAND_ARRAY_H #include
using namespace std;
template class ExpandArray{ T *data; unsigned int size_; int first; unsigned int capacity_; float exp; void expand(); void shrink(); public: // Constructors and Destructor ExpandArray( int i = 0, float p = .5 ); // i is the index of the first element in your ExpandArray // p is the percentage that this ExpandArray will grow or shrink ExpandArray( const T*, unsigned s, int i = 0, float p = .5 ); // i is the index of the first element in your ExpandArray // p is the percentage that this ExpandArray will grow or shrink // s is the number of items pointed to by T* ExpandArray( const ExpandArray& ); ~ExpandArray( );//[1] ExpandArray& operator=( const ExpandArray& ); // Insert the array or a pointer to an array into an out stream friend ostream& operator<<<>( ostream&, const ExpandArray& ); // access the item at index 0 <= i < size (assuming standard indexing[2]) T& operator[]( int i ); const T& operator[]( int i ) const; // Returns a sub array beginning at index first and going to index last-1 ExpandArray operator( )( int first, int last )const; // Append either a new item or another ExpandArray beginning at index size void append( const T& ); //[3] void append( const ExpandArray& ); // return the number of items in the array unsigned size( )const; // Remove the item at index 0 <= i < size (assuming standard indexing) void remove( int i ); // Remove the items from index first through index last-1 void remove( int first, int last ); // Remove the item at index size-1 (assuming standard indexing) void remove( ); // Insert at index 0 <= i < size (assuming standard indexing) void insert( int i, const T& ); // Insert at index 0 (assuming standard indexing) void insert( const T& ); }; template void ExpandArray::append(const T &ele){ size_++; if(size_ >= capacity_){ expand(); } data[size_-1] = ele; }
template void ExpandArray::append(const ExpandArray &oth){ for(int i = 0;i append(oth[i]); } } template const T& ExpandArray::operator[](int i)const{ return (*this)[i]; } template T& ExpandArray::operator[](int i){ if(i < 0 || i >= size_){ return (T)NULL; } return data[i]; } template unsigned ExpandArray::size()const{ return size_; } template ExpandArray::ExpandArray(int i,float p){ first = i; exp = p; capacity_ = 10; size_ = 0; data = new T[10]; }
template void ExpandArray::expand(){ capacity_ = capacity_ + (int)(capacity_*exp); T *new_data = new T[capacity_]; for(int i = 0;i new_data[i] = data[i]; } delete[] data; data = new_data; }
template ExpandArray::ExpandArray(const T *oth,unsigned s,int i,float p){ capacity_ = s + 10; size_ = s; first = i; exp = p; data = new T[capacity_]; for(int i = 0;i data[i] = oth[i]; } }
template ExpandArray::ExpandArray(const ExpandArray &oth){ *this = oth; }
template ExpandArray::~ExpandArray(){ delete[] data; }
template ExpandArray& ExpandArray::operator=(const ExpandArray &oth){ if(this == &oth){ return *this; } size_ = oth.size_; capacity_ = oth.capacity_; first = oth.first; exp = oth.exp; delete[] data; data = new T[capacity_]; for(int i = 0;i data[i] = oth.data[i]; } return *this; }
template void ExpandArray::insert(int i,const T &ele){ if(i < 0 || i >= size_){ return; } if(size_ >= capacity_){ expand(); } for(int j = size_;j > i;j--){ data[j] = data[j-1]; } data[i] = ele; size_++; }
template void ExpandArray::insert(const T &ele){ insert(0,ele); }
template
void ExpandArray::shrink(){ capacity_ = capacity_ - (int)(capacity_*exp); T *new_data = new T[capacity_]; for(int i = 0;i new_data[i] = data[i]; } delete[] data; data = new_data; }
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started