Question

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

  1. 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 class contains two groups of methods:

    • Member functions output(), check() addNumber() and removeNumber() have exactly the same functionality as described in the previous lab.
    • copy constructor, overloaded assignment and destructor ensure correct handling of the objects with dynamically allocated members.

    Your code should work with this test program. It is designed to test your implementation of varArray class.

    You are free to modify the class and the test program but you cannot change the principle of dynamic allocation of the array (i.e. you cannot make the member variables automatic) or remove testing of all the methods from the test program.

  2. Variable Size Array with Classes, Implementation. Create a project titled Lab11_VarArrayClasses. Using the class implemented in the first part of this lab, implement the functionality of the second part of the previous lab. That is, write a program that asks the user to input numbers to add to and remove from the array and then prints its contents.

    Test File
    
    
    #include <iostream>
    #include "vararray.h"
    
    using std::cout; using std::endl;
    
    
    void testfunc(varArray); // function to test pass-by-value for varArray
    
    int main(){
    
       varArray a1;
    
       // testing regular member functions
       a1.addNumber(1.);
       a1.addNumber(2.);
       a1.addNumber(3.);
       a1.addNumber(3.); // trying to add duplicate, should not add it
       
       cout << "a1 size is after number addition is: " << a1.arraySize() << endl;
    
       if(a1.check(1.) != -1) // check() returns -1 if number not present
          cout << "1 is present in the array" << endl;
    
       if(a1.check(5.) != -1)
          cout << "5 is present in the array" << endl;
    
       cout << "a1 before removal of 2.0: "; a1.output();
       a1.removeNumber(2.);
       cout << "a1 after removal of 2.0: "; a1.output();
    
       // uncomment this when you debugged the first part
       /*
       testfunc(a1); // testing copy constructor
       
       cout << "a1 after testfunc call: ";
       a1.output(); // if destructor is implemented correctly
                    // this should print properly after testfunc completes
    
       varArray a2, a3;
    
       a3=a2=a1; // testing stackability of the overloaded assignment
       cout << "a3 after stackable assingment: ";
       a3.output();
    
       a3=a3; // testing protection against self-assingment
       cout << "a3 after self-assignment: ";
       a3.output();
       */
    }
    
    
    /*
    // tests pass-by-value for object of class varArray
    void testfunc(varArray va){ // copy constructor is invoked on "va"
        cout << "parameter va: "; 
        va.output(); 
    } // destructor is invoked when "va" goes out of scope
    */
    
    Header
    
    
    
    
    #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_ */
    
    Definitions (another .cpp file) from last lab
    
    
    
    // prints the values in "arrayPtr" of "size"
    void output(double *arrayPtr, int size)
    {
            cout << "\n Your numbers: ";
            // Loops till end of the array and displays each element
            for (int c = 0; c < size; c++)
                    cout << arrayPtr[c] << " ";
    }// End of function output
    
     // returns the index of the element in "arrayPtr" of "size"
     // that corresponds to the element holding "number"
     // if number is not in the array, returns -1
    int check(double *arrayPtr, double number, int size)
    {
            // Initially found index is -1 for not found
            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 (arrayPtr[c] == number)
                            return c;
            }// End of for loop
             // End of the loop returns -1 for not found
            return index;
    }// End of function check
    
     // adds "number" to the array pointed to by "arrayPtr" of "size".
     // if the number is not already there, if "number" is there - no action
     // Note, the size of the array is thus increased.
    void addNumber(double *& arrayPtr, double number, int &size)
    {
            // Calls the function to check whether the number is available
            // in the array or not
            // Stores the return found status
            int found = check(arrayPtr, number, size);
    
            // Checks if the found status is -1 then number not found
            if (found == -1)
            {
                    // Creates a temporary array with existing size plus one
                    double *temp = new double[size + 1];
    
                    // Loops till end of the array and assign
                    // each element of the existing array to temp
                    for (int c = 0; c < size; c++)
                            temp[c] = arrayPtr[c];
    
                    // Increase the size by one
                    size++;
    
                    delete[] arrayPtr; // deallocating the array
    
                    // Resize the existing array with new size
                    arrayPtr = new double[size];
    
                    // Loops till end of the array minus one term
                    // and assign each element of the temp to existing array
                    arrayPtr = temp;
    
                    // Assigns the parameter number to existing array last position
                    arrayPtr[size - 1] = number;
            }// End of if condition
    
             // Otherwise number found
            else
                    cout << "\n The number found at: " << found << "\n Cannot add duplicate number.";
    }// End of function addNumber
    
     // removes a "number" from the "arrayPtr" of "size".
     // if "number" is not there -- no action
     // note, "size" changes
    void removeNumber(double *& arrayPtr, double number, int &size)
    {
            // Calls the function to check whether the number is available
            // in the array or not
            // Stores the return found status
            int found = check(arrayPtr, number, size);
    
            // Checks if the found status is not -1 then number found
            if (found != -1)
            {
                    // Creates a temporary array with existing size plus one
                    double *temp = new double[size];
    
                    // Shift each element to left from found index position to end of the array
    
                    // Loops from found index position to end of the array
                    // and assign each next index position element to current index position
                    for (int c = found; c < size; c++)
                            arrayPtr[c] = arrayPtr[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] = arrayPtr[c];
    
                    // Decrease the size by one
                    size--;
    
                    delete[] arrayPtr; // deallocating the array
    
                    // Resize the existing array with new size
                    arrayPtr = 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++)
                            arrayPtr[c] = temp[c];
            }// End of if condition
    
             // Otherwise number not found
            else
                    cout << "\n The number " << number << " not found.";
    }// End of function removeNumber
    
    
     
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// varArray.h

#ifndef VARARRAY_H_

#define VARARRAY_H_

#include <iostream>

using namespace std;

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 "varArray.h"

varArray::varArray()

{

       dArray = NULL;

       size = 0;

}

varArray::varArray(const varArray &other)

{

       size = other.size;

       dArray = new double[size];

       for(int i=0;i<size;i++)

             dArray[i] = other.dArray[i];

}

varArray& varArray::operator=(const varArray& other)

{

       if(this != &other)

       {

             if(dArray != NULL)

                    delete [] dArray;

             size = other.size;

             dArray = new double[size];

             for(int i=0;i<size;i++)

                    dArray[i] = other.dArray[i];

       }

       return *this;

}

varArray::~varArray()

{

       if(dArray != NULL)

             delete[] dArray;

}

// prints the values in "arrayPtr" of "size"

void varArray::output()

{

        cout << "\n Your numbers: ";

        // Loops till end of the array and displays each element

        for (int c = 0; c < size; c++)

                cout << dArray[c] << " ";

}// End of function output

// returns the index of the element in "arrayPtr" of "size"

// that corresponds to the element holding "number"

// if number is not in the array, returns -1

int varArray::check(double number)

{

        // Initially found index is -1 for not found

        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;

}// End of function check

// adds "number" to the array pointed to by "arrayPtr" of "size".

// if the number is not already there, if "number" is there - no action

// Note, the size of the array is thus increased.

void varArray::addNumber(double number)

{

       // Calls the function to check whether the number is available

       // in the array or not

       // Stores the return found status

       int found = check(number);

       // Checks if the found status is -1 then number not found

       if (found == -1)

       {

               // Creates a temporary array with existing size plus one

               double *temp = new double[size + 1];

               // Loops till end of the array and assign

               // each element of the existing array to temp

               for (int c = 0; c < size; c++)

                       temp[c] = dArray[c];

               // Increase 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 to existing 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.";

}// End of function addNumber

// removes a "number" from the "arrayPtr" of "size".

// if "number" is not there -- no action

// note, "size" changes

void varArray::removeNumber(double number)

{

        // Calls the function to check whether the number is available

        // in the array or not

        // Stores the return found status

        int found = check(number);

        // Checks if the found status is not -1 then number found

        if (found != -1)

        {

                // Creates a temporary array with existing size plus one

                double *temp = new double[size];

                // Shift each element to left from found index position to end of the array

                // Loops from found index position to end of the array

                // and assign each next index position element to current index position

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

}// End of function removeNumber

//end of varArray.cpp

//main.cpp

#include <iostream>

#include "vararray.h"

using std::cout; using std::endl;

void testfunc(varArray); // function to test pass-by-value for varArray

int main(){

   varArray a1;

   // testing regular member functions

   a1.addNumber(1.);

   a1.addNumber(2.);

   a1.addNumber(3.);

   a1.addNumber(3.); // trying to add duplicate, should not add it

   cout << "a1 size is after number addition is: " << a1.arraySize() << endl;

   if(a1.check(1.) != -1) // check() returns -1 if number not present

      cout << "1 is present in the array" << endl;

   if(a1.check(5.) != -1)

      cout << "5 is present in the array" << endl;

   cout << "a1 before removal of 2.0: "; a1.output();

   a1.removeNumber(2.);

   cout << "a1 after removal of 2.0: "; a1.output();

   // uncomment this when you debugged the first part

   testfunc(a1); // testing copy constructor

   cout << "a1 after testfunc call: ";

   a1.output(); // if destructor is implemented correctly

                // this should print properly after testfunc completes

   varArray a2, a3;

   a3=a2=a1; // testing stackability of the overloaded assignment

   cout << "a3 after stackable assingment: ";

   a3.output();

   a3=a3; // testing protection against self-assingment

   cout << "a3 after self-assignment: ";

   a3.output();

   return 0;

}

// tests pass-by-value for object of class varArray

void testfunc(varArray va){ // copy constructor is invoked on "va"

    cout << "parameter va: ";

    va.output();

} // destructor is invoked when "va" goes out of scope

//end of main.cpp

Output:

The number found at: 2 Cannot add duplicate number.a1 size is after number addition is: 3 1 is present in the array al before

Add a comment
Know the answer?
Add Answer to:
Variable Size Array with Classes, Testing. Study Code and Object Definition Windows of Microsoft ...
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
  • 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   ...

  • -Create a function output() in C that takes the pointer to the array and its size...

    -Create a function output() in C that takes the pointer to the array and its size and prints the arrays' contests. (malloc format) (function prototype : void output(int *arrayPtr, int size); -Create a function check() in C that takes the pointer to the array and a number and checks if the number is in the array. If the number is in the array, returns the index of the element of the array holding the number. Otherwise, returns -1.

  • Following the instruction This is c++ programming Lab Tasks: 1. Define a dynamic array class in...

    Following the instruction This is c++ programming Lab Tasks: 1. Define a dynamic array class in DynamicArray .h and DynamicArray.cpp files, according to the following UML class diagram: DynamicArray - int arrySize; - int currentSize; int* arrayPtr; + DynamicArray(int size) // Explicit constructor, which you define- allocate space in dynamic memory for an integer array of the given size. + DynamicArray) // Explicit destructor, which you define-de allocate dynamic memory. + additem(int item): bool // Set the value of the...

  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

  • #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int...

    #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int size;    int used;    void doubleSize(); public:    MyArray();    ~MyArray();    int length();    void insertHead(Item i);    void insertTail(Item i);    void deleteHead();    void deleteTail();    void sortAscending();    void sortDescending();    Item operator [](int i){        return myarray[i];    } }; template <typename Item> MyArray<Item>::MyArray(){    size = 5;    used = 0;    myarray = new Item[size];...

  • Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>...

    Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>       T data       TreeNode<T> left       TreeNode<T> right Since this TreeNode is a generic Template, use any data file we've used this quarter to store the data in the BinaryTree. To do this will likely require writing a compare function or operator. Hint: Think LEFT if the index is calculate (2n+1) and RIGHT if index is (2n+2). Source code: #include<iostream> using namespace std;...

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

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • The code should be written in C++. I included the Class used for this function. Also,...

    The code should be written in C++. I included the Class used for this function. Also, please fix the main function if necessary. Description: This function de-allocates all memory allocated to INV. This will be the last function to be called (automatically by the compiler) //before the program is exited. This function also erased all data in the data file. There are other functions that add, process and alter the file; the file printed after those functions will have new...

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
ADVERTISEMENT