Question

When running the program at the destructor  an exception is being thrown. Can someone help me out?...

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();
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
When running the program at the destructor  an exception is being thrown. Can someone help me out?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is c++ programming and here is my code. I am getting an exception thrown on...

    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...

    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;...

  • Please help me finish my code. Before the final pause in the main function, create an...

    Please help me finish my code. Before the final pause in the main function, create an ArrayList of another type such as double, char, short, bool, float, etc. Your choice. Add five data items to your array as we did for the int and string array lists. Print them out with a for loop as we did before. Print out the count and capacity of your new array list. Source.cpp #include #include #include #include "ArrayList.h" using namespace std; /// Entry...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    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...

    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...

  • Variable Size Array with Classes, Testing. Study Code and Object Definition Windows of Microsoft ...

    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...

  • Redesign your Array class from lab6 as a class template to work with the application below....

    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;...

  • Sometimes when I sit in restaurants waiting on food, I request the children’s menu to play...

    Sometimes when I sit in restaurants waiting on food, I request the children’s menu to play the games. One of my favorites is the word puzzle in which you search for words hidden in a scramble of letters. I began to work out a solution to this game programmatically. That seemed a little simple, so I decided to have you solve the problem of a scramble of integers, finding the sub-row or sub-column which sums to another integer. You will...

  • I need this done in C++ Can someone help me get my code from crashing. The...

    I need this done in C++ Can someone help me get my code from crashing. The code compiles but it can be easily crashed with user input. The rubric and code below Instructions: Write a program that scores the following data about a soccer player in a structure:            Player’s Name            Player’s Number            Points Scored by Player      The program should keep an array of 12 of these structures. Each element is for a different player on a...

  • How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the lin...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT