Implement the Functions in the C++ code below 1) No other headers are allowed ex
ID: 3727039 • Letter: I
Question
Implement the Functions in the C++ code below
1) No other headers are allowed except <algorithm>
2) Must have no memory leak
3) The file implements sorted array of singned char. Assume char has range [-128,127]
4) May modified any part of the .hpp file. (Modification are not allowed for the .cpp file)
Goal: Implement copy constructor and assignment operater to provide deep-copy funtionality. Functioninsert()'s job is to take a signed char insert it in the array and make sure array is sorted in the ascending order when done.
/----------------------------------------------------------------------------------------------------------------------------
#include <algorithm>
class sorted_sc_array {
public:
sorted_sc_array() : size_(0), ptr_(nullptr) { }
~sorted_sc_array() { delete[] ptr_; }
// IMPLEMENT
sorted_sc_array(const sorted_sc_array& A);
// IMPLEMENT
sorted_sc_array& operator=(const sorted_sc_array& A);
// RETURNS SIZE OF THE ARRAY
int size() const { return size_; }
// RETURNS RAW POINTER TO THE ACTUAL DATA, CAN BE INVOKED AT ANY TIME
const signed char* data() const { return ptr_; }
// IMPLEMENT: AFTER INSERT COMPLETES THE ARRAY MUST BE IN ASCENDING ORDER
void insert(signed char c);
private:
int size_; // size of the array
signed char* ptr_; // pointer to the array
};
/----------------------the following code if for testing only, output should be 'pass' if the code works.---this file cannot be modified----------------
/***
* File: a2.cpp
// DO NOT EDIT THIS FILE !!!
// YOUR CODE MUST BE CONTAINED IN a2.hpp ONLY
#include <iostream>
#include "a2.hpp"
int main(int argc, char* argv[]) {
sorted_sc_array A;
{
sorted_sc_array T;
for (signed char c = -128; c < 127; ++c) T.insert(c);
T = T;
sorted_sc_array V = T;
A = V;
}
const auto first = A.data();
const auto last = first + A.size();
auto size = A.size();
bool res = std::is_sorted(first, last);
if (!res || (A.size() != 255)) std::cout << "fail";
else std::cout << "pass";
std::cout << std::endl;
return 0;
} // main
Please attach a Vargrind report at the end with the code.
Thanks
Explanation / Answer
here is your a2.hpp : ------------>>>>>>>
#include <algorithm>
#define nullptr NULL
class sorted_sc_array {
public:
sorted_sc_array() : size_(0) {
capacity_ = 10;
ptr_ = new signed char[capacity_];
}
~sorted_sc_array() { delete[] ptr_; }
// IMPLEMENT
sorted_sc_array(const sorted_sc_array& A){
*this = A;
}
// IMPLEMENT
sorted_sc_array& operator=(const sorted_sc_array& A){
size_ = A.size_;
capacity_ = A.capacity_;
ptr_ = new signed char[capacity_];
for(int i = 0;i<size_;i++){
ptr_[i] = A.ptr_[i];
}
return *this;
}
// RETURNS SIZE OF THE ARRAY
int size() const { return size_; }
// RETURNS RAW POINTER TO THE ACTUAL DATA, CAN BE INVOKED AT ANY TIME
const signed char* data() const { return ptr_; }
// IMPLEMENT: AFTER INSERT COMPLETES THE ARRAY MUST BE IN ASCENDING ORDER
void insert(signed char c){
if(size_ >= capacity_){
doubleSize();
}
for(int i = 0;i<size_;i++){
if(ptr_[i] > c){
signed char temp = ptr_[i];
ptr_[i] = c;
c = temp;
int j = i+1;
for(j;j<size_;j++){
temp = ptr_[j];
ptr_[j] = c;
c = temp;
}
ptr_[size_] = c;
return;
}
}
}
private:
void doubleSize(){
capacity_ = capacity_*2;
signed char *temp = new signed char[capacity_];
for(int i = 0;i<size_;i++){
temp[i] = ptr_[i];
}
delete[] ptr_;
ptr_ = temp;
}
int capacity_;
int size_; // size of the array
signed char* ptr_; // pointer to the array
};