When running the program at the destructor an exception is being thrown. Can someone help me out?
vararray.h:
#ifndef VARARRAY_H_
#define VARARRAY_H_
class varArray {
public:
varArray(); // void constructor
int arraySize() const { return size; } // returns the
size of the array
int check(double number); // returns index of
element containg "number" or -1 if none
void addNumber(double); // adds number to the
array
void removeNumber(double); // deletes the number from
the array
void output(); // prints the values of the array
// big three
varArray(const varArray&); // copy
constructor
varArray& operator=(const varArray&); //
overloaded assignment
~varArray(); // destructor
private:
double *dArray; // pointer to the dynamically
allocated array
int size; // array size
};
#endif /* VARARRAY_H_ */
VarArray.cpp:
#include <iostream>
#include "vararray.h"
using std::cin; using std::cout; using std::endl;
varArray::varArray() {
dArray = new double[0];
size = 0;
}
int varArray::check(double number) {
int index = -1;
for (int i = 0; i < size; ++i) {
if (dArray[i] == number) {
index = i;
break;
}
}
return index;
}
void varArray::addNumber(double number) {
int tmp = -1;
for (int i = 0; i <= size; i++) {
if (number == dArray[i])
return;
else
tmp = 1;
}
if (tmp == 1) {
double *temp = new double[size +
1];
for (int i = 0; i < size; i++)
{
temp[i] =
dArray[i];
}
temp[size] = number;
delete[] dArray;
dArray = temp;
++size;
}
}
void varArray::removeNumber(double number) {
bool found = false;
int tmp;
for (int i = 0; i < size; i++) {
if (dArray[i] == number) {
found =
true;
tmp = i;
}
}
if (found == true) {
--size;
double *temp2 = new
double[size];
for (int i = 0; i <= size; i++)
{
if (dArray[i] !=
number) {
if (i >= number)
temp2[i - 1] =
dArray[i];
else
temp2[i] = dArray[i];
}
}
delete[]dArray;
dArray = temp2;
}
}
void varArray::output() {
cout << endl;
for (int i = 0; i < size; ++i) {
cout << dArray[i] << "
";
}
cout << endl;
}
varArray::varArray(const varArray& org) {
size = org.size;
dArray = new double[size];
for (int i = 0; i < size; i++)
dArray[i] = org.dArray[i];
}
varArray::~varArray() {
delete[] dArray; size = 0;
}
varArray& varArray::operator=(const varArray& tmp)
{
if (this == &tmp)
return *this;
size = tmp.size;
delete[] dArray;
dArray = new double[size];
for (int i = 0; i < size; ++i) {
dArray[i] = tmp.dArray[i];
}
return *this;
}
VarMain.cpp:
#include <iostream>
#include "vararray.h"
using std::cout; using std::endl; using std::cin;
int main()
{
double i, k, n;
varArray object;
cout << "Enter the size of array : ";
cin >> n;
cout << "Enter the array elements : ";
for (i = 0; i < n; i++)
{
cin >> k;
object.addNumber(k);
}
cout << "Enter the number of elements to be
deleted : ";
cin >> n;
cout << "Enter the elements to delete : ";
for (i = 0; i < n; i++)
{
cin >> k;
object.removeNumber(k);
}
cout << "Displaying the contents of the array :
";
object.output();
}
// vararray.h
#ifndef VARARRAY_H_
#define VARARRAY_H_
class varArray{
public:
varArray(); // void constructor
int arraySize() const {return size;} // returns the size of the array
int check(double number); // returns index of element containg "number" or -1 if none
void addNumber(double); // adds number to the array
void removeNumber(double); // deletes the number from the array
void output(); // prints the values of the array
// big three
varArray(const varArray&); // copy constructor
varArray& operator=(const varArray&); // overloaded assignment
~varArray(); // destructor
private:
double *dArray; // pointer to the dynamically allocated array
int size; // array size
};
#endif /* VARARRAY_H_ */
//end of vararray.h
// varArray.cpp
#include <iostream>
#include "vararray.h"
using std::cin; using std::cout; using std::endl;
varArray::varArray()
{
dArray = new double[0];
size = 0;
}
varArray::varArray(const varArray &org)
{
size = org.size;
dArray = new double[size];
for(int i=0;i<size;i++)
dArray[i] = org.dArray[i];
}
varArray& varArray::operator=(const varArray& other)
{
if(this != &other)
{
delete dArray; // deallocate the earlier allocated array
size = other.size;
dArray = new double[size];
for(int i=0;i<size;i++)
dArray[i] = other.dArray[i];
}
return *this;
}
varArray::~varArray()
{
delete dArray;
size = 0;
}
void varArray::output()
{
cout<<endl;
for (int c = 0; c < size; c++)
cout << dArray[c] << " ";
cout<<endl;
}
int varArray::check(double number)
{
int index = -1;
// Loops until end of the array
for (int c = 0; c < size; c++)
{
// Checks if current index position value of the array
// is equals to the parameter number then return
// c value as found index position
if (dArray[c] == number)
return c;
}// End of for loop
// End of the loop returns -1 for not found
return index;
}
void varArray::addNumber(double number)
{
// Calls the function to check whether the number is available
int found = check(number);
// Checks if the found status is -1 then number not found
if (found == -1)
{
double *temp = new double[size + 1];
for (int c = 0; c < size; c++)
temp[c] = dArray[c];
// Increase the size by one
size++;
delete dArray; // deallocating the array
dArray = temp;
// Assigns the parameter number to existing array last position
dArray[size - 1] = number;
}// End of if condition
// Otherwise number found
else
cout << "\n The number found at: " << found << "\n Cannot add duplicate number."<<endl;
}
void varArray::removeNumber(double number)
{
// Calls the function to check whether the number is available
int found = check(number);
// Checks if the found status is not -1 then number found
if (found != -1)
{
double *temp = new double[size];
for (int c = found; c < size; c++)
dArray[c] = dArray[c + 1];
// Loops till end of the existing array
// Assign each element of the existing array to temp array
for (int c = 0; c < size; c++)
temp[c] = dArray[c];
// Decrease the size by one
size--;
delete dArray; // deallocating the array
// Resize the existing array with new size
dArray = new double[size];
// Loops till end of the array minus one term
// and assign each element of the temp array to existing array
for (int c = 0; c < size; c++)
dArray[c] = temp[c];
}// End of if condition
// Otherwise number not found
else
cout << "\n The number " << number << " not found."<<endl;
}
//end of varArray.cpp
// varMain.cpp
#include <iostream>
#include "vararray.h"
using std::cout; using std::endl; using std::cin;
int main(){
double i, k, n;
varArray object;
cout << "Enter the size of array : ";
cin >> n;
cout << "Enter the array elements : ";
for (i = 0; i < n; i++)
{
cin >> k;
object.addNumber(k);
}
cout << "Enter the number of elements to be deleted : ";
cin >> n;
cout << "Enter the elements to delete : ";
for (i = 0; i < n; i++)
{
cin >> k;
object.removeNumber(k);
}
cout << "Displaying the contents of the array : ";
object.output();
return 0;
}
//end of varMain.cpp
Output:
When running the program at the destructor an exception is being thrown. Can someone help me out?...
This is c++ programming and here is my code. I am getting an exception thrown on the destructor for permanentworker. Can you tell me why I am getting this exception? #include <iostream> #pragma warning(disable:4996) class PermanentWorker { private: char *name; int salary; public: PermanentWorker(const char* nam, int money) : salary(money) { name = new char[strlen(nam) + 1]; strcpy(name, nam); } int getPay() const { return salary; ...
Who could write the array.cpp file ? //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...
Variable Size Array with Classes, Testing. Study Code and Object Definition Windows of Microsoft Visual Studio described here. As you work on the below project, demonstrate to the instructor the usage of this feature. Create a project titled Lab11_VarArrayTest. Implement the dynamically expanding and contracting array of doubles described in the previous lab as a class. You should use this class definition. The class attributes are a pointer to the dynamically allocated array dAarray and the array size size This...
Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...
vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector { public: Vector( int initsize = 0 ) : theSize( initsize ), theCapacity( initsize + SPARE_CAPACITY ) { objects = new T[ theCapacity ]; } Vector( const Vector & rhs ) : theSize( rhs.theSize), theCapacity( rhs.theCapacity ), objects( 0 ) { objects = new T[ theCapacity ]; for( int k = 0; k < theSize; ++k) objects[ k ] = rhs.objects[ k...
Redesign your Array class from lab6 as a class template to work with the application below. write your overloaded output stream operator as an inline friend method in the class declaration. Include the class template header file in your application as below. #include "Array.h" main() { Array<char> c(3); c.setValue(0,'c'); c.setValue(1,'s'); c.setValue(2,'c'); cout << c; Array<int> i(3); i.setValue(0,1); i.setValue(1,2); i.setValue(2,5); cout << i; Array<int> j(3); j.setValue(0,10); j.setValue(1,20); j.setValue(2,50); cout << j; Array<int> ij; ij = i + j; cout << ij;...
How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the linear doesn't. Here's my code. #include <iostream> using namespace std; void selectionSort(int[], int, int& ); void showSelection(int[], int); void sortArray(int[], int, int&); void showArray(const int[], int); int main() { int values[25] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24...
Need help with my ksmall program. I get an error saying segmentation dump. Thanks #include<iostream> using namespace std; int ksmall(int*, int, int , int); void swap(int*, int*); int main() { int SIZE = 10; int target; int begining=0; int ending=SIZE-1; int *array1= new int[SIZE]; cout << "Enter 10 integers: " << endl; for (int i=0; i<SIZE; i++) { cin>>array1[i]; } cout << " What is the Kth smallest number...
Having to repost this as the last answer wasn't functional. Please help In the following program written in C++, I am having an issue that I cannot get the reducdedForm function in the Rational.cpp file to work. I cant figure out how to get this to work correctly, so the program will take what a user enters for fractions, and does the appropriate arithmetic to the two fractions and simplifies the answer. Rational.h class Rational { private: int num; int...
read in numbers into array , print out, sort - my program reads in and prints out but crashes before the sort - crashes after it prints out the scores - *important I have to use pointers!!!! c++ #include <iostream> using namespace std; void sortarray(int *tarray, int ); void displayarray(int *[], int); void averageshowarray(int *[], int); int main() { int Size; int count; cout << "enter the size: " << endl; cin >> Size; int...