Please help fill in the code to implement an array class for characters. /* Comm
ID: 3878997 • Letter: P
Question
Please help fill in the code to implement an array class for characters. /* Comments help to know what to do in each function. Needs to be in C++
/**
* Fill the contents of the array.
* @param[in] ch Fill character*/
void fill (char ch);
/// Reverse the contents of the array such that the first element is now
/// the last element and the last element is the first element.
void reverse (void);
/**The slice() method returns a shallow copy of a portion of an array into
* a new array object selected from begin to end (end not included). The original
* array will not be modified.
* @param[in] begin The starting index
* @return A new Array object*/
Array slice (size_t begin) const;
/**
* @overload
* @param[in] begin The starting index
* @param[in] end The ending index
* @return A new Array object*/
Array slice (size_t begin, size_t end) const;
private:
//////////////////////////////////////////////////////////////////////////////
/// DEFINE ANY HELPER METHODS HERE
//////////////////////////////////////////////////////////////////////////////
/// Pointer to the actual data.
char * data_;
/// Current size of the array.
size_t cur_size_;
/// Maximum size of the array.
size_t max_size_;
};
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstddef>
using namespace std;
class Array {
private:
char * data_;
size_t cur_size_;
size_t max_size_;
public:
// Constructor
Array() {
max_size_ = 10;
cur_size_ = 0;
data_ = new char[max_size_];
}
// Destructor
~Array() {
}
void fill (char ch);
void reverse (void);
Array slice (size_t begin) const;
Array slice (size_t begin, size_t end) const;
friend ostream& operator<< (ostream &dout , Array &v);
};
void Array::fill(char i) {
if (cur_size_ == max_size_){
cout << "ERROR: Cannot insert " << i << ". Array is full";
}
else {
data_[cur_size_] = i;
cur_size_++;
}
}
void Array::reverse() {
size_t last_element_index = cur_size_ - 1;
size_t first_element_index = 0;
char temp;
while(first_element_index < last_element_index)
{
temp = data_[first_element_index];
data_[first_element_index] = data_[last_element_index];
data_[last_element_index] = temp;
first_element_index ++;
last_element_index --;
}
}
Array Array::slice (size_t begin) const{
Array newArray;
for(size_t i=begin ;i < cur_size_; i++){
newArray.data_[newArray.cur_size_] = data_[i];
newArray.cur_size_++;
}
return newArray;
}
Array Array::slice (size_t begin, size_t end) const{
Array newArray;
for(size_t i=begin ;(( i < cur_size_ )&& (i < end) ); i++){
newArray.data_[newArray.cur_size_] = data_[i];
newArray.cur_size_++;
}
return newArray;
}
ostream& operator<< (ostream &dout , Array &v){
dout << "[" << v.data_[0];
for(size_t i=1 ;i < v.cur_size_; i++){
dout << ", " << v.data_[i];
}
dout << "]";
return dout;
}
int main()
{
Array m;
//for(int i = 0; i < 5; i++)
m.fill('a');
m.fill('b');
m.fill('c');
m.fill('d');
cout << m;
m.reverse();
cout << m;
Array c = m.slice(2);
Array d = m.slice(1,3);
cout << c;
cout << d;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.