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

c++ //Figure 1: vararray.h // variable size array class // Saleh M Alnaeli + Mik

ID: 3693668 • Letter: C

Question

c++

//Figure 1: vararray.h

// variable size array class

// Saleh M Alnaeli + Mikhail Nesterenko

// KSU

#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(int number); // returns index of element containg "number" or -1 if none

void addNumber(int);    // adds number to the array

void removeNumber(int); // 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:

int *dArray; // pointer to the dynamically allocated array

int size;   // array size

};

#endif /* VARARRAY_H_ */

//Figure 2:

// testing the implementation of class varArray

// Saleh Alnaeli + Mikhail Nesterenko

// KSU

#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: "; a1.output();

   a1.removeNumber(2);

   cout << "a1 after removal of 2: "; 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

*/

Objects with Dynamically Allocated Members 1. Variable Size Array with Classes, Testing. Create a project titled Lab11 VarArrayTest. Implement the dynamically expanding and contracting integer array described in the previous lab as a class. You should use class definition show in l/Figure 1: vararrav.h Attributes are a pointer to the dynamically allocated array dAarray and the array size sizeThis 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. o Your code should work with the test hown in //Figure 2:. 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.

Explanation / Answer

main.cpp

#include <iostream>
#include "vararray.h"

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

int main(){
   varArray a1;
   char in = 'b';
   int num = 0;
   while (in != 'q'){
       //asks for input
       cout << "choose and operation (a/r/q) and a number:" << endl;
       cin >> in;

       if (in == 'a'){
           cin >> num;
           a1.addNumber(num);
       }

       if (in == 'r'){
           cin >> num;
           a1.removeNumber(num);
       }
       if (in != 'q')
           a1.output();
   }
}

vararray.cpp
//cpp with function definitions;
//William Komer
//7-22-15

#include "varArray.h"
#include <iostream>

using namespace std;

varArray::varArray(){
   dArray = new int[0];
   size = 0;

}

void varArray::output(){
   for (int i = 0; i < size; i++){
       cout << dArray[i] << " ";
   }
   cout << endl;
}

int varArray::check(int number){
   int index = 0;
   for (int i = 0; i <= size; ++i){
       dArray[i];
       size = 0;
       if (dArray[i] == number){
           index = i;
           break;
       }
       else
           index = -1;
   }
   return index;
}

void varArray::addNumber(int number){
   int tmp = -1;
   for (int i = 0; i <= size; i++){
       if (number == dArray[i])
           return;
       else
           tmp = 1;
   }
   if (tmp == 1){
       int *temp = new int[size + 1];
       for (int i = 0; i < size; i++){
           temp[i] = dArray[i];
       }

       temp[size] = number;
       delete[] dArray;
       dArray = temp;
       ++size;
   }
}

void varArray::removeNumber(int number){
   bool found = false;
   int tmp;
   //int *temp2 = new int[size - 1];
   for (int i = 0; i < size; i++){
       if (dArray[i] == number){
           found = true;
           tmp = i;
       }
   }

   if (found == true){
       --size;
       int *temp2 = new int[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;
   }
}

varArray::varArray(const varArray& org){
   size = org.size;
   dArray = new int[size];
   for (int i = 0; i < size; i++)
       dArray[i] = org.dArray[i];
}

//Destructor
varArray::~varArray(){}

//overloading
varArray& varArray::operator=(const varArray& tmp){
   size = tmp.size;
   delete[] dArray;
   dArray = new int[size];
   for (int i = 0; i < size; ++i){
       dArray[i] = tmp.dArray[i];
   }
   return *this;
}

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(int number); // returns index of element containg "number" or -1 if none
   void addNumber(int);    // adds number to the array
   void removeNumber(int); // 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:
   int *dArray; // pointer to the dynamically allocated array
   int size;   // array size
};

#endif /* VARARRAY_H_ */

sample output

choose and operation (a/r/q) and a number:                                                                                                                  
q                                                                                                                                                           
sh-4.3$ g++ -std=c++11 -o main *.cpp                                                                                                                        
sh-4.3$ main                                                                                                                                                
choose and operation (a/r/q) and a number:                                                                                                                  
a 3                                                                                                                                                         
3                                                                                                                                                           
choose and operation (a/r/q) and a number:                                                                                                                  
a 2                                                                                                                                                         
3 2                                                                                                                                                         
choose and operation (a/r/q) and a number:                                                                                                                  
a 5                                                                                                                                                         
3 2 5                                                                                                                                                       
choose and operation (a/r/q) and a number:                                                                                                                  
q                                                                                                                                                           

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