Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem with my C++ code with pointer and array. 1.The file implements sorted ar

ID: 3726472 • Letter: P

Question

Problem with my C++ code with pointer and array.

1.The file implements sorted array of singned char. Assume char has range [-128,127].

2. copy constructor and assignment operater provides deep-copy funtionality. Function insert()'s takes a signed char insert it in the array and make sure array is sorted in the ascending order when done.

----------Code below----.hpp file-----------------------------

// File: a2.hpp

#ifndef A2_HPP

#define A2_HPP

#include <algorithm>

class sorted_sc_array {
public:
sorted_sc_array() : size_(0), ptr_(nullptr) { }

~sorted_sc_array() { delete[] ptr_; }

// IMPLEMENT ME - copy constructor (deep copy)
sorted_sc_array(const sorted_sc_array& A){
size_ = A.size_;

ptr_ = new signed char[size_];

for(int i=0; i<size_; i++){
ptr_[i] = A.ptr_[i];
}//copy into ptr
}

// IMPLEMENT ME
sorted_sc_array& operator=(const sorted_sc_array& A){
size_ = A.size_;

ptr_ = new signed char[size_];

for(int i = 0; i < size_; ++i)
{
ptr_[i] = A.ptr_[i];
}//copy into ptr

return *this;

}

// RETURNS SIZE OF THE ARRAY (i.e. HOW MANY ELEMENTS IT STORES)
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 ME: AFTER INSERT COMPLETES THE ARRAY MUST BE IN ASCENDING ORDER
void insert(signed char c){
//add new element to array, keep them ascending order


//temp char array
signed char* newPtr = new signed char[size_+1];

//compare c and input it to correct posistion
int i ;
for (i = 0; i < size_; ++i)
{
/* code */
if(ptr_[i]>c)
newPtr[i]=c;
else
newPtr[i]=ptr_[i];
}

if(i==size_)
newPtr[size_]=c;

else{
for (int j = i; j < size_; ++j)
{
/* code */
newPtr[j+1]=ptr_[j];
}
}

delete [] ptr_;

//size_ = size_+1;
//ptr_ = new signed char[size_];
// ptr_ = newPtr;
}

private:
int size_; // size of the array
signed char* ptr_; // pointer to the array

}; // class sorted_sc_array

#endif // A2_HPP

/-------------------following is the code given to test my code------------------------

/***
* 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

When excute the program, if my code worked correctly is should print pass to the screen but I'm seeing false and don't know why. Please explain and fix my code. Thanks!

Explanation / Answer

if (!res || (A.size() != 255))  

This is the condition that executes the print statement that prints "fail" on the screen. Hence, this statement bust be returning true in your case. I debugged the code and found out that A.size() != 255 was returning true because A.size() was returning 0. Hence, backtracking, I found out that in the for loop

for (signed char c = -128; c < 127; ++c) T.insert(c);

The value of T.size() was 0 all the way through. (before the T.insert(c); statement as well as after the T.insert(c); statement). Hence, there was some issue in the insert function. So, I checked the insert function. Have a look at the last four lines of this function.

delete [] ptr_;

//size_ = size_+1;
//ptr_ = new signed char[size_];
// ptr_ = newPtr;

The last three lines have been commented out. Hence, the size_ and ptr_ properties are not being updated. I tried uncommenting these lines. And the output was pass.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote