I have a C ++ program that does the following: get an unsorted array of integers and divide it into subarrays in ascending order. eg. my array is 1,2,3, -2,4 Thus, the output will be: 1,2,3 // sub array 1 -2,4 // sub array 2
the way I demanded to do this is to allocate an array of pointers, and each cell in this array will point to an additional array (also dynamic) containing one series. Also, I am not allowed to change the "main" function
I now have a "print" method that prints subarrays using operator overloading.
I checked and found that the printing method worked fine, but in this piece of code:
cout << "\ns3: \n";
print(s3);
s3 = s1;
cout << "\ns3 again: \n";
print(s3);
print (s3) prints fine, but after the assignment statement when I call print (s3) again, I get the output file. after some dubugging, I think it came out to the destructor something.
I really understand this if someone can point out and identify my problem and come up with a solution. of course i will give the source code
Update
I want to explain myself a little better: after each call to "print", when I trace, the first thing that is done before the "print" is the copy constructor. when I set a breakpoint in the copy constructor, I see that the "src" pointer in the function signature (copy constructor function) points to the correct location (the first element in the array), but after assigning s3 = s1, when I print (s3) again pointer in copy constructor is undesirable
**
here is the code:
#include <iostream>
using namespace std;
typedef long int * LONG_INT_PTR;
class SeriesArray
{
private:
LONG_INT_PTR *stable;
int *count_arr;
int count_size;
int size;
public:
SeriesArray::SeriesArray(LONG_INT_PTR arr, int n );
SeriesArray::SeriesArray(const SeriesArray& src );
SeriesArray::~SeriesArray();
long int SeriesArray::get_entry(int i,int j) const;
SeriesArray& SeriesArray::operator=(const SeriesArray& src);
friend ostream& operator<<(ostream& stream, const SeriesArray& src);
};
SeriesArray::SeriesArray(LONG_INT_PTR arr, int n )
{
size=n;
int j=0;
int s=0;
count_size=0;
count_arr=new int[size];
for (int i=0;i<size;i++)
{
if (arr[i]<arr[i+1])
count_size++;
else
{
count_size++;
count_arr[j]=count_size;
j++;
count_size=0;
}
}
size=j;
stable=new LONG_INT_PTR[size];
for (int i=0;i<size;i++)
{
stable[i]=new long int[count_arr[i]];
for (int k=0;k<count_arr[i];k++)
{
stable[i][k]=arr[s];
s++;
}
}
}
SeriesArray::SeriesArray(const SeriesArray& src )
{
size=src.size;
count_arr=new int[size];
stable=new LONG_INT_PTR[size];
for (int i=0;i<size;i++)
{
count_arr[i]=src.count_arr[i];
stable[i]=new long int[count_arr[i]];
}
memcpy(this->stable,src.stable,src.size*sizeof(long int));
}
SeriesArray::~SeriesArray()
{
for (int i=0;i<size;++i)
delete[] stable[i];
delete[] this->count_arr;
count_size=0;
}
long int SeriesArray::get_entry(int i,int j) const
{
if (i<this->count_size&&this->stable[i][j]!=NULL)
return (this->stable[i][j]);
else return NULL;
}
SeriesArray& SeriesArray::operator=(const SeriesArray& src)
{
if (this==&src)
return *this;
if (stable!=NULL)
delete[] stable;
if (count_arr!=NULL)
delete[] count_arr;
size=src.size;
count_arr=new int[src.size];
memcpy(count_arr,src.count_arr,src.size*sizeof(int));
stable=new LONG_INT_PTR[size];
for (int i=0;i<size;i++)
{
stable[i]=new long int[src.count_arr[i]];
}
memcpy(stable,src.stable,src.size*sizeof(long int));
return *this;
}
ostream& operator<<(ostream& stream,const SeriesArray& src)
{
for (int i=0;i<src.size;i++)
{
for (int j=0;j<src.count_arr[i];j++)
{
stream << " " << src.stable[i][j] << " " ;
}
stream << "\n\n\n" ;
}
return stream;
}
void print(SeriesArray src)
{
cout << src;
cout << "\n";
}
int main()
{
long int arr1[20] = {23, 91, -71, -63, 22, 55, 51, 73, 17, -19,
-65, 44, 95, 66, 82, 85, 97, 30, 54, -34};
long int arr2[10] = {0, 1, -7, -6, 2, 5, 6, 7, 1, -1};
int count[20], i, j, n =20, sno;
long int *parr[20];
SeriesArray s1(arr1, 20);
SeriesArray s2(arr2, 10);
SeriesArray s3(arr2, 10);
cout << "\narr1:\n";
for(i=0; i < 20; i++)
cout << " " << arr1[i] << " ";
cout << "\n";
cout << "\n\ntable:\n";
print(s1);
cout << "\narr2\n";
for(i=0; i < 10; i++)
cout << " " << arr2[i] << " ";
cout << "\n";
cout << "\n\ntable:\n";
print(s2);
cout << "\ns3: \n";
print(s3);
s3 = s1;
cout << "\ns3 again: \n";
print(s3);
cout << "\ns1 again: \n";
print(s1);
cin>>i;
return 0;
}